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


In reply to Re: Assigning a varable inside of (?{}) by davido
in thread Assigning a varable inside of (?{}) by monsterzero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.