Wednesday 8 July 2020

perl program to Renaming Files with a Prefix

This Perl script renames all the files in the current directory with a given prefix. The prefix can be passed as a command-line argument.

Method 1: Using rename function

#!/usr/bin/perl

my $prefix = shift @ARGV || '';

opendir my $dh, '.' or die "Couldn't open current directory: $!";

while (my $filename = readdir $dh) {

    next if $filename =~ /^\./; # Skip dotfiles

    my $newname = $prefix . $filename;

    rename $filename, $newname or warn "Couldn't rename $filename: $!";

}

closedir $dh;


or


#!/usr/bin/perl

use strict;

use warnings;

use File::Glob;


my $prefix = 'new_';

my $pattern = '*.txt';


foreach my $file (glob $pattern) {

    my $new_file = $prefix . $file;

    rename $file, $new_file or die "Can't rename $file to $new_file: $!";

}

Save the script as rename.pl and run it with perl rename.pl newprefix_. This will add the prefix "newprefix_" to all the files in the current directory.


Method 2: Using File::Copy

This method uses the File::Copy module to copy files with a new name and then delete the old file. We can use the File::Glob module to get a list of files matching a pattern and then use the File::Copy module to copy each file with the new name.

#!/usr/bin/perl

use strict;

use warnings;

use File::Glob;

use File::Copy;

my $prefix = 'new_';

my $pattern = '*.txt';

foreach my $file (glob $pattern) {

    my $new_file = $prefix . $file;

    copy $file, $new_file or die "Can't copy $file to $new_file: $!";

    unlink $file or die "Can't delete $file: $!";

}

Save the script as rename_files_method2.pl and run it with perl rename_files_method2.pl. This will rename all .txt files in the current directory with the prefix new_.


Method 3: Using regular expressions

This method uses regular expressions to modify the filenames with a prefix. We can use the File::Glob module to get a list of files matching a pattern and then use regular expressions to modify each filename.

use strict;

use warnings;


use File::Glob;

my $prefix = 'new_';

my $pattern = '*.txt';

foreach my $file (glob $pattern) {

    my $new_file = $file;

    $new_file =~ s/^/$prefix/;

    rename $file, $new_file or die "Can't rename $file to $new_file: $!";

}

