More...

Perl File I/O
STDIN

STDIN is used to ask for input from the keyboard. The important to keep in mind, is that the entry ends with a newline value (\n) that normally has to be removed from the variable. This can be accomplished with chomp. The other feature is that any input in Perl that uses '<>' (referred to as the diamond operator)  is automatically copied to the Perl variable '$_'.

You can use the 'defined' command to determine if any entry has been made. This is useful in a program loop.

while (<STDIN>) {
    chomp; # this is the same as using chomp($_)
    @all_lines . $_;
}

The following example is a Perl program that accepts filenames as parameters and outputs the contents of each file.

while (<>) {
    print $_;
}

If the program was named 'printfiles.pl', you would enter the following to print the contents of file1 and file2.

printfiles.pl file1 file2

top of page


STDOUT, print, printf

The command 'print' is used to write to standard output. The print command is actually a function that takes a list of arguements and returns a value, just like any other function.

print ((2+3), "hello");
# prints '5hello'

You can use the printf command if you are familiar with this C command.

top of page


File handles - Open, Close

A filehandle is a variable name assigned to a disk file name. It is recommended that filehandlers are always entered in only uppercase. Again, be careful not to assign a name that is a protected perl name.

Examples to open a file...

open( CUSTOMER, "/usr/data/customer" );
# opens the file for reading
open( OUT, ">new_or_existing file" );
# opens for write & will create a new file if it doesn't exist
open( LOG, ">>/usr/logfile");
# this will open for write and append data

Example to close file...

close(CUSTOMER);

top of page


Die

The die function is used to provide an error message if the open is not successful. The variable '$!' contains the error string. If the die statement ends with newline '\n', it prevents the perl program name and line from printing.

open(LOG, ">>logfile") || die "Can't append to logfile, error = $!\n";

top of page


Using File handles

Once a file is open, you can read or write to it just like using <STDIN> or <STDOUT>

The following reads the all of the lines from the file and prints each line.

open (EP, "/etc/passwd");
while (<EP>) {
    chomp;
    print "password line = $_\n";
}

The following reads from IN file and writes contents to OUT file.

open(IN, "/usr/file1") || die "can't open file, error = $!\n";
open(OUT, "/usr/newfile") || die "Can't create new file, error = $!\n";
while (<IN>) {
    print OUT $_;
}
close(IN);
close(OUT);

top of page


File Tests

Sometimes before opening a file, you may want to validate it does or doesn't exist or check on the type of file. The syntax for performing this is a "-" sign plus a single test character plus the disk file name.   Example: -e "/etc/password"  would evaluate as true if the file existed. The following chart defines all of the test characters.

Test character Meaning (for file or directory) Test character Meaning (for type specified)
-r is readable -f plain file
-R is readable by real user, not effective user -d directory
-w is writable -T text file
-W is writable by real user, not effective user -b binary file
-e exists -z file exists and has zero bytes
-s exists and has nonzero size -l symbolic link
-x is executable -s socket
-X is executable by real user, not effective user -p named pipe
-o is owned by user -c character special
-O is owned by real user, not effective user -t isatty() is true
-k sticky bit is set -M modifcation age in days
-u setuid -A Access age in days
-g setgid -C Inode modification in days

top of page


Format - Defining

The format command is a writing template to format the printed output.It consists of 3 steps.

  • Define the format (usually set once)
  • Loading the data into the variable portions of the format
  • Invoking the format.

The define step is similiar to creating a subrouting. Note that it ends with a single period. Here is an example...

format MYFORMAT =
fieldline
value1, value2, value3
fieldline
value1, value2

Each fieldline may contain literal text or fieldholder '@'used to insert a scarlar variable. The characters after the @ symbol control the number of characters to print and how to justify.

Character What it does
< left hand justify
> right hand justify
| center
#.# for numeric values
* Any number of characters
format PHONES =
Phone Number List
Name: @<<<<<<<<<<<<<<<<<<<<<< Phone number: @>>>>>>>>>>>>
$name, $phone

Here is an example that allows for multiple lines of text associated with a single line of text. Instead of the '@' symbol.

The '^' is used to signify multiline variable.
The '~~' at the beginning line suppressions the output if there is no more text to print or repeats the format as many times as necessary to print all data for the $comment variable.

format PEOPLE =
Name: @<<<<<<<<<<<<<<<<<<<<<  Comment: ^<<<<<<<<<<<<<<<<<<<<<<<<
$name, $comment
~~                                  ^<<<<<<<<<<<<<<<<<<<<<<<<
$comment

To prevent a long report from continously printing, you need to create another format routine where the name is the same as the detail section and "_TOP" is appended.  The variable '$%' can be used to print the current page number. The default length is 60 lines per page and is stored in the variable '$='.  The following is an example of a top of form format and changes the default number of lines to 55.

$= = 30;
format PHONES_TOP =
Phone Number List - Page @<<
$%.

top of page


Format - Using

The format statement is involved with a write function where the expression for the write statement is the format name. The following example reads a file named '/usr/source' with the following content...

Roger Petersen:630-941-9332
Scott Petersen:630-941-9338

Using the above format PHONES, the following will read the 'source' file and create a new formatted file '/usr/phones.txt'.

open(PHONES, ">/usr/phones.txt") || die "Can't create new file, error = $!\n";
open(IN,"/usr/source") || die "Can't find file, error = $!\n";
while (<IN>) {
    chomp;
    ($name, $phone) = split(/:/);
    write (PHONES);
}

top of page


Change directories - chdir

The function chdir() is used to change to a different directory. The success of the change can be tested with die.

chdir("/etc") || die "Can't move to /etc, error = $!\n";

top of page


Globbing

To list all files in a directory, the combination of putting the path name in '< >'s and adding an '*' does the trick. You can also use the glob function in place of the '< >'s.

@a = </etc*>;
@a = glob("/etc*");

top of page


Removing a file

To remove a file, the function unlink is used.

unlink ("/usr/filename");
unlink <*.txt>; # remove all files ending with txt

top of page


Renaming a file

The rename function is used to change the name of a file.

rename( $old, $new);

top of page


Linking files

To link a new file name to an existing file name, use the link function. To create a symbolic link, the function symlink is used.

link( $existing_file, $another_filename);
symlink( $existing_file, $another_filename);

top of page


Change file permissions

The chmod function is used to the permissions of a file or directory.

chmod(0111, "myfile");

top of page


Create/Delete directories

The functions mkdir or rmdir are used to create or delete directories. The permission attributes can also be set when using the mkdir function.

mkdir("newdirectory", 0777);
rmdir("newdirectory");

top of page


Change ownership

To change the owner and/or user group for a file, the chmod function is used. The UID and GID must be the actual numeric values.  If you don't want to change both the UID and GID, use a '-1' for the UID or GID.

chmod(123, 2, "filename");
# UID is set to 123, GID is to 2 for the file "filename"

top of page

Email us at Info@AHinc.com
© Advanced Horizons, Inc. All Rights Reserved