http://www.perlmonks.org?node_id=619274


in reply to Re: using split
in thread using split

if ($cols[11] = $target1)

looks wrong!

Perhaps, but not really. One may need to assign a variable from another expression then test the truthness of that variable. What looks wrong if the variable is assigned to a literal value. This will issue a syntax warning (unless warnings are disabled):
my $x; if ($x = 'a literal value') { # will issue warnings }
Note that the if is (effectively) testing the contents of $target1, not equality of $target1 and $cols[11].
While you are right about the equality part, but the if construct is effectively testing the whole expression, not just the RHS.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Replies are listed 'Best First'.
Re^3: using split
by GrandFather (Saint) on Jun 05, 2007 at 03:26 UTC

    I didn't say "is wrong", I said "looks wrong" (yes, I know you didn't say I said that either, but you seem to have reacted as though I did). It looks wrong to the extent that it should either be rewritten to "look right" or be commented. However the only appropriate comment effectively says "I did a dumb thing that needs an even dumber comment so that you know I'm being silly". Much better to rewrite it as:

    $x = $y; if ($x) { ... }

    which makes it quite clear that the assignment is intended and also quite clear what is being tested. Most countries don't tax code by the line so the cost of being clear is pretty low. ;)


    DWIM is Perl's answer to Gödel