Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Refactoring: dumb or witty use of ternary operator?

by cLive ;-) (Prior)
on Jun 22, 2004 at 04:32 UTC ( [id://368612]=note: print w/replies, xml ) Need Help??


in reply to Refactoring: dumb or witty use of ternary operator?

Or am I overseeing some "important" side effect?

Probably a warnings freak like me :) If the the value of $outhash{$str} is undefined, you'll get a warning about using an undefined value. update - you won't but I bet your colleague assumed the same thing that I did :)

My guess is that the keys aren't known until the script is run, and that's your colleague's way of getting rid of the warnings. A perfectly valid way too. They are making you explicitly aware of the fact that $outhash{$str} may be undefined when you try to increment it.

This would work equally well:

$outhash{$str}||=0; $outhash{$str}++;

.02

cLive ;-)

Replies are listed 'Best First'.
Re: Refactoring: dumb or witty use of ternary operator?
by Abigail-II (Bishop) on Jun 22, 2004 at 10:06 UTC
    If the the value of $outhash{$str} is undefined, you'll get a warning about using an undefined value.
    Are you sure about that? Can you show code giving the warning? If you can construct code that generates a warning when ++ is applied to an undefined value, make sure to report it as a bug, because that's not supposed to happen.
    This would work equally well:
    $outhash {$str} ||= 0; $outhash {$str} ++;
    Yeah, but why bother? From the documentation about auto-increment and auto-decrement:
    "undef" is always treated as numeric, and in particular is changed to 0 before incrementing (so that a post-increment of an undef value will return 0 rather than "undef").

    Abigail

      "Are you sure about that?".

      No, obviously, but my guess is that his colleague made the same assumption that I did :)

      cLive ;-)

Re^2: Refactoring: dumb or witty use of ternary operator?
by diotalevi (Canon) on Jun 22, 2004 at 18:32 UTC
    If that were a check for an undef value, it should have used the defined() test which is instructive to us when trying to understand the code.
Re^2: Refactoring: dumb or witty use of ternary operator?
by PetaMem (Priest) on Jun 22, 2004 at 06:54 UTC
    If the the value of $outhash{$str} is undefined, you'll get a warning about using an undefined value.

    This was my first thought too, but then the plain mentioning of $outhash{$str} in the ternary operator test section should do also - shouldn't it?

    Bye
     PetaMem
        All Perl:   MT, NLP, NLU

      Try this:
      #!/usr/bin/perl use strict; use warnings; my $x; $x = $x+1; print $x;
      I get:
      Use of uninitialized value in addition (+) at tmp.pl line 6.
      Then try:
      #!/usr/bin/perl use strict; use warnings; my $x; $x = $x ? $x+1 : 1; print $x;
      to see the difference...

      cLive ;-)

      Neither methods generate a warning for me, using perl 5.8.4.

      $ perl -wle '$outhash{$ARGV[0]}++; print $outhash{$ARGV[0]}' foo 1 $ perl -wle '$outhash{$ARGV[0]} ? ($outhash{$ARGV[0]}++) : ($outhash{$ARGV[0]} = 1); print $outhash{$ARGV[0]}' foo 1 $

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://368612]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found