Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Thanks vr for the answer!

Your answer has made me understand why, I several times "found" limitations, on what can be an argument to Regexp Quote Like Operators.

The (useless here) "c" modifier in combination with other errors have fooled me. I have also erroneously thought that the string used in $str =~ m{$pat} could not change during the match.

The error message "Use of uninitialized value in pattern match (m//)" has made me believe that the input was the problem. Not the value after one or more matches.

use strict; use warnings; use 5.010; use Data::Dump qw(dump dd ddx); use Test::More; sub match { # $pat, $str_ref warn "\nmatch", dump @_; my $pat = $_[0]; my $str_ref = $_[1]; my @res; say STDERR '$str_ref before loop: ', dump $str_ref; while ( my $rv = $$str_ref =~ m{$pat}g ) { say STDERR 'in loop: ', dump $str_ref, $rv, @{^CAPTURE}; push @res, @{^CAPTURE}; } say STDERR 'match: ', ( dump @res ), "\n"; return @res; } sub spliT { # $pat, $str_ref warn "\nspliT", dump @_; my @rv = split $_[0], ${ $_[1] }; say STDERR 'split: ', ( dump @rv ), "\n"; return @rv; } $_ = 'ERROR?'; /(.+)/; # set $1 my $match_OK = [ ':', ':' ]; my $split_OK = [ "a", ":", "b", ":", "c" ]; is_deeply [ match( qr{(:)}, \'a:b:c' ) ], [ ':', ':' ], q{\'a:b:c'}; my $dollar1_ref; { my $str = 'a:b:c'; $_ = $str; /(.+)/; $dollar1_ref = \$1; warn "\n\$dollar1_ref: ", dump $dollar1_ref; is_deeply [ match( qr{(:)}, $dollar1_ref ) ], $match_OK, q{$dollar +1_ref}; warn "NOK because \$\$dollar1_ref is changed by m{}\n"; warn "\n\$dollar1_ref: ", dump $dollar1_ref; is_deeply [ spliT( qr{(:)}, $dollar1_ref ) ], $split_OK, q{spliT( qr{(:)}, $dollar1_ref)}; warn "\n\$dollar1_ref: ", dump $dollar1_ref; warn "\n ===== leave scope"; } warn "\n\$dollar1_ref: ", dump $dollar1_ref; # $dollar1_ref = \undef # is_deeply [ match( qr{(:)}, $dollar1_ref ) ], $match_OK, q{$dollar1_ +ref}; # is_deeply [ spliT( qr{(:)}, $dollar1_ref ) ], $split_OK, # q{spliT( qr{(:)}, $dollar1_ref}; done_testing(); __DATA__ output: match(qr/(:)/, \"a:b:c") at pm_5.pl line 17. $str_ref before loop: \"a:b:c" in loop: (\"a:b:c", 1, ":") in loop: (\"a:b:c", 1, ":") match: (":", ":") ok 1 - \'a:b:c' $dollar1_ref: \"a:b:c" at pm_5.pl line 51. match(qr/(:)/, \"a:b:c") at pm_5.pl line 17. $str_ref before loop: \"a:b:c" in loop: (\":", 1, ":") match: ":" not ok 2 - $dollar1_ref # Failed test '$dollar1_ref' # at pm_5.pl line 53. # Structures begin differing at: # $got->[1] = Does not exist # $expected->[1] = ':' NOK because $$dollar1_ref is changed by m{} $dollar1_ref: \"a:b:c" at pm_5.pl line 56. spliT(qr/(:)/, \"a:b:c") at pm_5.pl line 31. split: ("a", ":", "b", ":", "c") ok 3 - spliT( qr{(:)}, $dollar1_ref) $dollar1_ref: \"a:b:c" at pm_5.pl line 60. ===== leave scope at pm_5.pl line 61. $dollar1_ref: \"ERROR?" at pm_5.pl line 64. 1..3 # Looks like you failed 1 test of 3.

I am a little bit surprised that split could handle split $_[0], ${ $_[1] };

Thank you very much!!


In reply to Re^2: Split fake, an emulation of split by bojinlund
in thread Split fake, an emulation of split by bojinlund

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2020-10-29 05:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (269 votes). Check out past polls.

      Notices?