Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Yet another List Versus Scalar Context Meditation

by tphyahoo (Vicar)
on Feb 04, 2005 at 14:40 UTC ( [id://428070]=perlmeditation: print w/replies, xml ) Need Help??

#scalar versus array context with reverse. # see http://prometheus.frii.com/%7Egnat/yapc/2000-stages/slide16.html # for where I began playing with this idea... use warnings; use strict; #scalar context, because there's a scalar on the left. my $reverse_gnat = reverse("gnat","bang"); print "1:"; print $reverse_gnat; #gnabtang print "\n"; my @reverse_gnat = reverse("gnat","bang"); print "2:"; #list context, because there's an array on the left. print @reverse_gnat; #banggnat print "\n"; print "3:"; #scalar context (because of the dot operator, I guess) print @reverse_gnat . "\n"; #2, the number of elements in the array # Commas and dots in print statements *ain't* the same... print "4:"; #list context, I guess that's the default for print. print reverse ("gnat", "bang", "\n"); #newline, then bang gnat print "\n"; # leave out the parenthesis, gnatbang\n is the only item in the list # so the effect is nothing happens print "5:"; print reverse "gnat" . "bang" . "\n"; #gnatbang print "6:"; #reverse forced into scalar context by the dot operator print reverse ("gnat") . "bang" . "\n"; #tangbang #few more examples for review. print "7:"; print reverse ("gnat") . "\n"; #tang print "8:"; print reverse "gnat" . "\n"; #gnat print "9:"; print reverse "gnat" , "\n"; #newline, then gnat

Replies are listed 'Best First'.
Re: Yet another List Versus Scalar Context Meditation
by merlyn (Sage) on Feb 04, 2005 at 14:47 UTC
Re: Yet another List Versus Scalar Context Meditation
by DentArthurDent (Monk) on Feb 07, 2005 at 16:12 UTC
    I've struggled with this kind of issue before. One of the main benefits of Perl is that TIMTOWTDI, no doubt about it. I know, it's all about context. There's nothing wrong with the rules as they are currently constitued. However, I've found in a number of cases Perl does not do the least suprising thing. The examples above show that while being incredibly flexible, there's a lot of room for a small mistake to yield far reaching, mean, nasty consequences. At what point does TIMTOWTDI make Perl code start to be less readable, and thus less maintainable?

    I don't have any preconceived notions about it, I'm just asking the question.
    ----
    My mission: To boldy split infinitives that have never been split before!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://428070]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found