The best way to learn PERL is to integrate it with your favorite IDE like Eclipse using following update site:
After installing EPIC verify the installation by opening the Perl perspective.
The next step to setup your Perl environment (this could have been done before installing EPIC) is to download and install ActivePerl for Windows (i.e. Perl runtime and command interpreter).
Once you have completed above steps you are ready to use EPIC and Perl to have some fun with Perl scripting from within Eclipse IDE.
“Hello World” using Perl
Create a new “Perl Project” in Eclipse this should switch to Perl perspective. Let say the new project is “Hello World Project”. Right click on new project created and select New -> “Perl File” and save it as helloWorld.pl
Type the following lines of code in “helloWorld.pl” and save it.
#!/usr/bin/perl
use warnings;
print "Hello World! Welcome to Perl.\n"
Now right click on the file name or within the file and select run as “Perl Local” [as shown below] you should see the following output:
File Handling in Perl
Reading from a file: To read a file content we can write a simple Perl script as follows:
open MYFILE,'/dev/perl/test/test.txt' or die "Error while opening a file - $!";
my $lineno = 1;
while ( ){
print $lineno++;
print ": $_";
}
The above code can be changed to accept a file name for the user, and it can be any number of files provided as program arguments. The special variable $ARGV in Perl keeps track of arguments supplied.
my $lineno=1;
my $currentFile="";
#Ask the input location to the user and show file content
print "\n\nReading two files provided as runtime argument to Perl.\n";
while (<>){
if($currentFile ne $ARGV){
$currentFile=$ARGV;
$lineno=1;
print "\n\t\tFile: $ARGV\n\n"
}
print $lineno++;
print ": $_";
}
Writing/copying a file: To make a copy of a given file, we have to open two files. Open INPUTFILE for reading the source data and OUTPUT file to write a copy of source data.
To open a file for writing we use the same syntax as for reading except filename will begin with “> “. Following script is an example to copy a file.
#!/usr/bin/perl
#FileCopy.pl
use warnings;
use strict;
#Copy a file
my $inputfile = $ARGV[0];
my $outputfile = $ARGV[1];
open INPUTFILE, $inputfile or die "Unable to open a file for reading $!";
open OUTPUTFILE, "> $outputfile" or die "Unable to open a file for writing $!";
while( ){
print OUTPUTFILE "$_";
}
Logging in Perl
Logging is the most important part in any programming language as it allows programmers to debug their code with the help of log statements. For an example we will modify above code and add some log statements to help us understand what is happening while the code is executing. [If you noticed when the above code was executed no output was generated on the STDOUT.]
In the code below, you will notice that we have used “>> “ to open a file. This is used if you want to append the content to an existing file. Also using this operator will create a file if it doesn’t exists.
#!/usr/bin/perl
#FileCopy.pl
use warnings;
use strict;
#Open a log file for writing
#NOTE: The directory specified must exists or an error will be reported.
my $logfile = '/dev/perl/logs/application.log';
open LOGFILE, ">> $logfile" or die "Unable to open log file. $!";
print "You won't see anything on console after this....";
#After select LOGFILE every print statement will be written to LOGFILE.
select LOGFILE;
#Copy a file
my $inputfile = $ARGV[0];
my $outputfile = $ARGV[1];
print "Source file name: $inputfile, Destination file name: $outputfile\n";
open INPUTFILE, $inputfile or die "Unable to open a file for reading $!";
print "Source file is opned for reading.\n";
open OUTPUTFILE, "> $outputfile" or die "Unable to open a file for writing $!";
print "Destination file is opened for writing.\n";
print "File Copy - STARTED";
while( ){
print OUTPUTFILE "$_";
print ".";
}
print "\nFile Copy - COMPLETED.\n";
select STDOUT;
NOTE: Don’t forget to “select STDOUT” at the end of your program.
File Validation before reading/writing
Before we can read/write a file we must ensure that the file exists and if it does you should not overwrite it or take a backup of existing file and then write new file etc…
You can rely on if( [operator] $filename) {} statement to do the job.
Where:
- Operator is one of –e, -f, -d, -z, -s, -r, -w, -x, -o i.e. file exists, it’s a file, it’s a directory, file has zero size, file has non-zero size, file is readable, file is writeable, file is executable, and file is owned by you.
- $filename is the name of the file to be tested.
Functions/Subroutines – If you are from a C/Java background you probably want to call it functions i.e. to extract reusable functionality into a separate named code block which may/maynot accept any arguments and may/maynot return something.
In Perl, it is called subroutines. We will use our last example and refactor the code extract file copy functionality using subroutine/function.
If you are using EPIC then process becomes much easier, see screenshot below on how to refactor the code to extract a subroutine.
When you select “Extract Subroutine” you will be prompted to enter a name for the subroutine:
If you notice, EPIC will automatically create a subroutine along with required arguments and also make a call to the subroutine from the extracted location.
This concludes the introduction to Perl programming using Eclipse IDE.
No comments:
Post a Comment