Perl Code Snippets
Below are some useful Perl code snippets for use in everyday projects.
Sometimes it may be necessary to accept input from the user so that it can be utilised within a script. This can be achieved using 'STDIN'. Here, the input is placed into a variable called 'user' and then displayed as part of a greeting.
use strict; use warnings; # Greeting variable. my $greeting = "Hello"; # Accept user input. print "Please enter your name: "; my $user =; # Display greeting. print "$greeting $user";
When dealing with directories and files it may be necessary to check for their existence. Below is an example of how to do this.
use strict; use warnings; # Example path and file. my $examplePath = "C:\\demo\\"; my $exampleFile = "C:\\demo\\example.txt"; # Check if the path exists. if (-d $examplePath) { print "The path '$examplePath' exists.\n" } else { print "The path '$examplePath' doesn't exist.\n" } # Check if the file exists. if (-e $exampleFile) { print "The file '$exampleFile' exists.\n" } else { print "The file '$exampleFile' doesn't exist.\n" }
This works in the same way on Unix like operating systems, such as Linux.
use strict; use warnings; # Example path and file. my $examplePath = "/home/demouser/demo"; my $exampleFile = "/home/demouser/demo/example.txt"; # Check if the path exists. if (-d $examplePath) { print "The path '$examplePath' exists.\n" } else { print "The path '$examplePath' doesn't exist.\n" } # Check if the file exists. if (-e $exampleFile) { print "The file '$exampleFile' exists.\n" } else { print "The file '$exampleFile' doesn't exist.\n" }
Perl can be used to copy and move files.
use strict; use warnings; use File::Copy; # Example paths and files. my $exampletPathSource = "C:\\demo\\"; my $exampletPathDestination = "C:\\demo\\backup"; my $exampleFile1 = "C:\\demo\\example1.txt"; my $exampleFile2 = "C:\\demo\\example2.txt"; # Copy the example1.txt file. copy($exampleFile1, $exampletPathDestination) or die "Copy failed: $!"; # Move the example2.txt file. move($exampleFile2, $exampletPathDestination) or die "Move failed: $!";
This works in the same way on Unix like operating systems, such as Linux.
use strict; use warnings; use File::Copy; # Example paths and files. my $exampletPathSource = "/home/demouser/demo"; my $exampletPathDestination = "/home/demouser/demo/backup"; my $exampleFile1 = "/home/demouser/demo/example1.txt"; my $exampleFile2 = "/home/demouser/demo/example2.txt"; # Copy the example1.txt file. copy($exampleFile1, $exampletPathDestination) or die "Copy failed: $!"; # Move the example2.txt file. move($exampleFile2, $exampletPathDestination) or die "Move failed: $!";
Here is an example of how to generate a random number between two values, in this case 1 and 59, then display it.
use strict; use warnings; # Generate random number. my $random_number = int(rand(59)) + 1; # Display the result. print "Random number between 1 and 59: $random_number\n";
Here, a random 20 character password is generated from a specified set of characters.
use strict; use warnings; # Possible characters in password. my @allowedChars = ('a'..'z', 'A'..'Z', 0..9,'!','_','-','$','@','!'); # Create a random 20 character password. my $randPassword = join '', map $allowedChars[rand @allowedChars], 0..20; # Display the generated password. print "$randPassword"
A text file can be read from and processed line by line.
use strict; use warnings; # Text file to read. my $filename = "C:\\demo\\example.txt"; # Open the text file using a file handler. open(FH, '<', $filename) or die $!; # Read the text file using a file handler. while (<FH>) { # Display the line of text. print $_; } # Close the file handler. close(FH);