Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling functions from array variables 1

Status
Not open for further replies.

audiopro

Programmer
Joined
Apr 1, 2004
Messages
3,165
Location
GB
I have been searching but cannot find any reference to calling functions directly from a name array list.
[code}
@vars=("subrout1","subrout2","subrout3");
[/code]
In the above code, what would be the syntax to call a sub routine named $vars[1]?

Keith
 
You are trying to use the array as an array of soft references.
This will fail under "use strict;". Do you really need to do that or could you use hard references?


Trojan.
 
If I tell you the scenario, maybe you can suggest an alternative approach.
I have an incoming array which contains a series of numbers.
These control how the incoming variables are calculated within the script.
My approach was to run a seperate sub routine for each variation but I am unable to run sub's like that.
I am sure there must be a simple way to do this.

Keith
 
use a hash instead?

Code:
my %coms = (
  sub1 => \&sub1,
  sub2 => \&sub2,
  sub3 => \&sub3
);

post the code you are working with, or the relevant part of the code, to show how you are using this array with the numbers to control the calculations you are running.
 
In it's simplest form
Code:
my $code = $_[1];
if($code eq "A"){
   &function1();
}elsif($code eq "B"){
   &function2();
}elsif($code eq "C"){
   &function3();
etc .......
The conditional statements work ok and the script does what it says on the tin but I am trying to condense it as much as possible.

The sub routine names would be stored in a hash and then any sub routine could be called referenced by the incoming var.
But how would I call the subroutine?

Keith
 
This is the script I am working with.
Code:
my %coms=(
 A => \&lower_mark_up,
 B => \&standard_mark_up,
 C => \&high_mark_up
);

$coms{B}->();
$coms{C}->();
$coms{A}->();

sub lower_mark_up{
print "1";
}
sub standard_mark_up{
print "2";
}
sub high_mark_up{
print "3";
}
It works and prints our 231 as expected but I do not fully understand how it works.
I understand how the hash is populated with references to the array names but could you please explain the line
Code:
$coms{B}->();

Keith
 
the hash is not populated with references to array names, but references to code (or sub routines). Like all references in perl, you have to use a syntax that lets perl know it's a reference, that's what the ->() is for: a reference to a sub rotuine. You can also code it like this too:

&{$coms{A}}();

but the other way looks more readable to me.
 
Thanks Kevin
Still not sure how it works but it does.
Could you point me at a good tutorial as regards references, all the books and articles I have read appear to have over looked this useful process.

Keith
 
Try googling for "perlreftut" too (and, to a lesser extent "perllol", which deals with using references to create multidimensional data structures).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top