Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Warning while using ternary operator

by Athanasius (Monsignor)
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,


Comment on Re^3: Warning while using ternary operator
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-09-23 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls