Saturday, January 10, 2015

File Handling in Perl


Keyboard is considered as a file in most OS
<STDIN> is the file handle used to read from the keyboard
when we use STDIN in <>(angular) brackets it will return the file pointer or file handler using which we can read the contents from the Keyboard

A very simple example

    eg : print "Enter your name";
         $name = <STDIN>; # Read from the keyboard
         print "Good Morning, $name..!! \n";
       
         When you execute the program you will notice that it will print two new lines
         //$name will also contain the new line character
       
Chop function
---------------
This will remove the last character of whatever is given to it.

So we can rewrite the above program as
    print "Enter your name";
    chop($name = <STDIN>);
    print "Good Morning, $name..!! \n";

    This time the program will only print one new line char which is given in the program
    But remenber chopping sometimes might not be good, becuse it blindly removes the last char irrespective of what it is
   
Chomp Function
----------------
The chomp functions similar to the chop, witha difference that it chops of the last char only if it is a new line char
       
    print "Enter your name";
    chomp($name = <STDIN>);
    print "Good Morning, $name..!! \n";
   
Opening a File in Perl
-----------------------

The open command opens the file and return the file handle

For standard input we had a predefined file handle <STDIN>

    eg : $file = "/home/bin/report.txt";
         open XYZ, $file;
         while(<XYZ>) {
         print "Line number $. is $_";
         }
       
         Difference between XYZ and <XYZ> is , XYZ represents the file and <XYZ> represents the contents of the file
       
         $. is the line number
         $_ is the present line which is being read
       
         Sometimes file name may be wrong, or file may not exist, to handle that we use the following construct
       
         $file = "/home/bin/report.txt";
         open XYZ, $file or die "Error in open: $!"
         while(<XYZ>) {
         print "Line number $. is $_";
         }
       
         $! returns the error code or message
       
Reading from the file in Perl
-------------------------------

The <> is the line input operator, all the data read goes into $_

Writing into a file in Perl
----------------------------

    Eg : $out = "/home/bin/out.txt";
         open XYZ, ">$out" or die "Error in Write : $!" ;
         for $i (1..20) {
         print XYZ "$i :: Hello the time is scalar(localtime) \n";
         }
       
         Here ">$out" open the file given in the path
         print XYZ, will write the output to the file and not on the screen
         scalar(localtime) is the function in perl which returns the system time.
       
Appending the file
-------------------

        Eg : $out = "/home/bin/out.txt";
         open XYZ, ">>$out" or die "Error in Write : $!" ;
         for $i (1..20) {
         print XYZ "$i :: Hello the time is scalar(localtime) \n";
         }
       
        Here ">>$out" will open the file for appending
       
Print the file in Perl
------------------------
Print the contents of Perl is very easy

    eg : $out = "/home/bin/out.txt";
         open IN $input or die "Error in opening : $!";
         while(<IN>){
         print;         //By defaults print $_
         }
         close IN;
       
Command line args in Perl
--------------------------

Perl uses special array called @ARGV

List of arguments passed in the command line along with the scipt name will be stored in @ARGV

    eg : If ou invoke perl as
        perl test.pl red blue green
       
        then @ARGV wil be red blue green
       
        printing the command line arguments
       
        foreach(@ARGV) {
        print "$_";
        }
       
Difference between @ARGV and <ARGV>
--------------------------------------

@ARGV will contain the text after the scrpt nam efrom thr command line
<ARGV> will assume that each element after the script name is a name of the file
If no file name is given and <ARGV is used then by default it starts reading from the default input it keyboard

Consider an example
    This program will print the contents of the file you are passing
   
    eg : $lineno = 1;
         while(<>) {
            print $lineno++;
            print ":$_";
         }
       
         perl list_lines.pl file1.txt //contents of the file file1.txt will be printed
         perl list_lines.pl a.txt b.txt c.txt //contents of all the three files
       

       
       

       
   
       
       

1 comment:

  1. It's 2015. Lexical file handles have been available in Perl for years. Please don't write tutorials that don't use them.

    open XYZ, ">>$out" or die "Error in Write : $!" ; # This dated, do not use.

    open my $xyx_fh, ">>$out" or die "Error in Write : $!" ; # This is much better.

    Also, the three-argument version of open() is highly recommended.

    open my $xyx_fh, '>>', $out or die "Error in Write : $!" ; # This is best

    And you wrote:

    "The <> is the line input operator, all the data read goes into $_"

    Which just isn't true. There is only one circumstance where the data read by <> goes into $_ - when the <> is the only expression between the parentheses of a "while (...)" statement.

    If you're going to write a tutorial on Perl, you should probably take the time to ensure the you know what you're talking about!

    ReplyDelete