Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^3: Warning while using ternary operator

by Athanasius (Chancellor)
on Dec 16, 2013 at 12:49 UTC ( #1067325=note: print w/replies, xml ) Need Help??

in reply to Re^2: Warning while using ternary operator
in thread Warning while using ternary operator

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;


22:46 >perl Useless use of a constant ("nothing") in void context at 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,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067325]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2018-05-22 12:55 GMT
Find Nodes?
    Voting Booth?