Passing In Parameters
Macros can have parameters passed into them. This is a two step process. First, when the macro is used it needs to pass in the parameters like this:
^Sum(12,5);
Second we, as programmers, must make use of the parameters, like this:
my ($session, $firstNumber, $secondNumber) = @_;
Here's an example macro that demonstrates the use of those parameters. The macro will accept two parameters, sum them, and return the result. So if we passed in 12,5 as parameters, we should get a result of 17.
package WebGUI::Macro::Sum;
use strict;
sub process {
my ($session, $firstNumber, $secondNumber) = @_;
return $firstNumber + $secondNumber;
}
1;
Let's take that a little further. Now let's accept N parameters and sum all of them no matter how many parameters there are. This will enable us to pass in 4,21,13,0,7,5 and get a result of 50.
package WebGUI::Macro::Sum;
use strict;
sub process {
my ($session, @values) = @_;
my $sum = 0;
foreach my $value (@values) {
$sum += $value;
}
return $sum;
}
1;