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

[MacPerl] Re: scalars (was FNG)



<x-flowed>At 16:23 -0500 1/28/99, Chris Nandor wrote:
> It says there is no way to declare a scalar of type number or string.  If
> that were the case, then these would not return different values.  A better
> example:
>
>   #!perl -wl
>   $x = '1';  # declared as string
>   print ~$x; # treated as string
>   $x += 0;   # declared as a number
>   print ~$x; # treated as number
>   $x .= '';  # declared as string again
>   print ~$x; # treated as string again
>
> At least, this makes the Camel wrong as I read it.  But it seems clear to
> me that these scalars are typed as strings and as numbers depending on how
> you used it.  Else ~$x would always produce the same thing, since in every
> case $x == 1 (and $x eq '1' :).


I think that's the point, though.  In C you would say
  int i;

and i _is_ an integer. You have to forcibly coerce it or change it to 
make it something else.

In Perl it's not so much setting a type for all time as how you _use_ 
the variable.  (That is "declared as a number" means "it was being 
used as a number when it was declared" which is is not quite exactly 
the same thing as "declared as type number")

I admit that this is perhaps a subtle distinction.  But probably (as 
is much of Programming Perl) this is aimed at people with a C (or 
FORTRAN) background.  C programmers are not used to variables 
changing back and forth between string and number depending on how 
you use them... :-)

- V.

---
      |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
     |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
    '---''(_/--'  `-'\_)       http://www.cfcl.com/~vlb  www.ptf.com/macperl

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


</x-flowed>