Top Banner

of 17

File Handling With Perl

Jun 02, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 File Handling With Perl

    1/17

  • 8/10/2019 File Handling With Perl

    2/17

    A file handle is a label that we use to tell Perlwhich file were intending to use.

    STDINof is a file handle for thespecial file standard inputthat is used to readtext from the user typing on a keyboard.

    The counterpart of STDIN is the STDOUT

    file handle which we implicitly use when wewrite print. The third file handle STDERRis where error

    messages printed by die go.

  • 8/10/2019 File Handling With Perl

    3/17

    For writing/reading from files, we need filehandles.

    File handles are usually one way, which means a

    file can be opened for either writing or reading. To open a file for reading, type:

    open FH, $filename or die $!;

    FHis the name of the file handle to the file name

    specified by $filename. The second argument canbe a literal as well, like output.log.When specifying the full path of a file on

    Windows, make sure the reverse slashes are

    either escaped or use forward slashes.

  • 8/10/2019 File Handling With Perl

    4/17

    If the file couldnt be opened for any reason,

    the die statement will work. The error

    message returned by die will be stored in thespecial variable $!.

    Printing the contents of the variable $! will letus know what error was encountered.

    die does not print a newline, so whenever youuse a die and print an error message, be sureto include \n.

  • 8/10/2019 File Handling With Perl

    5/17

  • 8/10/2019 File Handling With Perl

    6/17

    ARGV is a special file handle which opens thenames of the files provided at the time of

    running the program. If nothing is provided,ARGV reads from the standard input.

    @ARGV stores all the command linearguments and the file handle storeseach file in turn.

    Perl provides a shortcut for this: , theempty diamond.

  • 8/10/2019 File Handling With Perl

    7/17

    while(){

    print $_;}

    This time, Perl will process the file names whichwe provide at the time of running the program.

    If more than one file name is given, Perl will

    process them one by one. The name of thecurrent file is stored in the special variable$ARGV.

  • 8/10/2019 File Handling With Perl

    8/17

    Used in a scalar context, the file handlereturns only the next line; however used in alist context, it will return the entire file.my @file;

    @file = ;

  • 8/10/2019 File Handling With Perl

    9/17

    Till now, weve been reading in lines from thefiles we opened, because a line is the defaultdefinition of a record. But this default behavior

    can be changed. We can do this by changing the value of the

    special variable $/(the record separatorvariable). For example, if we set it to (emptystring), we can read in paragraphs from the textfile instead of single lines.

    To read in the entire file into a string, set thevalue of $/to undef.

  • 8/10/2019 File Handling With Perl

    10/17

    To open a file for writing, we write:open FH, > filename or die $!;

    This will either create a new file or wipe outthe contents of an existing file and start overagain. To append to an existing file, use >>instead of >.

    To write on to the file, we use:print FH ;where list is what we want written to the file.

  • 8/10/2019 File Handling With Perl

    11/17

    On text files, Perl treats special characters likenewlines differently on different operating

    systems. This processing is unnecessary forbinary files. To tell Perl to compensate for binary files, we

    use the binmodeoperator:binmode FILEHANDLE;

    Use binmodeonly with binary files, never textfiles.

  • 8/10/2019 File Handling With Perl

    12/17

    Perl gives us the function umask(expr) whichcan be used to set user permissions on a filewe create. The expression it expects is a octalnumber defining the permissions we want seton the file.

    For example, umask(0710)means oct 710

    which in binary is (111,001,000) which meansthe owner can read, write, execute the file; hisgroup can only execute, but not modify orread; and everyone else has no permissions.

  • 8/10/2019 File Handling With Perl

    13/17

    Just like standard input/output, data may be sentto other programs as well using the concept of apipe.

    A pipe in Perl connects (usually) the standardoutput of a file to the standard input of another.

    For example, we may have a Perl file sortMe.plxthat reads in a text file abc.txtand sorts it. If we

    want to send the output of sortMe.plxto anotherPerl script, say new.plx,we write:perl sortMe.plx < abc.txt | perl new.plx

  • 8/10/2019 File Handling With Perl

    14/17

    Perl provides us with file tests which allow usto check various characteristics of files before

    opening them. If we want to check whether a file exists, we

    write:if (-e somefile.txt){}

    For a list of commonly used file tests, see thenext page.

  • 8/10/2019 File Handling With Perl

    15/17

  • 8/10/2019 File Handling With Perl

    16/17

    Just as with files, Perl also allows us to readdirectories.

    A globis like a regular expression that allows us

    to read directories. In a glob, * matches anyamount of text and ? would match any onecharacter. To get all the files in a directory, wecould say @fileNames = glob(*);

    We could also read in directories just like lines ina file, however for this, we would use opendirinstead of open. Similarly to read each directory,we would use readdir.

  • 8/10/2019 File Handling With Perl

    17/17

    What do the following regular expressions mean?(1) $var =~ /(\w+)$/(2) $code !~ /^#/(3) s/#{2,}/#/g

    Write a program to read in a text file and print out thelast 5 lines in the file.

    Write a program that reads in a text file and counts outthe number of times the word we occurs in it.

    Write a program to sort the lines in a text file and print

    them out to the screen. Write a program that reads in the names of two text files

    and copies the first into the second. Visit learn.perl.org and read through the section on

    regular expressions