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

Re: [MacPerl] Callbacks not reliable?



aksuska@insideflyer.com (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.
>
>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!?


You want this:

sub FormatNum {

  my $num = shift;
  my ($new, $i);

  $num =~ tr/0-9.//cd;        # numeric only
  my ($dec) = $num =~ s/(\.\d+)//;        # remove decimal
  my @nums = split( //, $num );
  ...


As others have pointed out, $1 etc. may have leftover values if the
match fails.


  -------------------                            -------------------
  Ken Williams                             Last Bastion of Euclidity
  ken@forum.swarthmore.edu                            The Math Forum



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