in reply to Re^2: Is it possible to call a package::main subroutine from a module?
in thread Is it possible to call a package::main subroutine from a module?
It's not always a Bad Thing to use $_[0] directly, but there are several really good reasons not to. I'll let Perl::Critic::Policy::Subroutines::RequireArgUnpacking explain:
Subroutines that use @_ directly instead of unpacking the arguments to local variables first have two major problems. First, they are very hard to read. If you're going to refer to your variables by number instead of by name, you may as well be writing assembler code! Second, @_ contains aliases to the original variables! If you modify the contents of a @_ entry, then you are modifying the variable outside of your subroutine. For example:
sub print_local_var_plus_one { my ($var) = @_; print ++$var; } sub print_var_plus_one { print ++$_[0]; } my $x = 2; print_local_var_plus_one($x); # prints "3", $x is still 2 print_var_plus_one($x); # prints "3", $x is now 3 ! print $x; # prints "3"This is spooky action-at-a-distance and is very hard to debug if it's not intentional and well-documented (like chop or chomp).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Is it possible to call a package::main subroutine from a module?
by TorontoJim (Beadle) on Mar 22, 2016 at 16:25 UTC |