in reply to Re: Extract potentially quoted words
in thread Extract potentially quoted words

Although in Perl, most statements can also be used as expressions, there are a few exceptions. The while above is a statement modifier and cannot be used in an expression.

For example:

push @elements, $1 while (/\G\s*"(.*?)"/gc or /\G\s*'(.*?)'/gc or /\G\s*(\S+)/gc) or die "Argh";
is still parsed as:
push @elements, $1 while( (/\G\s*"(.*?)"/gc or /\G\s*'(.*?)'/gc or /\G\s*(\S+)/gc) or die "Argh" );
and if we try to force your desired interpretation:
(push @elements, $1 while /\G\s*"(.*?)"/gc or /\G\s*'(.*?)'/gc or /\G\s*(\S+)/gc) or die "Argh";
we get: syntax error at line 1, near "$1  while"

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: Re: Re: Extract potentially quoted words
by merlyn (Sage) on Jun 07, 2001 at 02:30 UTC
    No, there are no statements that can be used as expressions. What were you thinking? There's a clear delineation in Perl between statement things and expression things, and never the twain shall meet.

    -- Randal L. Schwartz, Perl hacker

      To quote perlsyn.pod:

      The only kind of simple statement is an expression evaluated for its side effects.

      Take a random Perl script you have lying about somewhere. Most of the statements in that code will likely be simple statments, that is, just an expression. Such statements can be used as expressions since they are, um, just "an expression evaluated for its side effects". You could put "my @value=" in front of it to demonstrate that fact and most of the time you end up with valid Perl code.

      There are lots of times where I take code that was a statement and rework code around such that I'm now using that statement as an expression (for example, by moving it into the condition of an if or while).

      But if you have a statement that isn't so simple, moving into a "context" where an expression is required will probably get you a syntax error.

              - tye (but my friends call me "Tye")
        Right, but EXPR is an expression, and "EXPR;" is a statement. In loose terminology, you could call the latter "an expression", but it isn't. It's a statement that contains an expression that has a side effect.

        And adding "my @value =" is not adding it to the statement... it's adding it to the expression part of the statement.

        I'll repeat what I said in the chatterbox. In Perl, you have statements, and you have expressions. They are not at the same level in the syntax tree, and you cannot ever substitute one for the other. Your examples above do not show that yet.

        -- Randal L. Schwartz, Perl hacker

      Well, it was a misinterpretation. I guess when I read the line from perlsyn
      The only kind of simple statement is an expression evaluated for its side effects.
      I twisted it around to "a statement is an expression".

      But I think I've got it now: EXPR1 while EXPR2 is a statement, and using || versus or depends on (and affects) what else is in EXPR2, not the full statement.

      On the other hand, "do_something() or die" is an expression (which, being evaluated for its side-effects, is also a statement). If the do_something returns 0 or '', the die will be evaluated as part of the expression, not as a statement modifier.

      Have I got it right now?

      I guess I tripped on the outward similarity of the two, and thought of or as a statement modifier like while and if.

      P.S. merlyn, I don't think this is what you wanted to illustrate in your note, but I did learn something valuable. Thanks!

        Yes, that's exactly what I was pointing at. If you keep "statement" and "expression" separate, it's obvious that "EXPR1 while EXPR2;" is a statement, not an expression, and cannot further be modified, and that "EXPR1 or EXPR2" is still an expression and can be used in a larger expression.

        If you start blurring the concept of "statement" and "expression", you'll be surprised because you start having to use terms like "sometimes" and "mostly". But the rules are actually very obvious once you separate them cleanly.

        -- Randal L. Schwartz, Perl hacker

Re: Re: Re: Extract potentially quoted words
by VSarkiss (Monsignor) on Jun 07, 2001 at 01:24 UTC
    Aha! I knew your last example wouldn't work because I tried it. But I misinterpreted how the code I posted was working.

    Thanks for the correction, I appreciate it.