28 Feb
Posted by rdash as Perl tips, Programming
This tutorial is a variation of the last one. As before, we read in lines of a data file and print them out with line numbers. The difference is that in this version, we specify the filename in the commandline parameters instead of redirecting standard input. (Feel free to compare the output of this script against that of the last one.) Once again, minimal error-checking is performed.
#!/usr/bin/perl
######################
#
# FILE: readfile02.pl
#
######################
#
# Read all the lines of a named input data file
# and print each line out with a line number.
#
use strict; # Declare strict checking on variable names, etc.
my $argc; # Declare variable $argc. This represents
# the number of commandline parameters entered.
my $lnum; # Initialize line counter
my $fname; # Declare file name variable
# Assign the number of commadline parameters to $argc
# Check the number
#
$argc = @ARGV;
if ($argc != 1)
{
# If the number of commandline parameters is not 1,
# print an usage message.
#
usage(); # Call subroutine usage()
exit(); # When usage() has completed execution,
# exit the program.
}
# You could use an else clause here, but it's not necessary
#
# Get the pathname and filename.
# Note: first item in a Perl array is at index 0, not 1
#
$fname = $ARGV[0];
# Open the file for reading, and assign arbitrary filehandle, FH.
# If the file does not exist or is otherwise unopenable, let the
# program stop gracefully with a message. (Note: some installations
# of Perl do not properly support the die() function).
#
open(FH, "<$fname") || die("Couldn't open file $fname\n");
#
# While there are lines to read from the data file, loop
#
while (<FH>)
{
# The <FH> operator assigns the next line of data
# from standard input to the Perl system variable $_.
#
$lnum++; # Increment the line counter
chomp; # Remove the 'newline' character, '\n', from $_
print "$lnum: $_\n"; # Print the line in brackets
}
exit(0); # Exit gracefully
sub usage
{
# Display the usage of this script.
# The first parameter is the filename,
# including any absolute or relative path
#
print "Usage: perl readfile02.pl inputfilename\n";
}
This is #4 in a series of Perl scripts. Please feel free to leave comments if there is something that you would like to see. I cannot guarantee that I’ll be able to answer immediately, but I will answer as soon as I can.
Technorati Tags: webguru, perl-tips, perl scripts, web programming
2 Responses
Randal L. Schwartz
March 3rd, 2006 at 11:54 am
1Again, rather than doing this the long way, your program is equivalent to:
while () {
print “$.: $_”;
}
You’re really teaching backwards-sidewards Perl style here. I’m not just trying to make it “shorter”. I’m tryiing to show you “native Perl”, not some broken combination of things you think you know.
rdash
March 3rd, 2006 at 2:26 pm
2Agreed. As I’ve said in response to your other comments , I was trying to ease people to new Perl into all the system vars. But the net result is that I’ve covered too many concepts into one intro tutorial and confused matters. I need to put a bit more time into these tutorials. Thanks again for the comments.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar