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

Re: [MacPerl] Callbacks not reliable?



At 17:50 -0600 2000.09.14, Keary Suska wrote:
>The problem/quirk I am noticing can be shown with the following change to
>Chris's code below:
>
>  #!perl -wl
>  $_ = 1;
>  foo(1);  # 1
>  foo(2);  # undef
>  /(1)/; print $1;  # 1
>  /(2)/; print $1;  # 1
>  foo(2);
>
>  sub foo {
>     /($_[0])/;
>     print $1;
>  }
>
>I would think that the $1 in Main is in a different scope than the $1 in
>foo. But this does not necessarily seem the case...

When there is a successful match, $1 is set kinda like this:

   local($1) = {MATCH};

Since there never IS a successful match, and $1 already has a value outside
the subroutine, that value is used.

The best way to deal with it is through using a conditional:

  sub foo {
     print $1 if /($_[0])/;
  }

Or:

  if ($num =~ s/(\.\d+)//) {        # remove decimal
     $dec = $1;
  }

-- 
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