Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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;

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,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067325]
help
Chatterbox?
[Discipulus]: hi karl, i played (well tried to) play the guitar since many years. it is something not natural for me: with the flute was love at first .. sound
[Discipulus]: iirc i took some interesting guitar link from your homenode
[marioroy]: nice karlgoethebier guitar links on your homenode.
[marioroy]: The nature of the Guitar
[Discipulus]: my best with guitar is to fingerpicking some De André songs

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-03-24 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (298 votes). Check out past polls.