
Let's say you have values that you want to pass into a subroutine. This is how you would pass two values, "red" and "apple" into a subroutine named "fruit".
fruit("red", "apple");
In the fruit subroutine, you would then use @_ or $_ or shift to access the values passed into the subroutine.
In this example, $var will contain "red" the first time the subroutine is called, and then contain "apple" the next time the subroutine is called.
sub fruit{
my $var = shift;
print "$var \n";
}
In this example, $color will contain the first value passed into the subroutine, "red". $type will contain the second value passed into the subroutine, "apple". Notice that "my" precedes $color and $type, which means that the $color and $type variables are scoped in the "fruit" subroutine. Since $color and $type are scoped in the "fruit" subroutine, you can use $color and $type inside of the "fruit" subroutine. In this example, we simply print $color and $type inside of the "fruit" subroutine.
sub fruit{
my ($color, $type) = @_;
print "$color $type \n";
}
The following will produce the same exact results, using $_ instead.
sub fruit{
my $color = $_[0];
my $type = $_[1];
print "$color $type \n";
}
Accessing variables outside of the subroutine
Attempting to use $color or $type outside of the "fruit" subroutine will return Global symbol "$color" requires explicit package name. This is because the $color and $type variables were defined inside of the "fruit" subroutine.
sub fruit{
my ($color, $type) = @_;
}
print "$color $type \n";
To access a variable outside of it's subroutine, you will just need to define the variable outside of it's subroutine. In this example, $color and $type are defined outside of the "fruit" subroutine. Also, you would not use "my" when associating $color and $type from @_ inside of the subroutine.
my $color = "";
my $type = "";
fruit("red", "apple");
sub fruit{
($color, $type) = @_;
}
print "$color $type \n";
Passing in variables
Let's say you've already defined $color and $type somewhere outside of the fruit subroutine.
my $color = "red";
my $type = "apple";
In this scenario, there is no need to pass $color and $type into the subroutine.
fruit();
Since $color and $type have already been defined, $color and $type can be used within the subroutine without passing $color and $type into the subroutine.
sub fruit{
print "$color $type \n";
}
Passing in two (or more) arrays
Let's say you've an array of colors and an array of fruit, like this.
my @colors = qw(red orange yellow green);
my @fruits = qw(apple orange banana grape);
When passing the arrays into the subroutine, you will need to escape the @ character.
fruit(\@colors, \@fruits);
You can then use each array in the subroutine like this.
sub fruit{
my ($colors, $fruits) = @_;
my @colors = @{$colors};
my @fruits = @{$fruits};
foreach my $color (@colors) { print "$color \n"; }
foreach my $fruit (@fruits) { print "$fruit \n"; }
}
Did you find this article helpful?
If so, consider buying me a coffee over at