Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others goofing around in the Monastery: (5)
    As of 2024-03-29 10:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found