Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Warning while using ternary operator

by simonz (Acolyte)
on Dec 16, 2013 at 11:10 UTC ( #1067311=note: print w/ replies, xml ) Need Help??


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

Yes , you are right, I want what you just mentioned.
But my question is where does $i ne 'one' is happening in my code ? $i is assgined to 'one' and not getting changed.


Comment on Re^2: Warning while using ternary operator
Re^3: Warning while using ternary operator
by BrowserUk (Pope) on Dec 16, 2013 at 11:14 UTC
    But my question is where does $i ne 'one' is happening in my code ? $i is assgined to 'one' and not getting changed.

    The point is that the compiler knows at compile-time that 'nothing' is in a void context; so it warns you.

    You can choose to ignore that warning; or you could even disable it; but it is surely better to simply correct the code.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Warning while using ternary operator
by Athanasius (Monsignor) on Dec 16, 2013 at 12:49 UTC
    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,

Re^3: Warning while using ternary operator
by Bloodnok (Vicar) on Dec 16, 2013 at 14:45 UTC
    It sounds almost like you expect c<$i> to be updated - which won't happen; Are you are confusing a test with an assignment - the eq operator tests for string equality between the LHS and RHS, it is most definitely not an assignment operator.

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-08-30 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls