Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Slice misuse ?

by blogical (Pilgrim)
on Aug 07, 2006 at 21:23 UTC ( [id://566014]=perlquestion: print w/replies, xml ) Need Help??

blogical has asked for the wisdom of the Perl Monks concerning the following question:

I had:

$first = ('abcd' =~ /(\w)\w(\w)/ )[0]; print $first;

I wanted
print ('abcd' =~ /(\w)\w(\w)/ )[0];

But apparently I can't have what I want. What is at work here?

"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden

Replies are listed 'Best First'.
Re: Slice misuse ?
by bobf (Monsignor) on Aug 07, 2006 at 21:33 UTC

    strict and warnings would have given you a clue:

    print (...) interpreted as function at C:\test.pl line 11. syntax error at C:\test.pl line 11, near ")["

    From perldiag:

    %s (...) interpreted as function
    (W syntax) You've run afoul of the rule that says that any list operator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See Terms and List Operators (Leftward) in the perlop manpage.

    I'm not a guru on these things, but I think the parens you used to place the regex in list context (and take the slice) are interpreted by perl to be the optional parens around the arguments to the print function. You need an extra set to make everything happy:

    print( ('abcd' =~ /(\w)\w(\w)/ )[0] ); # prints 'a'

    Update: Yup, that was it. Added diagnostic message from perldiag.

Re: Slice misuse ?
by gellyfish (Monsignor) on Aug 07, 2006 at 21:31 UTC

    I think you wanted:

    print +('abcd' =~ /(\w)\w(\w)/ )[0];
    You need to use the unary + to disambiguate the parentheses which otherwise perl will parse as introducing the argument list for print which gives rise to the syntax error with the [0]

    /J\

Re: Slice misuse ?
by liverpole (Monsignor) on Aug 07, 2006 at 21:35 UTC
    Hi blogical,

    You could also use printf, which expects its first argument to be a format string, like so:

    printf "%s", ('abcd' =~ /(\w)\w(\w)/ )[0];

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Slice misuse ?
by GrandFather (Saint) on Aug 07, 2006 at 21:36 UTC

    Always add use strict; use warnings; at the start of your script. If you had done so you would have received the error:

    print (...) interpreted as function at ...

    and saved everyone some time. :)


    DWIM is Perl's answer to Gödel

      I had yet to run it in context, where I DO have warnings and strict on. I encountered the error while running perl -c on a mason component I wrote.

      my $id = $ARGS{to} || ( $m->dhandler_arg =~ /^(\w+)\/pledge/ )[0]; is the offending line. ( $m->dhandler_arg =~ /^(\w+)\/pledge/ )[0] seemed to be the offending part, so I pulled it out for examination.

      I used a print statement for testing. I should have run perl -We instead of -e on my test case. Also, I should have used a more accurate test case, as I seem to have gone hunting for the a problem I didn't originally have (and found it!)

      "One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
      - Henry David Thoreau, Walden

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found