Tuesday, November 8, 2011

WRITING AND READING PERL 5 SCRIPT

WRITING AND READING PERL 5 SCRIPT




REVISED: Sunday, March 3, 2013




You will learn how to write and read Perl 5 script.

I.  PERL 5 FILE OUTPUT AND  FILE INPUT SOURCE CODE  EXAMPLES

Perl's strengths are in text manipulation and parsing; therefore, Perl  5 has complete file input and output capabilities, especially syntax for line-at-a-time sequential input.

A. PERL 5 FILE OUTPUT SOURCE CODE EXAMPLE

"Copy Paste" the following Perl 5 program into your text editor:

#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
use diagnostics;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict; 
    
#Stack trace.
use Carp ();
  local $SIG{__WARN__} = \&Carp::confess;

open STDOUT, ">c:/strawberry/mary.txt";       
print "Mary had a little lamb.\n";            
print "Its fleas were white as snow.\n"; 
print "And every where that Mary went,\n";    
print "the fleas were sure to go.\n"; 
close STDOUT; 

"File Save As" writeMary.pl using the path of the Perl 5 download; e.g.:

c:\strawberry\writeMary.pl

Open Perl 5, depending on your platform you will have a Perl 5 prompt similar to the following:

C:\WINDOWS\system32>

From the above Perl 5 prompt type:

C:\WINDOWS\system32>c:\strawberry\writeMary.pl

Press Enter.

Perl 5 will display the following on your Perl 5 prompt:

C:\WINDOWS\system32>

If you were expecting to see the poem display on the Perl 5 prompt screen, surprise!

Lets examine the code line by line and see what happened to our poem.

Line 19:  open STDOUT, ">c:/strawberry/mary.txt"

Our open has two arguments: The first argument is a filehandle, in this example the filehandle is STDOUT. The two arguments are separated by a comma.  The second argument is a single string comprising how to open it, the path, and what to open. If the "expression" begins with ">", as ours does, the file is opened for output.  When you name a file in an "open for writing" statement, as we have done, the file may or may not exist before opening. If it does exist, first it is wiped clean as soon as it's opened. If it does not exist, first it is created with no contents.  mary.txt  does not exist; therefore, mary.txt is created with no contents.  open returns true when it works, and false when it fails.  

A print statement sends output to STDOUT by default.  You can specify the file you want to use by opening a filehandle to it. Then you use the new filehandle in your print statement, instead of the default STDOUT filehandle. There is no comma after the filehandle in the print statement.  You can name a filehandle anything you like.  By convention, a filehandle is written in upper case, but it does not have to be.

Since a print statement sends output to STDOUT by default we did not have to write lines 2-5 as shown below.  However, if you change the program it will work because both are the same.

Line 20:  print STDOUT "Mary had a little lamb.\n";           
Line 21:  print STDOUT "Its fleas were white as snow.\n";
Line 22:  print STDOUT "And every where that Mary went,\n";   
Line 23:  print STDOUT "the fleas were sure to go.\n";

When you're finished, you close the filehandle as shown below.

Line 24:  close STDOUT;

In summary, we created a file, which we opened for output, named mary.txt located at path c:/strawberry/ then we wrote our poem to that file and closed that file.  That is why it did not appear displayed on the Perl 5 prompt screen.

B. PERL 5 FILE INPUT SOURCE CODE EXAMPLE

"Copy Paste" the following Perl 5 program into your text editor:


#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
use diagnostics;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict; 
    
#Stack trace.
use Carp ();
  local $SIG{__WARN__} = \&Carp::confess;

#Specify the file
my $file = "c:/strawberry/mary.txt";
#Open the file and read data
#Die with grace if it fails
open (FILE, "<$file") or die "Can't open $file: $!\n";
my @lines = <FILE>;
print @lines;
close FILE;

"File Save As" readMary.pl using the path of the Perl 5 download; e.g.:

c:\strawberry\readMary.pl

From the Perl 5 prompt type the following:

C:\WINDOWS\system32>c:\strawberry\readMary.pl

Press Enter and the following will display on your screen:


C:\WINDOWS\system32>c:\strawberry\readMary.pl 
Mary had a little lamb.
Its fleas were white as snow.
And every where that Mary went,
the fleas were sure to go.

C:\WINDOWS\system32>

Lets examine the code line by line and see what happened to our poem.

Line 19:  #Specify the file.
Line 20:  my $file = "c:/strawberry/mary.txt";

As the comment in Line 1: states, Line 2: specifies the file by assigning "c:/strawberry/mary.txt" to $file.

As shown below, the file $FILE is opened or if the file can not be opened an error message is displayed on the screen. 

Line 21:  #Open the file and read data
Line 22:  #Die with grace if it fails
Line 23:  open (FILE, "<$file") or die "Can't open $file: $!\n";

Our open has two arguments: The first argument is a filehandle, in this example the filehandle is FILE. The two arguments are separated by a comma.  The second argument is a single string comprising how to open it, the path, and what to open. If the "expression" begins with "<", as ours does, the file is opened for input, reading.  When you name a file in an "open for reading" statement, as we have done, the file may or may not exist before opening. If it does exist it's opened. open returns true when it works, and false when it fails. If our open had failed the condition would have been false and the or would have printed both the error message "Can't open $file: and the appropriate system error message provided by $!.  

As shown below the file is read into the array @lines.

Line 24:  #Read the file into the array @lines.
Line 25:  my @lines = <FILE>;

print displays the poem on the screen.

Line 26:  #Print the file to the screen.
Line 27:  print @lines;

We are done! Therefore, as shown below, the file is closed.

Line 28:  #Close the file.
Line 29:  close FILE;

C. PERL 5 FILE INPUT OUTPUT SUMMARY

MODE DESCRIPTION
+<
READ,WRITE
+>
READ,WRITE,TRUNCATE,CREATE
+>>
READ,WRITE,CREATE,APPEND


You have learned how to write and read Perl 5 script.

Elcric Otto Circle




-->


-->


-->









How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"







No comments:

Post a Comment