Sunday, July 17, 2011

To repeat words in perl

This makes use of the repeatation operator "x" to repeat the words

Content of the perl file:

$dd="hello";
$ss="$dd "x 5;
print $ss;

Output:

G:\perl_programs>perl perl_repeat.pl
hello hello hello hello hello
G:\perl_programs>

To find the filesize in perl

Solution 1:
For example for file test.txt.It is done by using the stat function in File module

use File::stat;
my $filesize = stat("test.txt")->size;
print "Size: $filesize\n";

Output:

Size: 42

Solution 2 :
It is done using the -s option

my $dd= -s "test.txt";
print "Size : $dd";

Output:

Size: 42

Call by reference perl

When you call by reference ,you use directly the scalar reference and so any change in the function causes the change to happen in the main program.

Content of the perl file:

@array=("hello","aaaa","lll","ggg");
print "The content of array element 0 and 1 before function call is $array[0] and $array[1]"."\n";
function(@array);
print "The content of array element 0 and 1 after function call is $array[0] and $array[1]";
sub function()
{
$_[0]="Great";#this is the scalar reference of the first element in the array.
$_[1]="awesome";#this is the scalar reference of the second element in the array.
}

Output:

G:\perl_programs>perl perl_pass_by_reference.pl
The content of array element 0 and 1 befoe function call is hello and aaaa
The content of array element 0 and 1 after function call is Great and awesome
G:\perl_programs>

finding no of array elements perl

Content of the perl file:

@array=("hello","aaaa","lll","ggg");
print "The no of elements in the array is ".scalar(@array)."\n";
#it can also be done by this way
$no=@array;
print "The no of elements in array is ". $no;

Output:

G:\perl_programs>perl perl_array_no.pl
The no of elements in the array is 4
The no of elements in array is 4


G:\perl_programs>

Tuesday, July 12, 2011

Deleting the backup files in the directory perl

Use simply

unlink(<*.bak>);  
Here i have assumed the backup files to end with .bak this wildcard can be changed as per your requirement
in the perl script and execute it will delete all the files ending up .bak in the current directory


you can also execute from the command line as follows:

G:\perl_programs>perl -e "unlink(<*.bak>);"

Globbing concept in perl

Perl supports globbing which helps you to find files with wildcard characters.
To print all the files starting with "f" in the current directory use the script below


Perl script:

@array=; # this can be modified to your need
print "@array\n";

OUTPUT:

G:\perl_programs>perl perl_globbing.pl
function_perl.pl function_sendarray.pl


This concept can be also used to delete all the backup files ending with .bak in the current directory using the following lines:


unlink(<*.bak>);

Redirecting the input from another file perl

Content of the data:

 root> cat data
while(<STDIN>)
{
print();
}
while(<>)
{
print();
}
root> 

Content of the Perl File:
Perl file:

root> cat kk.pl
while(<>)
{
print();
}

Execution of the perl file:

root> perl kk.pl < data
while(<STDIN>)
{
print();
}
while(<>)
{
print();
}
root>