Okay pardon me for this silly question (I am learning perl!) but how do we run a perl file that has many sub functions. For example, if a file called thisismyfile.pl has following sub functions

sub thisismyfunction1
sub thisismyfunction2

where sub thisismyfunction1 will output table 1 and sub thisismyfunction2 will output table 2.
then when I want to run this .pl file from command line, I write in terminal

perl thisismyfile.pl thisismyfunction1

but this doesn't seem to give me the result (as in table1) I want? Is this the way to run a perl file with various sub functions ?

Recommended Answers

All 7 Replies

Nope. If you want it to do something when you run it from the command line, you need to put code in the top level of the file (not in a function).

You can easily make your script behave this way, though, if you add the following code to it:

if ($ARGV[0] eq 'thisismyfunction1') {
    &thisismyfunction1;
} elsif ($ARGV[0] eq 'thisismyfunction2') {
    &thisismyfunction2;
}

It's simplistic and won't scale well, but it works if it's used as you describe. Depends on what you need.

Thanks Trentacle. I am noticing something, so if these are the functions:

sub thisismyfunction1()
sub thisismyfunction2()
sub thisismyfunction3()
sub thisismyfunction4()

If in my thisismyfile.pl I do the following:

&thisismyfunction1;
&thisismyfunction2; 

in the beginning of my script in the .pl file and then in terminal run it as only:

perl thisismyfile.pl

then it outputs the first two tables that sub 1 and sub 2 were coded to do. Any input on this or suggestion?
Thanks again!

Well, yeah. &sub_name; just calls the sub_name subroutine. In your example you define four subroutines and call two of them. What would you expect to happen?

You could write your subroutines as values in a hash in your script and then run it from the commandline with the name of your desired function as the first argument on the command line. For example, a script named many_functions.pl

#!/usr/bin/perl
use warnings;
use strict;

my %funcs = (thisismyfunction1 => sub {print 'hello world number 1',"\n";},
             thisismyfunction2 => sub {print 'hello world number 2',"\n";},
            thisismyfunction3 => sub {print 'hello world number 3',"\n";},);

my $what_to_do = $ARGV[0];

$funcs{$what_to_do}();

Test it from the command line as follows:

david@david-laptop:~/Programming/Perl$ perl many_functions.pl thisismyfunction1
hello world number 1
david@david-laptop:~/Programming/Perl$ perl many_functions.pl thisismyfunction2
hello world number 2
david@david-laptop:~/Programming/Perl$ perl many_functions.pl thisismyfunction3
hello world number 3

You may prefer to name your subroutines, especially if they may contain lots of code, and store references to the subroutines as values in a hash. The following would give the same results as the above script:

#!/usr/bin/perl
use warnings;
use strict;

my %funcs = (thisismyfunction1 => \&world_1,
             thisismyfunction2 => \&world_2,
            thisismyfunction3 => \&world_3,);

my $what_to_do = $ARGV[0];

$funcs{$what_to_do}();

sub world_1 {print 'hello world number 1',"\n";}
sub world_2 {print 'hello world number 2',"\n";}
sub world_3 {print 'hello world number 3',"\n";}

Or

no strict 'refs';
$ARGV[0]->();

(This is not actually a recommendation, and I hereby disclaim any consequences of doing this in actual code, including but not limited to odd behavior, strange errors, problems debugging, write-only code, and enraged maintenance programmers.)

I should add that experienced programmers do not require their users to memorize the real names of all their subroutines to use as command-line arguments at run-time. Make the command-line arguments short, easy to remember and spell. Use a hash or an if-elsif-else statement to associate the command-line arguments with the name of the appropriate function or subroutine. If the argument value does not correspond to any of the expected values, your script should display a warning or error message.

Thank you all for the suggestions. I am marking this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.