Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Is it evaluated in scalar or list context?

by rnaeye (Pilgrim)
on Jan 19, 2013 at 17:32 UTC ( #1014216=perlquestion: print w/ replies, xml ) Need Help??
rnaeye has asked for the wisdom of the Perl Monks concerning the following question:

Hi! Monks,

I understand following:

my $var = something; #scalar context my ($var) = something; #list context

What happens below? Are $var1, $var2, $var3, and $var4 evaluated in scalar or list context?

my ($var1, $var2, $var3, $var4); $var1 = something1 ; $var2 = something2 ; $var3 = something3 ; $var4 = something4 ;

Thanks for help.

Comment on Is it evaluated in scalar or list context?
Select or Download Code
Re: Is it evaluated in scalar or list context?
by CountZero (Bishop) on Jan 19, 2013 at 17:40 UTC
    Scalar context, of course.

    The my ($var1, $var2, $var3, $var4); has no influence on any of the following lines. Actually context is irrelevant for that line since no assignment is done.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Is it evaluated in scalar or list context?
by Khen1950fx (Canon) on Jan 19, 2013 at 17:49 UTC
Re: Is it evaluated in scalar or list context?
by LanX (Canon) on Jan 19, 2013 at 17:50 UTC
    maybe your irritated by the "function-like" look of my, but

    my ($var1, $var2, $var3, $var4);

    is only a short form of

    (my $var1, my $var2, my $var3, my $var4);

    Cheers Rolf

Re: Is it evaluated in scalar or list context?
by Kenosis (Priest) on Jan 19, 2013 at 17:58 UTC

    In your example, my ($var1, $var2, $var3, $var4); is used to declare, not assign:

    my $var = something; # declare then assign in scalar context my ($var) = something; # declare then assign in list context ... my ($var1, $var2, $var3, $var4); # declare $var1 = something1 ; # assign in scalar context $var2 = something2 ; # assign in scalar context $var3 = something3 ; # assign in scalar context $var4 = something4 ; # assign in scalar context
Re: Is it evaluated in scalar or list context?
by 7stud (Deacon) on Jan 19, 2013 at 19:53 UTC
    $var1 = something1 ;

    On the left of the assignment is a scalar variable. A scalar variable on the left of an assignment cries out, "Give me a scalar(single) value to store!" Similarly, an array variable on the left of an assignment:

    @x = something;

    ...cries out, "Give me a list to store!"

    Now look at this code:

    use strict; use warnings; use 5.012; my @something = ('a', 'b', 'c'); my $x = @something; my($y) = @something; say $x; say $y; --output:-- 3 a

    When $x cries out for a single value, the array provides its length. On the other hand, ($y) is a list, and it cries out for a list--so that list assignment can be performed:

    my @something = ('a', 'b', 'c'); my $d; my $e; my $f; ($d, $e, $f) = @something; say $d; say $e; say $f; --output:-- a b c
    ($d, $e) = @something; say $d; say $e; --output:-- a b
    ($d) = @something; say $d; --output:-- a
    $d = @something; say $d; --output:-- 3
    my($s, $t, $u) = @something; say $s; say $t; say $u; --output:-- a b c
Re: Is it evaluated in scalar or list context?
by Athanasius (Monsignor) on Jan 20, 2013 at 04:13 UTC

    Hello rnaeye,

    my $var = something; # scalar context my ($var) = something; # list context

    This is correct, but from the rest of your question it seems you understand this to mean that it is $var which is in first scalar and then list context. Actually, it is something which is evaluated in either scalar or list context. That is, in an assignment, it is the right-hand side which is evaluated in either scalar or list context; the form of the left-hand side determines the context in which the right-hand side is evaluated.

    You can use wantarray in a subroutine to see this:

    use Modern::Perl; my $var1 = context('a'); my ($var2) = context('b'); say for $var1, $var2; context($var2); sub context { my $c = (defined wantarray) ? (wantarray ? 'list' : 'scalar') : 'void'; say '(', $_[0], '): is in ', $c, ' context'; return wantarray ? @_ : $_[0]; }

    which outputs:

    14:04 >perl 488_SoPW.pl (a): is in scalar context (b): is in list context a b (b): is in void context 14:08 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Is it evaluated in scalar or list context?
by Anonymous Monk on Jan 20, 2013 at 05:57 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014216]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-10-22 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls