Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: List context, expression precedence, and ()

by HyperZonk (Friar)
on Jul 21, 2001 at 21:36 UTC ( [id://98676]=note: print w/replies, xml ) Need Help??


in reply to List context, expression precedence, and ()

As you get used to Perl programming, I think you will find that (lists) are your friends. Experience will undoubtedly teach you when parentheses can be used only for precedence.

Also, I think that you will find that the specific uses for [brackets] to construct lists are also worth the small inconvenience of not being able to use parentheses without considering whether or not you are emitting a list. You see, square brackets can be used to generate an anonymous list reference, so you can generate a list of lists, or a list within a hash. Very powerful data structures can easily be built in Perl with these constructs. Detailed information about these constructions can be found in the perlref documentation.

Attacking lists may be seen by some as tilting at a sacred part of Perl, so some people might be tempted to downvote your meditation. I would suggest otherwise; your question is valid coming from a new Perl programmer, and should be answered. But I do encourage you to explore the power of lists in Perl, and the ease of creating complex data structures with Perl's current syntax. I think you will end up loving (lists) and their syntax as much as most of the rest of us.

Cheers

Replies are listed 'Best First'.
Re: List context, expression precedence, and ()
by pmas (Hermit) on Jul 21, 2001 at 22:54 UTC
    I do like lists, it is so much more flexible for passing parameters, easy to build optional parameters, etc. I just need always be concerned about scalar/list context in expressions, that's all.

    I found japhy's article about scalar/list context, and it was real eye-opener, when I learned that

    sub A { return ('a', 'b', 'c'); } $a = A(); # $a gets value 'c' ($b) = A(); # $b gets value 'a'
    As japhy explains:
    The A() function, in scalar context, has the comma operator act on the comma separated series of values, and so $a is set to 'c' which makes sense. $b, however, gets the value 'a', because it invokes list context on the function, which returns a list.

    japhy: ++ for you!

    pmas
    To make errors is human. But to make million errors per second, you need a computer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found