sub getPlatformFiles { my $plat_in = shift; my $nic = shift; my @list;
A style I prefer when not using OO-style programming is to use an list assignment rather than a shift, but this is wholly subjective:
sub getPlatformFiles { my ($plat_in, $nic) = @_; my @list;
There are some potential complexities involved in scoping here, which may bite you if you are incautious and use the same variable names in a script and a subroutine. Note the following is for demonstration purposes and using this approach without good reason can result in some painful bug hunting. If you declare a variable with my in scoping using that contains a subroutine, that subroutine can see all variables in that scope, and hence the following works:
#!/usr/bin/perl use strict; use warnings; my $variable = "Hello\n"; outputter(); sub outputter { print $variable; }
This is because the scope of $variable is at the script level and contains the subroutine definition. On the other hand, the following will output an error message:
because the variable is not declared prior to its use in the subroutine. These behaviors allow for some very powerful structures called closures.#!/usr/bin/perl use strict; use warnings; sub outputter { print $variable; } my $variable = "Hello\n"; outputter();
In reply to Re: Another scoping issue: How do I send 2 variables to my sub and use both.
by kennethk
in thread Another scoping issue: How do I send 2 variables to my sub and use both.
by MikeDexter
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |