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>