Calling subs from separate script (Perl)

From ben.goodacre.name/tech

Jump to: navigation, search

To better organise your code or to allow code to be referred to by multiple scripts it can be best to call the code, which is a sub in this example, from a separate perl script.

require "script.pl";
1;

Example

main.pl:

#!/usr/local/bin/perl

require "script.pl";

#Variables in main.pl are parsed if referenced in showresult:
my $location = "England";

my $output = &showresult("Hello",3);
print $output;

script.pl:

#!/usr/local/bin/perl

sub showresult {

my $message = $_[0];
my $number = $_[1];

return "You are located in $location, the message is $message and your lucky number is $number.\n";
1;

Output:

You are located in England, the message is Hello and your lucky number is 3.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox