Mug am Mittwoch, 25. Oktober 2006 13:12:
> Hi all,

Hello Mug

I'm not shure if I understand your question...

> I don't know if that anyway I can know what should I pass back
> from right hand side to left hand side, like :
>
> my $x = qw/a b c d e / ; # so I have $x = 5

The list on the right hand side is evaluated in scalar context, and that 
delivers the number of entries in the list.

> my ($x) = qw / a b c d e / ; # then I have 'a'

Here, the parenthesis provide list context to the right hand side. Perl tries 
to assign the nth list entry to the nth variable (in the left hand side 
list). Since the left hand side can only hold one value, only the first list 
item is assigned, and the following are discarded.

> or like
>
> $data = <F> ; # reads 1 line from file;

Scalar context => assign one line. <F> behaves different than a list in scalar 
context (does not result in the number of file lines). Consider:

my @array=(1,2,3,4);
my %hash =(1,2,3,4);
my @new=%hash;
my [EMAIL PROTECTED];

> @data = <F> ; # reads all line from the file

list context => assign all lines

> I actually want to write a piece of code like that :
>
> my %u_info = user_detail ( $ENV{QUERY_STRING} );
> # I have $u_info{id} = 'foo' , $u_info{pass} = 12345
>
> my @attribs = user_detail ( $ENV{QUERY_STRING} );
> # I have @attribs = ( 'foo', 12345 );
>
> while I am asking %u_info , the user_detail will return a hash ,
> and while I am asking @attribs, I got an array return.

Simply return a list, or an array, from the function. From the perspective of 
the function, it does not matter if the list is assigned to an array or a 
hash (provided an even number of elements).

But there is - in contrast - a way to find out from a subroutine if it's 
called in scalar or list context:

  return wantarray ? @list : $scalar;


Hope this helps 

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to