Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: elsif statement not being evaluated

by afoken (Prior)
on Nov 18, 2012 at 08:35 UTC ( #1004386=note: print w/ replies, xml ) Need Help??


in reply to Re^2: elsif statement not being evaluated
in thread elsif statement not being evaluated

Happens to everyone using C or languages inheriting or stealing from C. But you can make the compiler / interpreter complain loudly if you make sure the constant is on the left side of the operator:

if ($answer=42) { # $answer is now always 42 ... } if (42=$answer) { # "Can't modify constant item in scalar assignment" ... }

This won't help when you compare two variables, but in your case, the variables $an1 and $an2 aren't variable, but constant. So make them constant, either by using Readonly or by using constant:

use Readonly; Readonly my $fortytwo => 42; # note: =>, not = ... if ($fortytwo=$answer) { # "Modification of a read-only value attempte +d" ... }
use constant FORTYTWO => 42; # note: =>, not = ... if (FORTYTWO=$answer) { # "Can't modify constant item in scalar assign +ment" ... }

Or, in this trivial case, get rid of $an1 and $an2 and use 1 and 2.

I prefer Readonly over constant, because Readonly just makes the variables readonly, with no surprises, whereas constant (ab)uses the perl optimizer and implements the constants as functions returning a constant value. This has some nasty side effects documented in Readonly.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re^3: elsif statement not being evaluated
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-07-04 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls