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

Re: [MacPerl] FNG



<x-flowed>At 20:24 +0100 1/27/99, Christian Brechbuehler wrote:
>
>     1      #! /usr/local/bin/perl -w
>     2
>     3      $_ = "no match";
>     4      $x = s/%40/@/;
>     5      $y = "";
>     6      $notx = ~$x;
>     7      $noty = ~$y;
>     8      $str_equal = $x eq $y;
>     9      $num_equal = $x == $y;
>     10
>     11   print "'$x', '$y', $str_equal, $num_equal, '$notx', '$noty'\n";
>
> warns me that Argument "" isn't numeric in eq at line 9,

it's not...

> and prints
>
>     '', '', 1, 1, '4294967295', ''
>> First, $x is a string.  Then it is a number. My question: How many
>> kinds of "scalars" exist?

One :-) Or three. Depending on your viewpoint.

As sayeth the Camel, container of all knowledge (p. 38)

   While we might speak of a scalar as "containing" a number or a string,
   scalars are essentially typeless; there's no way to declare a scalar of
   type "number" or "string". Perl converts between the various subtypes as
   needed, so you can treat a number as a string or a string as a number,
   and Perl will do the Right Thing.

Well, Perl's idea of the Right Thing.  Which is sometimes not what 
the programmer had in mind (usually because the programmer wasn't 
planning on a conversion; i.e. the programmer goofed :-)

Perl is quite willing to treat any given scalar as whatever you ask 
it to be treated as. If you ask it to be treated as a string

     $str_equal = $x eq $y;

Perl complies.  Similarly, if you ask for treatment as a number

     $num_equal = $x == $y;

Perl does what you ask.  In Perl, "Be careful what you wish for.  You 
might get it."

We had this piece of code at a previous job:

     while (<FILE>) {
	    ($field1,$rest) = split(/\t/);
	    if (($field1 == $previous)  {
            # repetition
		    ...
        } else {
            # new identifier
            ...
        }
        $previous = $field1;
     }

input lines in FILE were of the form:

    g1840610        descriptive information
    g1840611        descriptive information
    g1840611        descriptive information
    g1840611        descriptive information
    g1840612        descriptive information

and the script iterated through.  It never got into the else clause...
It took me longer than I care to admit to track down the reason.

- Vicki
---
      |\      _,,,---,,_       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>