2

I'm learning Perl and I have 2 examples on how to get the same task completed, I'm just confused about the way the script is written.

script #1

#!/usr/bin/perl
use strict;
use warnings;
use IO::File;

my $filename = "linesfile.txt"; # the name of the file

# open the file - with simple error reporting

my $fh = IO::File->new( $filename, "r" );
if(! $fh) {
print("Cannot open $filename ($!)\n");
exit;
}

# count the lines
my $count = 0;
while( $fh->getline ) {
$count++;
}

# close and print
$fh->close;
print("There are $count lines in $filename\n");

Script #2

#!/usr/bin/perl
#
use strict;
use warnings;
use IO::File;

main(@ARGV);

# entry point
sub main
  {
     my $filename = shift || "linesfile.txt";
     my $count = countlines( $filename );
     message("There are $count lines in $filename");
  }

# countlines ( filename ) - count the lines in a file
# returns the number of lines
sub countlines
{
my $filename = shift;
error("countlines: missing filename") unless $filename;

# open the file
my $fh = IO::File->new( $filename, "r" ) or
    error("Cannot open $filename ($!)\n");

# count the lines
my $count = 0;
$count++ while( $fh->getline );

# return the result
return $count;    
}

# message ( string ) - display a message terminated with a newline
sub message
{
  my $m = shift or return;
  print("$m\n");
}

# error ( string ) - display an error message and exit
sub error
{
  my $e = shift || 'unkown error';
  print("$0: $e\n");
  exit 0;
}

From Script #2 I don't understand the purpose behind the following

  1. main(@ARGV);
  2. why do we need sub message and sub error?

Thanks

3 Answers 3

3

The purpose is splitting up the code in terms of responsibilities, to make it more readable, and therefore easier to maintain and extend in the future. The main function is intended as the obvious entry point, the message is responsible for writing a message to the screen, and error for writing an error message and terminating the program.

In this case splitting a simple program to subroutines doesn't buy much, but the 2nd script is likely meant as a teaching tool to show on a simple example how a program can be structured.

Sign up to request clarification or add additional context in comments.

Comments

3

The @ARGV array is the list of arguments that are supplied to the script. For example when calling a script like this:

./script.pl one two three

The @ARGV array would contain ( "one", "two", "three" ).

The sub message and sub error subroutines are used to display information to the user. For example:

message("There are $count lines in $filename");
error("Cannot open $filename ($!)\n");

The two lines above call those subroutines. It is just a slightly nicer way of informing the user, as consistent adjustments to the output can be made (such as adding a timestamp to the message or writing it to a file).

Comments

3

The line main(@ARGV); is calling the main subroutine with parameters,which are the program's arguments. The other subroutines are used in order to have reusable code.There will be many times that you 'll need to write the same code so creating a subroutine that contains this code is very useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.