J
Jan Fure
Hi;
I need to be able to select which subroutine to process a string with
based on the contents of the string.
I have written a code example of this, that uses a lookup table.
Is there a sensible way to do this without the lookup table, i.e., can
I let the parsed string value be the subroutine name?
Jan
#!/usr/local/bin/perl -w
use strict;
my @DATA = <DATA>;
foreach (@DATA) {
chomp;
my @ARGUMENTS = split / /;
my $output = subselect(@ARGUMENTS);
print "The chained output of 2 subroutines is $output\n";
}
sub subselect {
if ($_[0] eq 'sr2') {
my $return_value = sr2(@_);
return $return_value;
}
if ($_[0] eq 'sr3') {
my $return_value = sr3(@_);
return $return_value;
}
}
sub sr2 {
my $x2 = $_[1] ** 2;
return $x2;
}
sub sr3 {
my $x3 = $_[1] ** 3;
return $x3;
}
__DATA__
sr2 3.456
sr3 2
sr2 45
I need to be able to select which subroutine to process a string with
based on the contents of the string.
I have written a code example of this, that uses a lookup table.
Is there a sensible way to do this without the lookup table, i.e., can
I let the parsed string value be the subroutine name?
Jan
#!/usr/local/bin/perl -w
use strict;
my @DATA = <DATA>;
foreach (@DATA) {
chomp;
my @ARGUMENTS = split / /;
my $output = subselect(@ARGUMENTS);
print "The chained output of 2 subroutines is $output\n";
}
sub subselect {
if ($_[0] eq 'sr2') {
my $return_value = sr2(@_);
return $return_value;
}
if ($_[0] eq 'sr3') {
my $return_value = sr3(@_);
return $return_value;
}
}
sub sr2 {
my $x2 = $_[1] ** 2;
return $x2;
}
sub sr3 {
my $x3 = $_[1] ** 3;
return $x3;
}
__DATA__
sr2 3.456
sr3 2
sr2 45