Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Perl and the magic of the perl compiler

by coolmichael (Deacon)
on Jun 13, 2001 at 07:23 UTC ( #88000=perlquestion: print w/replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

while(<>){print if (m/foo/i)}
works quite well. It does what I expected it to do. It reads lines from STDIN and prints them if they match.

print while(<>)
also does what I expect it to do, it prints every line from STDIN.

print if(m/foo/i)
also does what I thought it would. It prints $_ if it matches foo.

print if(m/foo/i) while(<DICT>);
doesn't compile. Instead, I get an error message.

C:\>perl -e "use diagnostics; print if (m/foo/i) while(<>)" syntax error at -e line 1, near ") while" Execution of -e aborted due to compilation errors (#1) Uncaught exception from user code: syntax error at -e line 1, near ") while" Execution of -e aborted due to compilation errors.

It makes sense to me, the way I've written it. Maybe I'm asking perl to do to much magic with the if infront of the while. I've tried to find the answer in perlman:perlsyn and the camel (2nd ed.) but haven't had any luck. Maybe I'm not looking in the right place. Can anyone please shed some light?

Replies are listed 'Best First'.
Re: Perl and the magic of the perl compiler
by MeowChow (Vicar) on Jun 13, 2001 at 07:38 UTC
    Quoth the perlsyn:
    Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending). The possible modifiers are:
    if EXPR unless EXPR while EXPR until EXPR foreach EXPR
                   s aamecha.s a..a\u$&owag.print
Re: Perl and the magic of the perl compiler
by Zaxo (Archbishop) on Jun 13, 2001 at 07:37 UTC
    It's a matter of operator precedence1:
    $ perl -e 'm/foo/i && print while <>' i was foo i was foo i was fee i was foolish i was foolish ^D $
    [1] Update: And, as MeowChow says, a single modifier.

    After Compline,

Re: Perl and the magic of the perl compiler
by pmas (Hermit) on Jun 13, 2001 at 08:57 UTC
    Update: Obviously, I was wrong and MeowChow was right.
    Lesson learned: Do not rush to answer questions long after midnight - just go sleep. It is better to your health - and to your XP... : )
    So my last late-night comment below is generally correct, but does not answer question asked above.

    Original answer
    You may want to open file DICT. Operator <STDIN> reads a line from filehandle STDIN, perl opens STDIN for you, but I guess your DICT is not opened yet.
    Also, <> by default uses STDIN, so <> works, too. There was recently discussion about magical <STDIN>, try STDIN in Super Search.

    Sorry I cannot help more, it's getting really late...:(


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://88000]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-20 02:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.