where does $i ne 'one' is happening in my code ?
It isn’t! To see this, print out the values of $hash->{name} and $i:
#! perl
use strict;
use warnings;
my $hash = {};
my $i = 'one';
print "\nOriginal code:\n\n";
($i eq 'one') ? $hash->{'name'} = "hello"."world" : 'nothing';
printf "name: %s, i: %s\n", $hash->{name}, $i;
print "\nBrowserUk's fix:\n\n";
$hash->{name} = ($i eq 'one') ? "hello" . "world" : 'nothing';
printf "name: %s, i: %s\n", $hash->{name}, $i;
Output:
22:46 >perl 801_SoPW.pl
Useless use of a constant ("nothing") in void context at 801_SoPW.pl l
+ine 21.
Original code:
name: helloworld, i: one
BrowserUk's fix:
name: helloworld, i: one
22:46 >
As you can see, the warning is printed before the script runs, because it’s a compile-time warning, as BrowserUk says. But in both cases, the result of the assignment is that $hash->{name} contains helloworld, showing that $i eq 'one' is true. What makes you think that $i becomes not equal to 'one'?
For the ternary operator, see perlop#Conditional-Operator.
Hope that helps,
|