Per that perlop discussion I wrapped // in quotes with 'm' and tried:

perl -le 'print for a .. z' | perl -nle 'if (/d/ .. /h/) { next unles +s "m//"; print }'

and got:

  % perl -le 'print for a .. z' | perl  -nle 'if (/d/ .. /h/) { next unless "m//"; print }'
  d
  e
  f
  g
  h

And then I pedantically did:

for my $c ( 'a'..'z') { next unless ($c =~ /[d-h]/); say $& if $&; }

and got:

  d
  e
  f
  g
  h

I guess I don't understand. Isn't 'd e f g h' what is expected? I've never really trusted one-liners. Brian Foy wrote an interesting piece on SO, to wit:

https://stackoverflow.com/questions/22652393/regex-1-variable-reset

except his example using //; did not work for me. He expected all vars to be cleared but when I ran his code:

# The regex capture variables are only reset on the next successful ma +tch. # This way, Perl saves a lot of time by not affecting variables when m +atches # fail. As such, only use those variables with a guard, to wit: # if ( /abc/ ) { # this tests for /abc/ success and now it's OK t +o use $& # ... # } # Here's an extended demonstration, with a special surprise at the end +: say "First long example...\n"; $_ = 'Hello Perl'; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # successful match /(P)(erl)/; print "First match\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # unsuccessful match /(P)(ython)/; print "Failed capture\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # successful match again /(Pe)(r)(l)/; print "Three captures\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # successful match, fewer captures /(Perl)/; print "One capture\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # successful match, no captures /Perl/; print "No captures\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n"; # successful match, no pattern, special case //; print "No nothing\n"; print "\$1: $1\n\$2: $2\n\$3: $3\n\$&: $&\n\n";

I got:

  $1: 
  $2: 
  $3: 
  $&: 

  First match
  $1: P
  $2: erl
  $3: 
  $&: Perl

  Failed capture
  $1: P
  $2: erl
  $3: 
  $&: Perl

  Three captures
  $1: Pe
  $2: r
  $3: l
  $&: Perl

  One capture
  $1: Perl
  $2: 
  $3: 
  $&: Perl

  No captures
  $1: 
  $2: 
  $3: 
  $&: Perl

  No nothing
  $1: 
  $2: 
  $3: 
  $&: Perl

As you can see in 'No nothing' $& was not cleared for me as it was for him, as he reported in that piece. I don't trust using $n, $`, $& or $' unless I explicitly test for TRUE after the regex executes. Am I being overly paranoid?


In reply to Re: Empty pattern in regex by perlboy_emeritus
in thread Empty pattern in regex by choroba

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.