[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Novice question regards subroutines



Always a chance to learn. Bruce Van Allen wrote:
>If you use "1", the
>>1 is not being compared as a number, but rather a string.

And Chris Nandor replied:
>That is not true.
>
[snip]
>Perl does its best to see
>if "1" can be made into a recognizable number before doing the comparison.
>This even "works":
>
>  #!perl -wl
>  $x = 1e10;  # is this a number?
>  print $x;   # print to see if it is a number, indeed
>  print "yes" if $x == "1e10";  # can "1e10" in QUOTES
>                                # be considered a NUMBER?  i guess so ...
>
>Returns:
>
>  10000000000
>  yes

OK, so any value that can be resolved to a number will be, quoted or bare.
My main point for PwrSurge was to watch out for wrong use of operators.
Check this:

#!perl -wl
$x = "one plus one";          #watch out for this one!
print "yes" if $x = "two";    #Why does this work? The assignment succeeded,
print $x;                     #so it's true.

This returns:

# Found = in conditional, should be ==.
File 'Untitled #2'; Line 3
yes
two

If this were run without the -w, it would return:

yes
two

And it's clear that the answer to PwrSurge's problem, at least with the if
statements.

[code excerpt:

 if ($MainMenu = "1") {
    &CheckMenu;
  }
  elsif ($MainMenu = "2") {
    &DebitCardMenu;
  }
  elsif ($MainMenu = "3") {
    &ATMWithdrawlMenu;
  }
  elsif ($MainMenu = "4") {
    &DepositMenu;
  }
  elsif ($MainMenu = "5") {
    die "Terminating the system.";
  }
  else {
    print "Invalid  option; try again.\n";
    &MainMenu;

end code excerpt]

The first conditional, $MainMenu = "1", will _always_ return true, as long
as there is a single = there. This is not doing a comparison, it's doing an
assignment.

Onward.

 - Bruce

- Bruce

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Bruce Van Allen
bva@cruzio.com
408/429-1688
P.O. Box 839
Santa Cruz, CA  95061



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch