Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: If you believe in Lists in Scalar Context, Clap your Hands

by moritz (Cardinal)
on Oct 23, 2008 at 18:57 UTC ( #719125=note: print w/ replies, xml ) Need Help??


in reply to If you believe in Lists in Scalar Context, Clap your Hands

Finally, and for extra points (and points mean prizes), how does one describe the difference between a list and an array ?

That's not that hard. Anything that you can push onto is an array, all other things that look like arrays or lists are actually lists. (Any array method that changes the length works as a probe instead)

Examples for Arrays:

@array # this works: push @array, $item;

Examples for Lists:

# things returned from subs: localtime(); # slices: @INC[0,1,-1]; # literals qw(foo bar); # return values from operators ('12') x 10; # you can't push() on any of these - and it wouldn't make sense anyway


Comment on Re: If you believe in Lists in Scalar Context, Clap your Hands
Select or Download Code
Re^2: If you believe in Lists in Scalar Context, Clap your Hands
by oshalla (Deacon) on Oct 23, 2008 at 19:31 UTC

    OK. Lists cannot be lvalues, though you may have a list of lvalues...

    Suppose I refine the points paying question: as an rvalue how does one describe the difference between a list and an array ?

      An array is everything that could be used as an lvalue (perhaps with the exception of :lvalue subs, haven't tried those), independently of you actually use it as an lvalue.

      That definition so far helped in all real-world cases that I've look at.

        perhaps with the exception of :lvalue subs, haven't tried those

        lvalue subs are called in scalar context, so they can't return a list.

        use strict; use warnings; my ($i,$j); sub f :lvalue { ($i,$j) } f() = (4,5); print("$i,$j\n");
        Useless use of a constant in void context at script.pl line 5. Use of uninitialized value in concatenation (.) or string at script.pl + line 6. ,5

      Lists cannot be lvalues, though you may have a list of lvalues...

      Say again? An lvalue list returns a list of lvalues and enforces list context, just like an rvalue list returns a list of rvalues in list context.

      In both of the following snippets, you'll see an list as an lvalue ("M" flag).

      >perl -MO=Concise -e"($a,$b)=(4,5)" a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <2> aassign[t3] vKS ->a - <1> ex-list lKP ->6 <-- RHS list 3 <0> pushmark s ->4 4 <$> const[IV 4] s ->5 5 <$> const[IV 5] s ->6 - <1> ex-list lKPRM* ->9 <-- LHS list w/ "M" 6 <0> pushmark sRM* ->7 - <1> ex-rv2sv sKRM*/1 ->8 7 <#> gvsv[*a] s ->8 - <1> ex-rv2sv sKRM*/1 ->- 8 <#> gvsv[*b] s ->9 -e syntax OK
      >perl -MO=Concise -e"1 for ($a,$b)" d <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 3 <;> nextstate(main 1 -e:1) v ->4 c <2> leaveloop vK/2 ->d 8 <{> enteriter(next->9 last->c redo->9) lK/8 ->a - <0> ex-pushmark s ->4 - <1> ex-list lKPM ->7 <-- lvalue list 4 <0> pushmark sM ->5 - <1> ex-rv2sv sKM/1 ->6 5 <#> gvsv[*a] s ->6 - <1> ex-rv2sv sKM/1 ->- 6 <#> gvsv[*b] s ->7 7 <#> gv[*_] s ->8 - <1> null vK/1 ->c b <|> and(other->9) vK/1 ->c a <0> iter s ->b - <@> lineseq vK ->- - <0> ex-const v ->9 9 <0> unstack v ->a -e syntax OK

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-18 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (110 votes), past polls