fourmi has asked for the wisdom of the Perl Monks concerning the following question:

$BasicVar = 'BasicVar"; my $MyVar = "MyVar"; ($BasicVarParenth) = "BasicVarParenth"; my ($MyVarParenth) = "MyVarParenth";
What is the difference between th ebove, and what happens for ($Var) specifically if respecified insdide an if loop, does it return to the initial value when outside the loop? An explicit answer would be great, written or directed is fine! cheers ant

Replies are listed 'Best First'.
Re: variable definitions
by fourmi (Scribe) on Aug 15, 2003 at 13:31 UTC
    It's basically because i always seem to get $Updated == "No" in the following
    $Updated = "No"; foreach $Line (@Lines) { if ( $WeWantToMakeAChange = "true") { &MakeTheChange; $Updated = "Yes"; } } if ($Updated == "No") { &NoChangesToAnyLinesWereMadeSoNowDoThis; }

    &NoChangesToAnyLinesWereMadeSoNowDoThis always seems to run, even though changes are made...
      Try reading up on basic Perl operators. == is for numbers. eq is for strings. You're comparing strings, so use eq. Both use the same assignment operator, which is =.

      (Confusing explanation coming...) The technical reason why your if-statement keeps running is that strings, when treated as numbers, are equal to 0. Both "Yes" and "No" are numerically 0, and thus numerically equal.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      You should probably use strict first off - would catch one of the errors in your code.

      First - you're using the wrong equality tester - if you want to test if a variable is equal to the string "true" use the eq operator.

      This follow line has to be changed for 2 reasons - you are actually doing an assignment - the single equals = means $WeWantToMakeAChange gets the value of 'true'. Second you should use eq to evalue if a string is equal.

      if( $WeWantToMakeAChange eq "true" ) { }
      But why not use 1 for true and 0 for false instead of these strings?
        Damn, fatigue takes its toll again. Thanks folks up and running again 9when im supposed to..) cheers ant