Akhthar Parvez K wrote:
On Thursday 29 Apr 2010, Shlomi Fish wrote:
Why are you assigning to variables inside @_? It's almost always a bad idea.
@_ is the function parameters' list. You should read the values from there
(using "my ($param1, $param2, $param3) = @_;" or "my $param1 = shift;" (short
for "shift(@_)")) and then possibly mutate them if they are references and
return values from the function using return.
Well, I know that @_ is a list
No, anything with '@' in front of it is either an array or a slice.
with arguments passed to the function.
Subroutines in Perl are a bit more complicated than that.
perldoc perlsub
But I thought it also stores the "return" values from a subroutine.
No, the return values are not stored by perl anywhere.
eg:- If Function1 returns "abc" and "pqr" and I want to catch only the
first return value ("abc" in this case), I could use the following line:
my ( $name ) = ( $_[0] ) = Function1( $arg ); (Thanks to John and Shawn
for correcting me with that)
Anything wrong with this method? If so, what's the correct method then?
The subroutine returns a list (in list context) so you need to force
list context in the assignment and store the member of the list that you
require:
my ( $name ) = Function1( $arg );
Perhaps if you want to catch only the second value:
my ( undef, $name ) = Function1( $arg );
Or:
my $name = ( Function1( $arg ) )[ 1 ];
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/