Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Warning while using ternary operator

by BrowserUk (Patriarch)
on Dec 16, 2013 at 10:53 UTC ( [id://1067309]=note: print w/replies, xml ) Need Help??


in reply to Warning while using ternary operator

When $i ne 'one', the constant:  'nothing' is not assigned to anything, thus in a void context.

Did you want:

$hash->{'name'} = ($i eq 'one') ? "hello"."world" : 'nothing';

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.

Replies are listed 'Best First'.
Re^2: Warning while using ternary operator
by simonz (Sexton) on Dec 16, 2013 at 11:10 UTC

    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.

      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.
      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,

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1067309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found