Home > Archive > PERL Beginners > October 2006 > Is that I can do something like that ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is that I can do something like that ?
|
|
|
| Hi all,
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
my ($x) = qw / a b c d e / ; # then I have 'a'
or like
$data = <F> ; # reads 1 line from file;
@data = <F> ; # reads all line from the file
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.
Just note, I don't want to pass / ask reference.
Any pointers ? Thanks!
regards,
mug
| |
| Adriano Rodrigues 2006-10-30, 7:03 pm |
| On 10/25/06, Mug <perl@reborn.org> wrote:
> 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.
>
It cannot be done with the bare Perl interpreter, which only
recognizes scalar, list and void context. In your case, both %u_info =
and @attribs = call for a list context and that's all.
But you may try the CPAN module Want.
http://search.cpan.org/dist/Want
| |
| D. Bolliger 2006-10-30, 7:03 pm |
| 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 %other=@array;
> @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
| |
| Mumia W. 2006-10-30, 7:03 pm |
| On 10/25/2006 06:12 AM, Mug wrote:
> Hi all,
>
> 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
> my ($x) = qw / a b c d e / ; # then I have 'a'
>
> or like
>
> $data = <F> ; # reads 1 line from file;
> @data = <F> ; # reads all line from the file
>
> 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.
>
> Just note, I don't want to pass / ask reference.
>
> Any pointers ? Thanks!
>
>
> regards,
> mug
>
>
>
>
Let your user_detail subroutine build up a hash within itself and return
that hash.
sub user_detail {
my %hash;
... stuff ...
$hash{id} = '...';
$hash{pass} = '....';
return %hash;
}
| |
| Adriano Rodrigues 2006-10-30, 7:03 pm |
| On 10/25/06, Adriano Rodrigues <a.r.ferreira@gmail.com> wrote:
> On 10/25/06, Mug <perl@reborn.org> wrote:
>
> It cannot be done with the bare Perl interpreter, which only
> recognizes scalar, list and void context. In your case, both %u_info =
> and @attribs = call for a list context and that's all.
>
> But you may try the CPAN module Want.
>
> http://search.cpan.org/dist/Want
>
To tell the truth, it would be less subtle if you changed your
function so that an explicit param would tell Perl and also the ones
reading your code that a hash or a list would be returned.
my %u_info = user_detail ( $ENV{QUERY_STRING}, return_type => 'hash' );
# I have $u_info{id} = 'foo' , $u_info{pass} = 12345
my @attribs = user_detail ( $ENV{QUERY_STRING}, return_type => 'list' );
# I have @attribs = ( 'foo', 12345 );
With this change, you won't have dependencies to a module like 'Want'
that requires complilation.
| |
| Paul Lalli 2006-10-30, 7:03 pm |
| D. Bolliger wrote:
> Mug am Mittwoch, 25. Oktober 2006 13:12:
>
> The list on the right hand side is evaluated in scalar context, and that
> delivers the number of entries in the list.
No it doesn't. qw/a b c d e/ is shorthand for ('a', 'b', 'c', 'd',
'e'). The comma operator in scalar context returns its right-hand
argument. There is no "list" in the above code.
The $x there is assigned to 'e', not to 5.
Arrays evaluated in scalar context return their size. Lists in scalar
context do not exists.
Paul Lalli
| |
| nobull67@gmail.com 2006-10-30, 7:03 pm |
|
On Oct 25, 12:41 pm, a.r.ferre...@gmail.com (Adriano Rodrigues) wrote:
> On 10/25/06, Mug <p...@reborn.org> wrote:
>
>
>
>
[color=darkred]
> It cannot be done with the bare Perl interpreter, which only
> recognizes scalar, list and void context. In your case, both %u_info =
> and @attribs = call for a list context and that's all.
>
> But you may try the CPAN module Want.
>
> http://search.cpan.org/dist/Want
It don't believe it will help, it still can't destinguish the two list
contexts.
| |
| John W. Krahn 2006-10-30, 7:03 pm |
| D. Bolliger wrote:
> Mug am Mittwoch, 25. Oktober 2006 13:12:
>
>
> The list on the right hand side is evaluated in scalar context, and that
> delivers the number of entries in the list.
No it doesn't:
$ perl -le' $x = qw/ a b c d e /; print $x'
e
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| D. Bolliger 2006-10-30, 7:03 pm |
| John W. Krahn am Mittwoch, 25. Oktober 2006 19:04:
> D. Bolliger wrote:
>
> No it doesn't:
>
> $ perl -le' $x = qw/ a b c d e /; print $x'
> e
That's the line I should have run before posting...
thanks John, and sorry to
Mug,
a list behaves different than an array:
$ perl -le' @y = qw/ a b c d e /; $x=@y; print $x'
5
Dani
| |
| Uri Guttman 2006-10-30, 7:03 pm |
| >>>>> "DB" == D Bolliger <info@dbolliger.ch> writes:
DB> John W. Krahn am Mittwoch, 25. Oktober 2006 19:04:
[color=darkred]
DB> a list behaves different than an array:
DB> $ perl -le' @y = qw/ a b c d e /; $x=@y; print $x'
DB> 5
no, you missed the real point. there was NO LIST in john's one liner. it
was a bunch of comma separated strings. in older perls qw did generated
a list at run time. in newer perls qw does it at compile time and is no
different than 'a', 'b', ... 'e'. if you assign that to a scalar it just
does the comma op and returns the last expression. see what happens when
i turn on warnings.
perl -wle '$x = qw( a b c ); print $x'
Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.
c
the 'a' and 'b' were useless there. there was NO LIST, just comma ops
and strings.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Bryan R Harris 2006-10-30, 7:03 pm |
|
> D. Bolliger wrote:
>
> No it doesn't:
>
> $ perl -le' $x = qw/ a b c d e /; print $x'
> e
Why? That doesn't make sense to me.
1) perl -le '($x) = qw/a b c d e/; print $x'
a
2) perl -le '$x = qw/a b c d e/; print $x'
e
3) perl -le '$x = ( qw/a b c d e/ ); print $x'
e
4) perl -le '@a = qw/a b c d e/; $x = @a; print $x'
5
Why are (2) and (4) different? Or better what is (2) doing? Why doesn't
(3) work?
- B
| |
| John W. Krahn 2006-10-30, 7:03 pm |
| Bryan R Harris wrote:
>
>
>
> Why? That doesn't make sense to me.
>
> 1) perl -le '($x) = qw/a b c d e/; print $x'
> a
>
> 2) perl -le '$x = qw/a b c d e/; print $x'
> e
>
> 3) perl -le '$x = ( qw/a b c d e/ ); print $x'
> e
>
> 4) perl -le '@a = qw/a b c d e/; $x = @a; print $x'
> 5
>
>
> Why are (2) and (4) different?
In (2) you are assigning to a scalar (scalar context) and in (4) you are
assigning to an array (list context) and then assigning the number of elements
in the array to $x (an array in scalar context evaluates to the number of
elements in that array.)
> Or better what is (2) doing?
Read up on the comma operator in perlop (and I know there are no literal
commas in (2) but qw/a b c d e/ behaves exactly the same as ('a', 'b', 'c',
'd', 'e').)
perldoc perlop
> Why doesn't (3) work?
It does work. It just doesn't work the way you seem to think it should work.
The parentheses are used for precedence and since the expression qw/a b c d
e/ is unambiguous the parentheses are superfluous.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Dr.Ruud 2006-10-30, 7:03 pm |
| Bryan R Harris schreef:
> [attribution repaired] John W. Krahn
>
> Why? That doesn't make sense to me.
Do your tries with
perl -MO=Deparse -wle '...'
and you'll see.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Bryan R Harris 2006-10-30, 7:03 pm |
|
>
>
> Read up on the comma operator in perlop (and I know there are no literal
> commas in (2) but qw/a b c d e/ behaves exactly the same as ('a', 'b', 'c',
> 'd', 'e').)
>
> perldoc perlop
Why would this be considered a binary (?) use? I read the documentation,
but I think I'm missing something, it doesn't make sense.
This is really bizarre and unexpected for me (I'm not the OP, by the way).
Can someone help train my intuition as to why these aren't identical? And
why they shouldn't be?
% perl -le '$x = @{[ "a","b","c","d","e" ]}; print $x'
5
% perl -le '$x = ("a","b","c","d","e"); print $x'
e
- B
|
|
|
|
|