in reply to Several stupid questions about subs
what you get printed out is the word 'bar'. if you want to pass variables to a sub there are various ways;my $var = foo(); sub foo { return 'bar'; } print $var;
As far as I understand it $_[0], represents the first element of an array sent to the sub... so if you can do stuff like this;my $var = foo('some_text_for_instance'); sub foo { my $text = $_[0]; return 'bar'; }
You can also capture the whole passed array;my $var = foo($somevar,"some text",$somehashref); sub foo { my $variable = $_[0]; my $text = $_[1]; my $hashref = $_[2]; return $hashref->{'some_key'}; }
There is also my $var = shift; but I'm not sure what that is or does or why one would want to use it.sub foo { my @private_local_array = @_; #then lets return element 5 for some reason return $private_local_array[4]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Several stupid questions about subs
by Anonymous Monk on Apr 13, 2011 at 17:28 UTC | |
by ikegami (Patriarch) on Apr 13, 2011 at 17:48 UTC | |
by fidesachates (Monk) on Apr 13, 2011 at 17:56 UTC | |
by Anonymous Monk on Apr 13, 2011 at 18:56 UTC |