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

[MacPerl] Callbacks not reliable?



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!?
  $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";
}

Should I always be checking the return values for callbacks? I don't recall
ever having to do this.

-K

"Do not meddle in the affairs of dragons, because you are crunchy and taste
good with ketchup."



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