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]; }
In reply to Re: Several stupid questions about subs
by simonodell
in thread Several stupid questions about subs
by bryan172
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |