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

Syntax question with

by rlk (Pilgrim)
on Jan 02, 2001 at 08:53 UTC ( #49254=perlquestion: print w/replies, xml ) Need Help??
rlk has asked for the wisdom of the Perl Monks concerning the following question:

I'm hesitant to post an "did I find a bug in perl?" type question, but I ran across a snippet of code yesterday that has caused me some confusion.

The original code looked like this:

map {1,$_} @foo
However, this raises a syntax error. Some further testing revealed that
map {(1,$_)} @foo
and, somewhat more surprisingly
map {$_,1} @foo
both work as expected (i.e, return the elements of @foo interlaced with ones)

To further confuse the issue, this error cropped up several times in a set of otherwise completely functional scripts, leading me to wonder if this is a change (/me avoids using the word "bug") in 5.6, which I believe the scripts predate. Any thoughts?

Ryan Koppenhaver, Aspiring Perl Hacker

Replies are listed 'Best First'.
Re: Syntax question with
by merlyn (Sage) on Jan 02, 2001 at 09:15 UTC
    Perl has to guess whether the open curly brace after the map is the beginning of an anonymous array constructor or a block, since it could be both. It does this by "peeking ahead" a bit to see what's there.

    I've learned to force it to guess right when needed by either putting a plus in front of the open curly (for the anonymous hash flavor) or a semicolon immediately after the open curly (for a block). Pretty rare to need that, but when I do, it's handy that I recall those tricks. I first learned of that from perlref.

    -- Randal L. Schwartz, Perl hacker

Re: Syntax question with
by repson (Chaplain) on Jan 02, 2001 at 11:05 UTC
    If this is a new problem it seems that it may have something to do with the current prototype system.

    If map was declared map (&@) {} then all the constructs above would work (since the first argument is always interpreted as a code block/ref and I've tested it with a custom map under 5.6), except that the other map syntax map EXPR, list as opposed to the one under discussion map BLOCK list would not work.

    I can't work out how to use the EXPR syntax for user-defined subs (perlsub suggests grep is (&@) which wouldn't work since the first argument to grep can be a block or an re). So map must be a special case in the perl internals but the code that determines must as merlyn says be unable to determine what it should be using when the braces can have two meanings. If map is really special cased then this problem could probably be fixed if there was any real need, for now use {; } as merlyn suggested of create a wrapper for map which does have a prototype of (&@).

Re: Syntax question with
by I0 (Priest) on Jan 02, 2001 at 09:09 UTC
    furthur, map {1e1,2} @foo is a syntax error, whereas map {1e+1,2} @foo and map {1.0,2} @foo work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://49254]
Approved by root
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2017-05-29 21:29 GMT
Find Nodes?
    Voting Booth?