Perl Code Snippets
Perl Search for Files and Directories
1 2 3 4 5 6 7 |
# Use the file module to find files use File::Find; # Current file can be accessed by $File::Find::name # Inside the sub you can apply any command for example # changing the file mode to 777 find (sub { system("chmod 777 $File::Find::name"); },"/some/path"); |
Perl Remove Directory
1 2 3 4 5 |
# Use the file module use File::Path; # Remove the entire directory rmtree ("/some/path"); |
Perl Extract File and Directory Names from Full Paths
1 2 3 4 5 6 7 8 |
# Use the file module use File::Basename; # Extract base name $filename = basename ("/some/path"); # Extract directory name $dir = dirname ("some file name"); |
Perl Copy File
1 2 3 4 5 6 7 8 9 |
# Use file module use File::Copy; # Copy files copy("file1", "file2") or die "Copy failed: $!"; # Move files move("/dev1/fileA", "/dev2/fileB"); |
Perl Command Line Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Use Getopt module use Getopt::Long; # In the example below # (a) and (g) do not take values # (gr) and (gc) take values # if the command option (a) is used then $add is 1 # If the (gr) command option is used with some value # then the $getrel variable will hold that value # It is up to the programmer to choose the names # for the command options and the variable names # that hold their values $result = GetOptions ( "a" => \$add, "g" => \$get, "gr=f" => \$getrel, "gc=f" => \$getcase, ); # If $result is not 1 then command options # were not used properly if ($result != 1) { # Print usage } |
Perl Environment Variables
1 2 |
# Environment variables are accessed as follows $ENV{env_name}; |
Perl Check File Exists
1 2 3 4 |
if (-f file_name) { # Do something } |
Perl Get User Input
1 |
chomp ($userInput = ); |
Perl Upper Case
1 |
$uc_string = uc ($sc_string); |
Perl String Comparison
1 |
if ($userInput eq "Y") # u can use ne |
Perl Exit Error
1 |
die "Error: $!\\n"; |
Perl Get Number of Lines of an Array
1 |
my $numberLines = $#Array + 1; |
Perl Change Directory
1 2 |
chdir ($dir) or die "Error : can not cd to [ $dir ] : $!\\n"; |
Perl Number Comparison
1 2 3 4 |
if ($num1 == $num2) { # Do something } |
Perl Delete File
1 2 |
unlink <$file_name> or die "Error : can not delete [ $file_name ] for reading : $!\\n"; |
Perl Delete List of Files
1 |
unlink (*); |
Perl Read a List of Files and Directory Names
1 |
my @filesArr = </some/path>; |
Perl For Loop
1 2 3 4 |
for ($t = $from; $t <= $to; $t++) { # Do something } |
Perl Foreach
1 2 3 4 |
foreach (@array) { # current entry found in $_ } |
Perl Search for a Pattern in an Array
1 |
my @keyWords = grep (/%.%/, @array); |
Perl Directory Exists
1 2 3 4 |
if (-d $dir) { # Do something } |
Perl Sort Array
1 2 |
foreach $x (sort @array); foreach $x (reverse sort @array); |
Perl Print Formatted Text
1 |
printf("%-30.30s %-15.15s %-10.10s\n", "Database", "Owner", "Created On"); |
Perl Create Directory
1 2 |
mkdir ("SCCS", 0755) or die "doAdd: Error : can not mkdir SCCS : $!\n"; |
Perl Replace Text Remove White Space
1 2 |
$var =~ s/^\s+//; $var =~ s/\s+$//; |
Perl Dynamically Add to an Array
1 |
push (@my_files, $my_file); |
Perl String Length
1 |
$length = length($str); |
Perl File Extension Regular Expression Check
1 2 3 4 |
if ($fileName =~ /\.txt\b/) { # Do something } |
Perl Hash Definition
1 2 |
my %mHash; $mHash{$var} = $new_value; |
Perl Split String
1 |
my @SPLT = split ("_", $string); |
Perl Hash Loop
1 2 3 4 |
while (($key, $val) = each (%myHash)) { # Do something } |
Perl Pass Parameters to Function
1 2 3 4 |
sub myFunc { my ($var1, $var2) = @_; } |
Perl Current Working Directory
1 2 |
use Cwd; my $currDir = getcwd; |
Perl Number of Command Line Arguments
1 |
my $numArgs = $#ARGV + 1; |
Perl Split into Variables and Array
1 2 3 |
# The first 3 tokens go to the variables # and the rest go to the array ($v1, $v2, $mm, $v3, @array) = split (" ", $string); |
Perl Substring
1 |
my $firstChar = substr ($str, 0, 1); |
Perl While Loop
1 2 3 4 |
while ($Line ne "xyz") { # Do something } |
Perl Remove Trailing Characters
1 |
chomp $string; |
Perl Initialize an Array
1 |
@DBS = ("$database_1", "$database_2"); |