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

Re: [MacPerl] Callbacks not reliable?



At 15:31 -0600 2000.09.14, Keary Suska wrote:
>I had always thought in m// or s/// that if the pattern doesn't match that
>the callback variables (i.e. $1, $2, $3 ...) would be undefined. I am
>finding that this isn't the case at least with a substitution.

It seems fine to me.


>I have a number formatting routine:
>
>sub FormatNum {
>
>  my $num = shift;
>  my( $dec, @nums, $new, $i );
>
>  $num =~ tr/0-9.//cd;        # numeric only
>  $num =~ s/(\.\d+)//;        # remove decimal
># if the prior substitution fails
># $1 has a value from a previous match/sub!?
>  $dec = $1;
>  @nums = split( //, $num );
>  foreach ( reverse @nums ) {    # get each num in revese order and build
>from right to left
>    $new = ",$new" if $i> 0 and $i%3 == 0;    # add comma if needed (every
>third digit)
>    $new = "$_$new";
>    $i++;
>  }
>  return "$new$dec";
>}

Exactly what behavior are you seeing?  I took your sub and ran:

   print FormatNum(1.2);print FormatNum(1);

and I got "1.2" and "1".  $dec was not defined for the second one.

-- 
Chris Nandor                      pudge@pobox.com    http://pudge.net/
Open Source Development Network    pudge@osdn.com     http://osdn.com/

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org