Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Pragma inside grep block

by rovf (Priest)
on Jul 08, 2009 at 12:00 UTC ( [id://778211]=perlquestion: print w/replies, xml ) Need Help??

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

Why does Perl 5.8.8 complain "no" not allowed in expression in this program:

use strict; use warnings; grep { no strict 'refs'; }qw(x);
but not in this:
use strict; use warnings; grep { 0; no strict 'refs'; }qw(x);
?

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Pragma inside grep block
by moritz (Cardinal) on Jul 08, 2009 at 12:04 UTC
    Because perl mis-parses this for grep HASHREF (but I have no idea why), expects a statement, sees a 'no' and complains.

    Yes, this could be done better, but fiddling with the perl 5 lexer is non-trivial and not for the faint of hearted.

Re: Pragma inside grep block
by RMGir (Prior) on Jul 08, 2009 at 12:08 UTC
    Good question.

    5.10.0 has the same problem:

    $ perl -e'use strict; use warnings; grep { no strict "refs"; }qw(x); ' "no" not allowed in expression at -e line 2, at end of line BEGIN not safe after errors--compilation aborted at -e line 2. $ perl -e'use strict; use warnings; grep { 0; no strict "refs"; }qw(x); ' $
    Of course,
    { no strict "refs"; grep {0;} qw(x); }
    would work fine, so at least there's a workaround...

    Mike
      Workaround is to add ;
      grep { ;no strict 'refs'; } qw(x);
      but that confuse Deparse :D
      perl -MO=Deparse use strict; use warnings; grep { ;no strict 'refs'; } qw(x); ^D use warnings; use strict 'refs'; grep {();} 'x'; - syntax OK
      Also works
      grep { do{ no strict 'refs'; } } qw(x)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found