Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
sub addition {
# do sum using vars from @_
my $result = $_[0] + $_[1];
# return result
$result;
}
# Call function passing in vars
my $result = &addition(3,6);
# print result to screen
print $result;
my %operations = (
addition => \&addition,
subtractions => \&subtraction,
multiplication => \& multiplication,
division => \&division
);
my $action = 'addition'; # change this as necessary
if ( $operations{ $action } ) {
$operations{ $action }->( 3, 6 );
}
else {
print STDERR "Invalid operation\n";
}
$result = "addition";
$result(3,6);
# function
sub addition{
my ($a, $b) = @_;
return ($a+$b);
}
$a = 1; $b = 2;
# normal call
print "\$a=$a, \$b=$b, \&addition($a,$b)=", &addition($a,$b), "\n";
# reference to the function by name
$foo='addition';
print "\$a=$a, \$b=$b, \$foo=$foo, \&\$foo($a,$b)=", &$foo($a,$b), "\n";
$operations{ $action }->( 3, 6 );
Software error:
Can't use string ("addition") as a subroutine ref while "strict refs" in use at \cgi-bin\admin\test.cgi line 43.
# function
sub addition{
my ($a, $b) = @_;
return ($a+$b);
}
$a = 1; $b = 2;
my $foo='addition';
my $bar=&$foo($a,$b);
print "\$bar=$bar\n";
use strict;
$retval = $operation{ $action };
$sub_ref->();
&$sub_ref;
# you'll need curly brackets if it's in a hash:
&{ $hash{sub_ref} };