Reading/Writing Files in Perl

Discussion in 'Perl' started by pradeep, Feb 18, 2008.

Thread Status:
Not open for further replies.
  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    File input and output is an integral part of every programming language. Perl has complete file input and output capabilities, but it has especially handy syntax for line-at-a-time sequential input.The basics of handling files are simple: you associate a filehandle with an external entity (usually a file) and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle.

    A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened.

    Three basic file handles are - STDIN, STDOUT, and STDERR.

    Opening and Closing Files



    There are following two functions with multiple forms which can be used to open any new or existing file in Perl.

    Code:
    open FILEHANDLE, EXPR;
      open FILEHANDLE;
      sysopen FILEHANDLE, FILENAME, MODE, PERMS;
      sysopen FILEHANDLE, FILENAME, MODE;
    Here FILEHANDLE is the file handle returned by open function and EXPR is the expression having file name and mode of opening the file.

    File Output In Perl



    Code:
    $append = 0;
      if ($append)
      {
          open(OUTFILE, ">filename.out"); #open for write, overwrite
      }
      else
      {
          open(OUTFILE, ">>filename.out"); #open for write, append
      }
      print OUTFILE "Timestamp: "; #write text without newline
      print OUTFILE join(',',localtime(time)); #write text
      print OUTFILE "\n"; #write newline
      
      ### Print text usinh heredoc
      print OUTFILE <<"MyLabel";
      Steve was here
      and now is gone
      but left his name
      to carry on.
      MyLabel
      
      # close the file
      close(OUTFILE);

    File Input in Perl



    Opening for Read requires no angle brackets in the filename. If you wish, you can put in a left angle bracket <, which means "input file". It's good practice to close any files you open. Files can be read line by line, or the entire contents of the file can be dumped into a list, with each list element being a line. Here is an example of a program that reads a file, capitalizes each line, and prints it to the screen:

    Reading a File a Line at a Time

    Code:
    open(INFILE, "<filename.out");
      while(<INFILE>)
      {
          # Good practice to store $_ value because
          # subsequent operations may change it.
          my($line) = $_;
      
          # Good practice to always strip the trailing
          # newline from the line.
          chomp($line);
      
          # Convert the line to upper case.
          $line =~ tr/[a-z]/[A-Z]/;
      
          # Print the line to the screen and add a newline
          print "$line\n";
      }
    Reading a Whole File at Once

    Sometimes it's easier to read a whole file into a list, especially with complex break logic, read-ahead totals, or sorting. Here's a program that reads a file and prints it in sorted order:

    Code:
    open(INFILE, "<filename.out"); # open for input
      my(@lines) = <INFILE>; # read file into list
      @lines = sort(@lines); # sort the list
      my($line);
      foreach $line (@lines) # loop through list
      {
          print "$line\n"; # print in sorted order
      }
      close(INFILE);
    File Slurping

    You might occasionally want to grab an entire file without paying attention to line termination. You can do that by undefing the $/ built in variable, and then assigning the <file> to a scalar. This is called "slurping" the file.

    The following code slurps the STDIN file, then splits it into lines, then reassembles the lines into a single string, and prints the string:

    Code:
    #!/usr/bin/perl -w
      use strict;
      
      my $recTerminator = $/;
      local $/ = undef;
      my $buf = <STDIN>;
      local $/ = $recTerminator;
      my @lines = split /$recTerminator/, $buf;
      $buf = "init";
      $buf = join $recTerminator, @lines;
      print $buf;
      print "\n";
    The preceding code works like this:


    1. First we store the terminator character, which by default on Linux systems is linefeed -- "\n".
    2. Now we undef the line terminator character
    3. Now we slurp the entirety of STDIN
    4. Now we restore the line terminator character
    5. Now we split the string we read using the termator as a border
    6. Now we join the array back into a string
    7. We print the string
    8. Last but not least, we print an extra newline to fix a picket fence condition
    Slurping isn't as handy as it might seem. If you're a C programmer accustomed to using the read() and write() functions with a large buffer to accomplish incredibly fast I/O, you might think file-at-a-time I/O would be much faster than line oriented I/O. Not in Perl! For whatever reason, line oriented is faster.

    Conclusion



    Large buffer I/O is not efficient the way it is in C. If the file is large enough to save time by whole file reads, then it's so large as to exhaust electronic RAM memory, thus incurring swap penalties.

    The most efficient algorithm reads a line, writes a line, and stores nothing. That's not always practical, and it's certainly not the easiest way to design code.

    If you really want to get faster I/O in Perl, you might experiment with the sysopen(), sysread(), sysseek(), and syswrite() functions. But beware, they interact quirkily with normal Perl I/O functions.
     
  2. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    Above is taken from http://www.troubleshooters.com/codecorn/littperl/perlfile.htm

    These are taken from http://www.tutorialspoint.com/perl/perl_files.htm

    Above is taken from http://www.troubleshooters.com/codecorn/littperl/perlfile.htm

    Above is taken from http://www.troubleshooters.com/codecorn/littperl/perlfile.htm

    This is the same as " The Moral of the Story" of http://www.troubleshooters.com/codecorn/littperl/perlfile.htm




    I did'nt expect atleast from you. Please admin see it..

    It's totally crime it's already told by debleena.....
     
  3. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    I did'nt expect atleast from... Leader
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    We have taken a note of all of them and are in discuss with the pradeep.
     
  5. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    Okay!!! this is not for only one person. This should be in rule of Go4expert. Before publicing any article it should be verified by responsible person so that in future no issues and our forum will be too much quality and following all the processes og international rules.
     
    shabbir likes this.
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    We normally have editors but we never verified about the same and for the new articles being posted will have the criteria verified. We are also working on the other articles and as into process of removing them. Thanks for your input.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    We have closed the thread and the discussion about duplicate articles will be taken in the following thread.

    Duplicate Articles Problem
     
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice