in reply to Assigning a varable inside of (?{})
This is a cool little bug in your script based on the fact that the first time a regexp is compiled, $sunrise and $sunset are, in effect, locked in. Subsequent calls to get_time() create new lexical $sunrise and $sunset variables, but the regexp is still using the original (unreachable to you) $sunrise and $sunset from the first get_time() call. One solution is to use globals for this situation. ...or rethink your regexp strategy.
Observe the behavior of the following snippet, and take notice of the fact that on the second call to get_time() the reference to $sunrise printed within the regexp is a different reference than the reference to $sunrise printed outside the regexp. This supports the claim that the regexp got compiled once, the first time, locking in the first $sunrise, while each call to get_time() created a new lexical that the regexp simply wasn't using:
use strict; use warnings; while (<DATA>) { my ( $sunrise, $sunset ) = get_time($_); print "Sunrise is:", ${$sunrise}, "Sunset is:", ${$sunset}, "\n"; } sub get_time { my ($string) = @_; my ($sunrise, $sunset); $string =~ /sunrise:\s+(\d+:\d+)(?{ $sunrise = $^N; print \$sunris +e, qq!\n!;}) \s+sunset:\s+(\d+:\d+)(?{$sunset = $^N})/x print \$sunrise, "\n"; return ( \$sunrise, \$sunset ); } __DATA__ Aberdeen, Scotland 57 9 N 2 9 W sunrise: 03:12 sunset: 21:08 Adelaide, Australia 34 55 S 138 36 E sunrise: 06:52 sunset: 16:41
Also, you ought to be checking to ensure that you actually had a successful match before trusting and assuming that you did. It's not the issue in this particular case, but could become an issue silently, without you ever realizing it since you're not checking.
...by the way, Perl is behaving as documented.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Assigning a varable inside of (?{})
by ikegami (Patriarch) on May 03, 2006 at 17:52 UTC | |
|
Re^2: Assigning a varable inside of (?{})
by Jasper (Chaplain) on May 03, 2006 at 16:43 UTC |