Home Scripting Perl Tutorial File Input
Main Menu
System Verilog
Open Vera
Digital Concepts
Verification Basics
    - Perl Tutorial
        -- Perl Definition
        -- Syntax & Variables
        -- strings
        -- Arrays
        -- Associative Arrays
        -- If & While Loop
        -- File Input
        -- Print Output
        -- Strings and Regular Expressions
        -- Subroutines
        -- Running External Programs
        -- References
        -- Terse Perl
    - Bash
    - Makefile Tutorial
    - Perl Examples
    - Bash Examples
Interview Questions
Computer Architechture
C and C++
AsicGuru Blog
Tags Cloud
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Perl : File INput
Share This Articale:

File Input
Variables which represent files are called "file handles", and they are handled differently from other variables. They do not begin with any special character -- they are just plain words. By convention, file handle variables are written in all upper case, like FILE_OUT or SOCK. The file handles are all in a global namespace, so you cannot allocate them locally like other variables. File handles can be passed from one routine to another like strings (detailed below).

The standard file handles STDIN, STDOUT, and STDERR are automatically opened before the program runs. Surrounding a file handle with <> is an expression that returns one line from the file including the "\n" character, so returns one line from standard input.

The <>
operator returns undef when there is no more input. The "chop" operator removes the last character from a string, so it can be used just after an input operation to remove the trailing "\n".
The "chomp" operator is similar, but only removes the character if it is the end-of-line character.

$line = ;        ## read one line from the STDIN file handle
chomp($line);             ## remove the trailing "\n" if present
$line2 = ;      ## read one line from the FILE2 file handle
                                 ## which must be have been opened previously
Since the input operator returns undef at the end of the file, the standard pattern to read all the lines in a file is...

## read every line of a file
while ($line = ) {
    ## do something with $line
Open and Close
The "open" and "close" operators operate as in C to connect a file handle to a filename in the file system.
open(F1, "filename");                 ## open "filename" for reading as file handle
open(F2, ">filename");                ## open "filename" for writing as file handle
open(F3, ">>appendtome")        ## open "appendtome" for appending
close(F1);                                   ## close a file handle
Open can also be used to establish a reading or writing connection to a separate process launched by the OS. This works best on Unix.

open(F4, "ls -l |");                       ## open a pipe to read from an ls process
open(F5, "| mail $addr");             ## open a pipe to write to a mail process

Passing commands to the shell to launch an OS process in this way can be very convenient, but it's also a famous source of security problems in CGI programs. When writing a CGI, do not pass a string from the client side as a filename in a call to open().

Open returns undef on failure, so the following phrase is often to exit if a file can't be opened. The die operator prints an error message and terminates the program.

open(FILE, $fname) || die "Could not open $fname\n";

In this example, the logical-or operator || essentially builds an if statement, since it only
evaluates the second expression if the first if false. This construct is a little strange, but it is a common code pattern for Perl error handling. Input Variants In a scalar context the input operator reads one line at a time. In an array context, the input operator reads the entire file into memory as an array of its lines...

@a = ;         ## read the whole file in as an array of lines

This syntax can be dangerous. The following statement looks like it reads just a single line, but actually the left hand side is an array context, so it reads the whole file and then discards all but the first line....

my($line) = ;

The behavior of also depends on the special global variable $/ which is the current the end-of-line marker (usually "\n"). Setting $/ to undef causes to read the whole file into a single string.

$/ = undef;
$all = ;            ## read the whole file into one string

You can remember that $/ is the end-of-line marker because "/" is used to designate separate lines of poetry. I thought this mnemonic was silly when I first saw it, but sure enough, I now remember that $/ is the end-of-line marker.

This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start

Prev << If & While Loop

Next >> Print Output

Sign In
Login with :-
| | |  
  • Bookmark