Save the script as rename_files_method3.pl and run it with `perl rename_files

all .txt files in the current directory with the prefix new_.


Method 4: Using File::Find

This method uses the File::Find module to recursively find and rename files with a prefix. The File::Find module provides a way to traverse a directory tree and apply a function to each file found. We can use the rename function within the wanted function to rename each file with the specified prefix.

#!/usr/bin/perl

use strict;

use warnings;

use File::Find;

my $prefix = 'new_';


sub rename_file {

    if (-f && /^.*\.txt$/) {

        my $new_file = $prefix . $_;

        rename $_, $new_file or warn "Can't rename $_ to $new_file: $!";

    }

}

find(\&rename_file, '.');

Save the script as rename_files_method4.pl and run it with perl rename_files_method4.pl. This will recursively find and rename all .txt files in the current directory and its subdirectories with the prefix new_.


Method 5: Using opendir and readdir

This method uses the opendir and readdir functions to find and rename files with a prefix. The opendir function opens a directory, and the readdir function reads the contents of the directory. We can use regular expressions to match the filenames and then use the rename function to rename each file with the specified prefix.

#!/usr/bin/perl

use strict;

use warnings;

my $prefix = 'new_';

opendir(my $dh, '.') or die "Can't open current directory: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    rename $file, $new_file or warn "Can't rename $file to $new_file: $!";

}

closedir $dh;

Save the script as rename_files_method5.pl and run it with perl rename_files_method5.pl. This will find and rename all .txt files in the current directory with the prefix new_.


Method 6: Using File::Copy

This method uses the File::Copy module to copy files with a prefix to a new directory and then rename the files. The File::Copy module provides functions for copying and moving files. We can use the copy function to copy the original files to a new directory with the prefix, and then use the rename function to rename the files with the specified prefix.

use strict;

use warnings;

use File::Copy;

my $prefix = 'new_';

my $new_dir = 'new_files';

mkdir $new_dir unless -d $new_dir;

opendir(my $dh, '.') or die "Can't open current directory: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    copy($file, "$new_dir/$new_file") or warn "Can't copy $file: $!";

}

closedir $dh;

opendir($dh, $new_dir) or die "Can't open $new_dir: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    rename("$new_dir/$file", "$new_dir/$new_file") or warn "Can't rename $file: $!";

}

closedir $dh;

Save the script as rename_files_method6.pl and run it with perl rename_files_method6.pl. This will copy all .txt files in the current directory to a new directory named new_files with the prefix new_, and then rename the files with the specified prefix.


Method 7: Using File::Find::Rule

This method uses the File::Find::Rule module to find and rename files with a prefix. The File::Find::Rule module provides a simple interface for finding files that match specified criteria. We can use the name method to match filenames and then use the rename function to rename each file with the specified prefix.

use strict;

use warnings;

use File::Find::Rule;

my $prefix = 'new_';

my @files = File::Find::Rule->file()->name('*.txt')->in('.');

foreach my $file (@files) {

    my $new_file = $prefix . $file;

    rename $file, $new_file or warn "Can't rename $file to $new_file: $!";

}

Save the script as rename_files_method7.pl and run it with perl rename_files_method7.pl. This will find and rename all .txt files in the current directory with the prefix new_.

Labels:

Monday 18 November 2019

Perl Unlink - what is?

Perl Unlink:
perl unlink is used to remove the files. based on our requirements we can delete more than single file using unlink function.
by storing array or list elements  as filename we can achieve group of filenames deletion.


Cons:
1. Unlink will not notify which files could not remove. but it can be manageable using perl warning functions.
2. Unlink will not work on many os.
3. Super user permission necessary for Unlinking a directory although it is not recommended for usual coding.

Read more »

Labels: ,

Sunday 1 December 2019

perl piped open - reading data from a system process?

Perl Piped Open:

open function is very useful for reading or writing the data files on the system and apart from this file processing we can use pipe symbol for getting data from a system process or sending the data to a system process can be achieved.   lets try this now,


Read more »

Labels: , , , , , ,

Saturday 8 August 2020

Perl DB Connection Tutorial with Different databases

 Perl provides support for connecting and interacting with a variety of databases. Here are some examples of connecting to different databases using Perl:

Method 1: Connecting to MySQL using DBI

The Perl DBI (Database Interface) module provides a consistent interface for connecting to and interacting with different databases. Here's an example of connecting to a MySQL database using DBI:

Read more »

Labels:

Thursday 9 June 2022

perl vlsi program for Design Rule Checker


#!/usr/bin/perl


use strict;

use warnings;


# Read design rules from file

my $rule_file = shift;

open my $fh, "<", $rule_file or die "Error: Could not open rule file: $!\n";

my $rules = do { local $/; <$fh> };

close $fh;


# Read layout design from file

my $design_file = shift;

open $fh, "<", $design_file or die "Error: Could not open design file: $!\n";

my $design = do { local $/; <$fh> };

close $fh;


# Check design against rules

my $errors = 0;

foreach my $rule (split /\n/, $rules) {

    chomp $rule;

    $rule =~ s/^\s+|\s+$//g;

    next if $rule =~ /^#/;

    if ($rule =~ /^(\S+)\s+(.*?)$/) {

        my $rule_type = $1;

        my $rule_pattern = $2;

        if ($rule_type eq "layer") {

            # Check for correct layer usage

            my @matches = $design =~ /$rule_pattern/g;

            if (@matches) {

                print "Error: Found $rule_type \"$rule_pattern\" in layout design\n";

                $errors++;

            }

        }

        elsif ($rule_type eq "spacing") {

            # Check for minimum spacing between objects

            my $spacing = $rule_pattern;

            my $pattern = "\\s*[Mm]\\s*\\(\\s*\\d+\\s*\\)\\s*[Mm]"; # matches two adjacent metal layers

            my @matches = $design =~ /$pattern/g;

            foreach my $match (@matches) {

                if ($match =~ /($pattern){0,$spacing-1}/) {

                    print "Error: Found adjacent objects with spacing less than $spacing in layout design\n";

                    $errors++;

                    last; # exit loop once an error is found

                }

            }

        }

        else {

            print "Warning: Unknown design rule type \"$rule_type\"\n";

        }

    }

}


if ($errors == 0) {

    print "Design passed all design rule checks.\n";

}

else {

    print "Design failed $errors design rule checks.\n";

}


 it will print out any errors or warnings that are detected during the design rule checking process. If there are no errors, it will print a message indicating that the design has passed all design rule checks.

design rules are specified in a file, where each rule is a line that starts with a rule type ("layer" or "spacing"), followed by a rule pattern that specifies the layer or spacing requirements. 

For example:

# Design rules file

layer M1

layer M2

spacing 3 

run it from the command line and specify the design rules file and layout design file as arguments. For example:

$ perl design_rule_checker.pl design_rules.txt layout_design.gds

 design_rule_checker.pl is the name of the Perl program, design_rules.txt is the name of the file containing the design rules, and layout_design.gds is the name of the layout design file to be checked.

example design rules file that checks for some common design rule violations:

# Design Rules File



# Define spacing rules

spacing metal1_metal1 0.12

spacing metal1_via1 0.12

spacing metal2_metal2 0.15

spacing metal2_via2 0.15



# Define width rules

width metal1 0.25

width metal2 0.4



# Define enclosure rules

enclosure metal1_via1 0.1

enclosure metal2_via2 0.2

The spacing rule checks the minimum distance between two layers, while the width rule checks the minimum width of a metal layer. The enclosure rule checks the minimum amount of metal surrounding a via.

The metal1_metal1, metal1_via1, metal2_metal2, and metal2_via2 keywords are the layer types that the rules apply to. These keywords should match the layer names used in the layout design file.

To use this design rules file with the design_rule_checker.pl program, you would specify the filename as the first argument when running the program:

$ perl design_rule_checker.pl design_rules.txt layout_design.gds


#!/usr/bin/perl
use strict;
use warnings;

# Read in the design rules file
my $rules_file = $ARGV[0];
open(my $rules_fh, '<', $rules_file) or die "Could not open $rules_file: $!";

# Parse the design rules and store them in a hash
my %rules;
while (my $line = <$rules_fh>) {
    chomp $line;
    my ($rule_type, $layer_type, $value) = split ' ', $line;
    $rules{$rule_type}{$layer_type} = $value;
}
close $rules_fh;

# Read in the layout design file
my $layout_file = $ARGV[1];
open(my $layout_fh, '<', $layout_file) or die "Could not open $layout_file: $!";

# Parse the layout design and check against the design rules
my $line_number = 0;
while (my $line = <$layout_fh>) {
    $line_number++;
    chomp $line;

    # Check for minimum spacing violations
    if ($line =~ /^S\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {
        my ($type1, $layer1, $type2, $layer2, $distance, $extension) = ($1, $2, $3, $4, $5, $6);
        if ($rules{'spacing'}{"$layer1\_$layer2"} && $distance < $rules{'spacing'}{"$layer1\_$layer2"}) {
            print "Error: Minimum spacing violation on line $line_number\n";
        }
    }

    # Check for minimum width violations
    if ($line =~ /^W\s(\S+)\s(\S+)/) {
        my ($layer, $width) = ($1, $2);
        if ($rules{'width'}{$layer} && $width < $rules{'width'}{$layer}) {
            print "Error: Minimum width violation on line $line_number\n";
        }
    }

    # Check for minimum enclosure violations
    if ($line =~ /^E\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/) {
        my ($type1, $layer1, $type2, $layer2, $distance) = ($1, $2, $3, $4, $5);
        if ($rules{'enclosure'}{"$layer1\_$layer2"} && $distance < $rules{'enclosure'}{"$layer1\_$layer2"}) {
            print "Error: Minimum enclosure violation on line $line_number\n";
        }
    }
}
close $layout_fh;

The design rules file should contain the following information:

  • Minimum spacing between two layers
  • Minimum width for a layer
  • Minimum enclosure of one layer around another layer

The layout design file should contain the following information for each component:

  • Type of component
  • Layer of component
  • Position of component
  • Dimensions of component

The design_rule_checker.pl program will output an error message for any violations of the design rules, indicating the type of violation and the line number in the layout design file where the violation occurred.


Labels:

Monday 8 June 2020

perl program to copy files from one directory to another directory

Perl script copies all the files with a given extension from one directory to another. The source and destination directories are passed as command-line arguments, along with the file extension.

Method 1:

#!/usr/bin/perl

use File::Copy;

my ($src_dir, $dest_dir, $ext) = @ARGV;

opendir my $dh, $src_dir or die "Couldn't open directory $src_dir: $!";

while (my $filename = readdir $dh) {

    next unless $filename =~ /\.$ext$/; # Match extension

    my $src_path = "$src_dir/$filename";

    my $dest_path = "$dest_dir/$filename";

    copy $src_path, $dest_path or warn "Couldn't copy $filename: $!";

}

closedir $dh;

Save the script as copyfiles.pl and run it with perl copyfiles.pl sourcedir destdir txt. This will copy all the files with the extension "txt" from "sourcedir" to "destdir".

Method 2:Using File::Find

The File::Find module provides a way to recursively traverse a directory tree and perform actions on files that match certain criteria.

#!/usr/bin/perl

use File::Find;

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

find(sub {

    return unless -f $_; # Skip directories

    return unless /\.$file_ext$/; # Match file extension

    my $source_file = $File::Find::name;

    $source_file =~ s/^\Q$source_dir\E//; # Remove source directory from path

    my $dest_file = "$dest_dir$source_file";

    mkpath($dest_file); # Create parent directories if necessary

    copy($File::Find::name, $dest_file) or warn "Couldn't copy $File::Find::name: $!";

}, $source_dir);

Save the script as copy_files_method2.pl and run it with perl copy_files_method2.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory and preserve the directory structure.

Method 3: Using system()

The system() function in Perl allows you to execute shell commands. You can use the cp command to copy files from one directory to another.

#!/usr/bin/perl

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

my $command = "cp $source_dir/*.$file_ext $dest_dir/";

system($command) == 0 or die "Couldn't execute command: $command";

Save the script as copy_files_method3.pl and run it with perl copy_files_method3.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory using the cp command.

Method 4: Using opendir() and readdir()

This method uses the opendir() and readdir() functions to read the contents of a directory and then copy the files matching the specified file extension.

#!/usr/bin/perl

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

opendir(DIR, $source_dir) or die "Can't open $source_dir: $!";

my @files = readdir(DIR);

closedir(DIR);

foreach my $file (@files) {

    next unless $file =~ /\.$file_ext$/; # Match file extension

    my $source_file = "$source_dir/$file";

    my $dest_file = "$dest_dir/$file";

    copy($source_file, $dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method4.pl and run it with perl copy_files_method4.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

Method 5: Using glob()

The glob() function returns a list of filenames that match a specified pattern. You can use this function to get a list of files matching the specified file extension and then copy them to the destination directory.

#!/usr/bin/perl

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

foreach my $file (glob("$source_dir/*.$file_ext")) {

    my $dest_file = "$dest_dir/" . (split /\//, $file)[-1];

    copy($file, $dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method5.pl and run it with perl copy_files_method5.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

Method 6: Using Path::Tiny

The Path::Tiny module provides a simple interface to manipulate file and directory paths. It also provides a copy() function that can be used to copy files from one directory to another.

#!/usr/bin/perl

use Path::Tiny;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

foreach my $file (path($source_dir)->children(qr/\.${file_ext}$/)) {

    my $dest_file = path($dest_dir, $file->basename);

    $file->copy($dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method6.pl and run it with perl copy_files_method6.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

The Path::Tiny module also provides other useful functions for working with files and directories, such as move(), rename(), and unlink(). It's worth exploring if you need to do more complex file manipulation in your Perl scripts.

Labels:

Monday 4 November 2019

perl read file - 15 ways

Method 1:


open(FH,'<file.txt') or die "Cant open file $!";

print FH;

close(FH);

Read more »

Labels:

Friday 24 January 2020

What is the opendir() function in Perl?


opendir  function is used for reading the content of a particular folder.

Method 1:


opendir(DIRHANDLE,"/some director/path") or die "Can't open directory";

Method 2:


opendir DIRHANDLE, '/some diretory/path' or die "Can't open directory";

Read more »

Labels: , , , , , , , ,