#!/usr/bin/perl use warnings; # Define the subroutine sub linenum { my @new_array; # This is not necessary in Perl but makes the variables my $n = 0; # local to avoid conflicts in the rest of the program foreach (@_) { $n++; push @new_array, "$n " . $_; } @new_array; } # Grab the file names off the command line, input file first then output file $input_file = $ARGV[0]; $output_file = $ARGV[1]; # This is a more compact and idiomatic way of opening a file (see pages 167-168) open INPUT, "<$input_file" or die "Can't open $input_file for reading\n"; @input_array = ; # This stores the entire file in memory close INPUT; # and will not be efficient with LARGE files @output_array = linenum(@input_array); # Call the subroutine open OUTPUT, ">$output_file" or die "Can't open $output_file for writing\n"; print OUTPUT @output_array; close OUTPUT; # Note that "_array" appended to the names are redundant # They can be identified as arrays with the @ symbol # Although whatever is easier to read should be used