Category Archives: Perl Scripts

It contains Perl Scripts that I learn newly.

Linux System Command to Zip Files

The linux system command to create zip single or multiple files are as follows :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext

If the files are in another folder and you see those file names if the downloaded zip file in which you wish not to see these files, then use the following command :

zip zip_file_name.zip file_name1.ext file_name2.ext file_name3.ext

How search a string in an array?

To search a content in an array in perl, we can use the following format,

my @a=('Hai','Leop','mac');
my @b = grep(/a/i, @a);
print join ",",@b;

The output will be as follows:

Hai,mac

How to get the size of a hash in Perl?

To get the hash in Perl ,

%hash=(
     a=>'Max',
     b=>'Rac',
     c=>'Neo'
);
print scalar keys(%hash);

The out put will be as follows:

3

replace() in Perl?

It’s as follows:

sub replace
{
	my($old,$new,$string)=@_;
	$string=~s/$old/$new/;
	return $string;
}

It should be called as follows:

print &replace($to_be_changed_from,$change_it_to,$full_string)

How to compress a file in Perl?

To compress a file in Perl, it’s better to use the gzip method. For that we can use the module use IO::Compress::Gzip qw(gzip $GzipError) ;.

Eg:-

use IO::Compress::Gzip qw(gzip $GzipError) ;
my $input="new.pdf";
my $output="new1.pdf.gz";
gzip $input => $output or die "gzip failed: $GzipError\n";