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

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

  • Comment on Re^2: Refactoring: dumb or witty use of ternary operator?

Replies are listed 'Best First'.
Re^3: Refactoring: dumb or witty use of ternary operator?
by cLive ;-) (Prior) on Jun 22, 2004 at 08:12 UTC
    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 ;-)

Re^3: Refactoring: dumb or witty use of ternary operator?
by revdiablo (Prior) on Jun 22, 2004 at 07:07 UTC

    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 $