Dear monks, I saw the question Regexp for alphabetical order match within the string this morning. So I decided to give it a try, and came up with the following solution with matching time interpolation (??{ ... }) inside the regular expression -

use strict; my $seq1 = 'aabcdefg'; my $seq2 = 'aabcdefgabcde'; sub in_sequence { my $str = shift; # fetch the string value my $ok = 1; # set private variable $ok = 1 $str =~ m/(.)(.)(??{ $ok = 0 if $2 lt $1; 0 })/; return $ok; } print "Sequence is ", in_sequence($seq1)?"good":"bad", "\n"; print "Sequence is ", in_sequence($seq2)?"good":"bad", "\n";
What I want to achieve in subroutine in_sequence is as follows:
  • get the string value;
  • assign value 1 to my variable $ok;
  • in the regular expression, I fetch two characters at a time, and set $ok to 0 if the 2nd character is less than the 1st character;
  • return the value of $ok to the caller.

    However I am not getting the expected result. The values returned by the subroutine for $str1 and $str2 are both true.

    So I expanded the regular expression and inserted a few print statements. The debug version of the code is as follows:
    use strict; my $seq1 = 'aabcdefg'; my $seq2 = 'aabcdefgabcde'; sub in_sequence_debug { my $str = shift; my $ok = 1; print "Value of OK is $ok\n"; $str =~ m/(.)(.)(??{ print "$1 - $2 "; if ($2 lt $1) { $ok = 0; print "Not Ok\n" } else { print "Ok\n"; }; 0 })/; print "Value of OK is $ok\n"; return $ok; } print "Sequence is ", in_sequence_debug($seq1)?"good":"bad", "\n"; print "-"x40, "\n"; print "Sequence is ", in_sequence_debug($seq2)?"good":"bad", "\n";
    When I ran the code, I got the following output:
    Value of OK is 1 a - a Ok a - b Ok b - c Ok c - d Ok d - e Ok e - f Ok f - g Ok Value of OK is 1 Sequence is good ---------------------------------------- Value of OK is 1 a - a Ok a - b Ok b - c Ok c - d Ok d - e Ok e - f Ok f - g Ok g - a Not Ok a - b Ok b - c Ok c - d Ok d - e Ok Value of OK is 1 Sequence is good
    It seems that the value of my $ok has not been modified by the code $ok = 0 inside (??{ ... }).

    This suddenly made me curious and created the following test code to investigate the scoping of the variable $ok -
    use strict; my $seq1 = 'aabcdefg'; my $seq2 = 'aabcdefgabcde'; my $ok; # moved up to the file scope sub in_sequence_debug { my $str = shift; $ok = 1; print "Value of OK is $ok\n"; $str =~ m/(.)(.)(??{ print "$1 - $2 "; if ($2 lt $1) { $ok = 0; print "Not Ok\n" } else { print "Ok\n"; }; 0 })/; print "Value of OK is $ok\n"; return $ok; } print "Sequence is ", in_sequence_debug($seq1)?"good":"bad", "\n"; print "-"x40, "\n"; print "Sequence is ", in_sequence_debug($seq2)?"good":"bad", "\n";
    And this time, the output becomes -
    Value of OK is 1 a - a Ok a - b Ok b - c Ok c - d Ok d - e Ok e - f Ok f - g Ok Value of OK is 1 Sequence is good ---------------------------------------- Value of OK is 1 a - a Ok a - b Ok b - c Ok c - d Ok d - e Ok e - f Ok f - g Ok g - a Not Ok a - b Ok b - c Ok c - d Ok d - e Ok Value of OK is 0 Sequence is bad
    It actually worked when I move the $ok variable up one level to the file scope!

    It seems that something odd is happing to the variable scoping inside the match-time interpolation (??{ ... }), the variable assignment wrapped inside has no effect on my variables inside the subroutine, probably creates its own private variable, even when I do not explicitly declare them with my.

    Can anyone please explain to me why, by moving my $ok up one level into file scope, made the assignment $ok = 0 inside (??{ ... }) to work all the sudden?

    Thanks in advance!


    In reply to Variable scoping oddity inside (??{ ... }) by Roger

    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.