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
    One solution is to use globals for this situation.

    Yes, global (lexical or package) variables will do the trick, but localized package variables would be better:

    sub get_time { my ($string) = @_; local our ($sunrise, $sunset); $string =~ / sunrise: \s+ (\d+:\d+) (?{ $sunrise = $^N }) \s+ sunset: \s+ (\d+:\d+) (?{ $sunset = $^N }) /x; return ( \$sunrise, \$sunset ); }
    ...or rethink your regexp strategy.

    I think this would be the proper course of action here. Using (?{...}) is overkill, and using $^N imposes a requirement for Perl v5.8.0+ needlessly. The OP could use the following simple expression:

    sub get_time { my ($string) = @_; my ($sunrise, $sunset) = $string =~ / sunrise: \s+ (\d+:\d+) \s+ sunset: \s+ (\d+:\d+) /x; return (\$sunrise, \$sunset); }

    By the way, why return references to the values? return ($sunrise, $sunset); would make more sense.

Re^2: Assigning a varable inside of (?{})
by Jasper (Chaplain) on May 03, 2006 at 16:43 UTC
    As Davido says, moving them to be globals (I'm presuming changing the my declaration to an our solves this in the same way), you could also wrap the regex in an eval. This will force it to be compiled fresh every time, and you'll be able to use your block scoped variables as you have them.

    (edit) Hmmm, having tested this, it doesn't quite work as I expected. An eval block doesn't work, but evalling that line as a (single-quoted) string does work. Shrug.