Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Strange Occurrence in Substitution Statement

by Corion (Patriarch)
on May 06, 2025 at 18:54 UTC ( [id://11164965]=note: print w/replies, xml ) Need Help??


in reply to Strange Occurrence in Substitution Statement

See Perlop on 'the empty pattern':

If the PATTERN evaluates to the empty string, the last successfully matched regular expression in the current dynamic scope is used instead
  • Comment on Re: Strange Occurrence in Substitution Statement

Replies are listed 'Best First'.
Re^2: Strange Occurrence in Substitution Statement
by choroba (Cardinal) on May 06, 2025 at 18:59 UTC
    And a possible solution:
    $fname =~ s/(?:$extra)//;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thank you for the possible solution Choroba! I tried it and it works. It looks like the non-capturing group prevents the last successfully matched regular expression from being used when the pattern is empty.

      "It's not how hard you work, it's how much you get done."

        I think it's simpler, the pattern isn't empty, because (?:...) is always part of the pattern. Meta characters count too.

        This idiom is meant to be used as s//something/ in complicated parsing, kind of inherited from sed/awk IIRC.

        You should also be aware that a genuinely empty pattern IS always matching. In your case the replacement is just invisible because empty.

        So I'd rather prefer to make this explicit and self documenting

      • s/$extra// if $extra

        Nowadays I doubt there are more than a fraction of a percent of programmers knowing/using this feature.

        I'd really like to see a Perl version/feature to switch it off by default.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

Re^2: Strange Occurrence in Substitution Statement
by roho (Bishop) on May 06, 2025 at 19:15 UTC
    Thank you for the reference and the explanation Corion!

    "It's not how hard you work, it's how much you get done."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11164965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2025-06-16 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.