Texas A&M Supercomputing Facility Texas A&M University Texas A&M Supercomputing Facility

Exercise 3 : Reading and writing files

Laboratory Exercise for Introduction to Perl

This assumes you have already set up your initial directory with the init-perllab command (instructions).


Reading from a file (cabeza.pl)

  1. Change to the Lab3 directory:

    cd /scratch/mylogin/PerlLab/Lab3

    The directory path may be different if you installed the files in another location.
  2. List the directory contents:

    ls -l

    You should see the files: INSTRUCTIONS README Solutions cabeza.pl pairs.pl
  3. Look at the cabeza.pl file:

    cat cabeza.pl

    The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):
    1. #!/usr/bin/perl
    2.  
    3. # cabeza.pl - print the first 10 lines of a given file
    4.  
    5. use warnings;
    6. use strict;
    7.  
    8. # This is the first argument on the command line
    9. my $filename = shift(@ARGV);
    10.  
    11. # Check that file exists and is readable
    12. unless (-r $filename)
    13. {
    14. die "could not read file ($filename)";
    15. }
    16.  
    17. # Open the file, read up to 10 lines or to the end of file, and
    18. # output the lines to standard out. This should operate like the
    19. # unix command "head".
  4. Edit the cabeza.pl program so it will open the file specified in the variable $filename, read up to 10 lines, and output them to standard out. For example, to print the first 10 lines of the file "INSTRUCTIONS", it will look like:
    command
    ./cabeza.pl INSTRUCTIONS

    output
    http://sc.tamu.edu/shortcourses/SC-perl/lab/ex2/
    
      Exercise 3 : Reading and writing files
    
    
        Laboratory Exercise for Introduction to Perl
    
    This assumes you have already set up your initial directory with the
    init-perllab command.
    
    

  5. For reference, read the perl documentation on open() or the tutorial on using open(). You can also view these from eos by typing:

    perldoc -f open
    man perlopentut

SOLUTION


Writing to a file (pairs.pl)

  1. Look at the pairs.pl file:

    cat pairs.pl

    The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):
    1. #!/usr/bin/perl
    2.  
    3. # pairs.pl - save a list of numeric pairs to a file
    4. #
    5. # Write to the file "squares.dat" a list. Each line
    6. # will contain a number (i) and the square of that number.
    7. # The starting and ending number will be specified as user
    8. # input.
    9.  
    10. use warnings;
    11. use strict;
    12.  
    13. my $out_filename = 'squares.dat';
    14.  
    15. my ($start, $end);
    16.  
    17. print "Enter starting value: ";
    18. $start = <>;
    19.  
    20. print "Enter ending value: ";
    21. $end = <>;
    22.  
    23. # check that $start <= $end
    24.  
    25. # open $out_filename for writing
    26.  
    27. # make a loop ($i) from $start to $end
    28. # write $i and $i*$i to the file
    29.  
    30. # close the file
  2. Edit the program so it will open the file "squares.dat" and write a line for each value from the value in $start to the value in $end. On each line, print the value and the square of the value. For example, an input of 3 for start and 11 for end would result in the file "squares.dat" containing:
    output
      3   9
      4  16
      5  25
      6  36
      7  49
      8  64
      9  81
     10 100
     11 121
    

  3. For reference, read the perl documentation on open(), printf(), and die(). You can also view these from eos by typing:

    perldoc -f open
    perldoc -f printf
    perldoc -f die

SOLUTION