Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Warning while using ternary operator

by BrowserUk (Pope)
on Dec 16, 2013 at 10:53 UTC ( #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.


Comment on Re: Warning while using ternary operator
Select or Download Code
Re^2: Warning while using ternary operator
by simonz (Acolyte) 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
Node Status?
node history
Node Type: note [id://1067309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (19)
As of 2014-07-14 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (259 votes), past polls