Re: Perl : Question About Performing Subroutines

  • From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 8 Dec 2008 20:11:58 +0200

Hi,

Yes you can create a binary executable for Windows or Linux, using ActiveState's Perl Developer kit which is not free, but it is the best, or you can use the free tool PAR from cpan.

And yes you can do the second thing, like in the following example:

use strict;

my %hash = (
f1 => 'function1',
f2 => 'function2',
);

#Get the wanted key as the first parameter of the program:
my $current_function = shift or die "Specify the function key";

no strict 'refs';
&{$hash{$current_function}};

sub function1 {print "I am 1\n"}
sub function2 {print "I am 2\n"}

In order to run the program you will need to type something like:

perl program.pl f1

and it will execute the function1.

Or you can use instead a shorter variant that doesn't use a hash of values:

use strict;

my $current_function = shift or die "Specify the function key";

no strict 'refs';
&{$current_function};

sub function1 {print "I am 1\n"}
sub function2 {print "I am 2\n"}

In order to run the program, you will need to use something like:

perl program.pl function1

and it will execute the function1 subroutine.

Of course, you can get the content of the variable current_function from somewhere else.

Octavian

----- Original Message ----- From: <james.homme@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, December 08, 2008 6:13 PM
Subject: Perl : Question About Performing Subroutines


Hi,
The first thing I want to know is this. Is there a way to make a
self-executable Perl program that I can just give to someone and they can
run it without needing to install the Perl interpreter? The second thing
is this. Can I do this. Can I have a hash whose keys are a set of values
and whose values are the functions in my program I want to perform. For
example, I don't want to say if key is this, perform this function, elseif
key is this, perform that function. I simply want to look up a key and
perform whatever function is in the value for that entry in the hash. Can
I do this? If so, how?

Thanks.

Jim

James D Homme, Usability Engineering, Highmark Inc.,
james.homme@xxxxxxxxxxxx, 412-544-1810

"The difference between those who get what they wish for and those who
don't is action. Therefore, every action you take is a complete
success,regardless of the results." -- Jerrold Mundis
Highmark internal only: For usability and accessibility:
http://highwire.highmark.com/sites/iwov/hwt093/

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: