Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: ${^MATCH} sometimes fails in target of substitution?

by space_monk (Chaplain)
on Jun 16, 2013 at 17:38 UTC ( #1039234=note: print w/replies, xml ) Need Help??


in reply to ${^MATCH} sometimes fails in target of substitution?

perlre states:
As a workaround for this problem, Perl 5.10.0 introduces ${^PREMATCH} , ${^MATCH} and ${^POSTMATCH} , which are equivalent to $` , $& and $' , except that they are only guaranteed to be defined after a successful match that was executed with the /p (preserve) modifier.

Update: The problem you have documented is a bug in releases prior to 5.18.See Re: ${^MATCH} sometimes fails in target of substitution?.

Before 5.18 it appears that ${^MATCH} may only be available AFTER the match/replace. Adding print $(^MATCH} to your both example prints 'bcd' e.g.

perl -e '$x = "abcde"; $x =~ s/b../==${^MATCH}==/p; prin +t "$x Match:${^MATCH}\n"' a====e Match:bcd perl -e '$x = "abcde"; $sub="b.."; $x =~ s/$sub/==${^MATCH}==/p; print + "$x Match:${^MATCH}\n"' a==bcd==e Match:bcd

If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Replies are listed 'Best First'.
Re^2: ${^MATCH} sometimes fails in target of substitution?
by ikegami (Pope) on Jun 17, 2013 at 08:16 UTC

    If you're replacing, there was a successful match. This is a bug.

    Testing reveals this bug has been fixed:

    $ perl -v | grep 'This is' This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-li +nux-thread-multi $ perl -E'$_ = "abcde"; s/b../==${^MATCH}==/p; say' a====e $ perl -E'$_ = "abcde"; $sub="b.."; s/$sub/==${^MATCH}==/p; say' a==bcd==e
    $ perl -v | grep 'This is' This is perl 5, version 18, subversion 0 (v5.18.0) built for x86_64-li +nux-thread-multi $ perl -E'$_ = "abcde"; s/b../==${^MATCH}==/p; say' a==bcd==e $ perl -E'$_ = "abcde"; $sub="b.."; s/$sub/==${^MATCH}==/p; say' a==bcd==e

      Thanks, I amended my answer to reflect your comment and the earlier one of dave_the_m.

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039234]
help
Chatterbox?
[holli]: but then you DO give a fuck
[1nickt]: I don;t think beliefs should be down-voted, just behaviours.
[james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....
[james28909]: some kind of logic behind it
[james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
[holli]: here's something for you to watch, James. I think you will like it
[erix]: for the record: I have not downvoted anyone on that subthread that was my fault
[james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
[james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
[erix]: teleology -- I've never understood why that was thunk up

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2017-12-15 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?