To expand on what MeowChow wrote, I have no objection if the current @_ mechanism is also kept.
I like Perl's flexibility and here are some of the things you can do with the current parameter passing mechanism:
my $v="toto"; change( $v); print $v; sub change { $_[0]="tata"; }
prints tata as $_[0] is aliased to $v. You can
actually even write the function
sub change { $sv=\$_[0]; $$sv="tata"; }
sub my_print { if(UNIVERSAL::isa($_[0], 'GLOB' )) { my( $fh, $text)= @_; print $fh "my", $text; } else { print "my", $text; } }
Here if( UNIVERSAL::isa($_[0], 'GLOB') returns true if the first argument to the function is a filehandle stored in a GLOB or a sub-classed GLOB (a trick I found on PM BTW, thanks tye), so I can use the function as my_print( "toto"); or as my_print( \*FH, "tata");
#!/bin/perl -w use strict; my $s="toto"; my $n="3"; append( $s, "one"); append( $n, 1); print "s: $s n: $n\n"; sub append { if( $_[1]=~ /^\d+$/) { &append_nb; } else { &append_string; } } sub append_string { $_[0] .= $_[1]; } sub append_nb { $_[0] += $_[1]; }
outputs: s: totoone n: 4
Here &append_nb calls the function with the current @_, whatever is it, which is still aliased to the original variables, and thus still modifies it.
I'll let you figure out how &append_string is different from goto &append_string
In reply to Re: sub ($self,$var,@arr) {}
by mirod
in thread sub ($self,$var,@arr) {}
by raptor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |