Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

warning! -- Why?

by jwkrahn (Abbot)
on May 12, 2022 at 03:37 UTC ( [id://11143807]=perlquestion: print w/replies, xml ) Need Help??

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

sub process_variables { my ( $value, $remove_undef ) = @_; $value =~ s/ (?<! \\ ) \$ \{ ( .*? ) \} / defined $variable{ $1 } ? $variable{ $1 } : defined $remove_undef && $remove_undef # for if statements, any variable that is not defined, # we simple convert to 0 ? '0' # This could be an option that is used later, save # it so we don't warn if this option is not one of # ktests options. And put back the origin piece. : ( $used_options{ $1 } = 1 and "\${$1}" ) /exg; return $value; }

The code above produces the warning message:
Found = in conditional, should be == at file line n.
If I change:
( $used_options{ $1 } = 1 and "\${$1}" )
To:
do { $used_options{ $1 } = 1 and "\${$1}" }
I still get the same warning message.

But obviously the message is wrong because there is no = in conditional.

$ perl -v This is perl 5, version 24, subversion 1 (v5.24.1) built for x86_64-li +nux-gnu-thread-multi (with 90 registered patches, see perl -V for more detail) Copyright 1987-2017, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

Replies are listed 'Best First'.
Re: warning! -- Why?
by haj (Vicar) on May 12, 2022 at 07:05 UTC

    The and is the conditional here. The equal sign = has a higher precendence than and and Perl guesses (it's a warning, not a syntax error) that you meant to have a comparison on its left side. The expression might also not do what you want since the result of and is not its second operand, but I guess you want "\${$1}" as replacement string.

    The cleanest (IMO) way to get rid of that warning is to use do, as you did, but to replace the and in the do block with a semicolon:

    do { $used_options{ $1 } = 1; "\${$1}" }

      Bingo!

      Yes, I figured this out a couple of hours after I posted.

      Thanks

Re: warning! -- Why?
by Athanasius (Archbishop) on May 12, 2022 at 07:14 UTC

      Thanks. It is also about 15% faster than using do { ... } !

Re: warning! -- Why?
by Tux (Canon) on May 12, 2022 at 06:59 UTC
    ( $used_options{ $1 } = 1 and "\${$1}" )

    is a condition. expression and action could also be written as if (expression) { action } or action if expression and in that expression, your = should clearly be ==.


    Enjoy, Have FUN! H.Merijn
Re: warning! -- Why?
by hippo (Bishop) on May 12, 2022 at 08:54 UTC
    ( $used_options{ $1 } = 1 and "\${$1}" )

    I fail to see why you are using and here in the first place. Is there any scenario under which $used_options{ $1 } = 1 evaluates to false?


    🦛

Re: warning! -- Why?
by kcott (Archbishop) on May 12, 2022 at 09:37 UTC

    G'day jwkrahn,

    It's a common mistake to forget, or not realise, that "&&" has a higher precedence than "=" which, in turn, has a higher precedence than "and". See "perlop: Operator Precedence and Associativity" (there's a table at the end of that section which lists the order of precedence).

    Changing "and" to "&&" gets rid of the warning; however, in this instance, that's probably not what you want. Compare these two:

    $ perl -MO=Deparse,-p -e '($used_options{ $1 } = 1 and "\${$1}")' (($used_options{$1} = 1) and ("\${$1}")); $ perl -MO=Deparse,-p -e '($used_options{ $1 } = 1 && "\${$1}")' ($used_options{$1} = "\${$1}");

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found