[tpm] New Regex question - partially solved
Uri Guttman
uri at stemsystems.com
Sun Oct 28 13:59:47 PDT 2012
On 10/28/2012 12:35 PM, Rob Janes wrote:
> Sigh.
>
> Regarding the accusation that I did not read the docs. Here's an excerpt
> from man perlre (the highlights are mine):
>
> Warning on \1 Instead of $1
> Some people get too used to writing things like:
>
> $pattern =~ s/(\W)/\\\1/g;
>
> This is *grandfathered* (for \1 to \9) for the RHS of a substitute
> to avoid shocking the sed addicts, but it's
i said it was allowed but not considered good code.
> a dirty habit to get into. That's because in PerlThink, the
> righthand side of an "s///" is a double-quoted
> string. "\1" in the usual double-quoted string means a control-A.
> The customary Unix meaning of "\1" is
> kludged in for "s///". However, if you get into the habit of doing
> that, you get yourself into trouble if
> you then add an "/e" modifier.
>
> s/(\d+)/ \1 + 1 /eg; # causes warning under -w
>
> Or if you try to do
>
> s/(\d+)/\1000/;
>
> You can't disambiguate that by saying "\{1}000", whereas you can fix
> it with "${1}000". The operation of
> interpolation should not be confused with the operation of matching
> a backreference. Certainly they mean
> two different things on the left side of the "s///".
>
> So you see, \1 in the replace is not deprecated, it is grandfathered.
> Everybody, feel free to keep using it, although it's use may cause
> reviewers of your code to point and stare, perhaps gesticulate.
which is just as bad as being deprecated. i review code for my
perlhunter business and that is something i will always note. there is
NO reason to use \1 in the replacement when $1 is the same value and
works correctly in all situations. just because something may work
doesn't make it good code.
>
> stylistically, I'd say if you're using \1 on the match side, don't switch
> to $1 on the replace side. It's confusing.
and that is way off base too. $1 is the standard way to access a grab
both in the replacement and afterwards.
>
> As for being greatly mistaken about \1 and $1, I think not. \1 and $1 are
> slightly different beasts on the match side of an s/// regex. They both
> work, and are not deprecated. On the replace side, \1 and $1 are
> equivalent, there is no deprecation of \1. I've made specific statements
> about how \1 and $1 behave on the match side so as to better understand
> their differentiation. I've posted tested examples as well, and snippets
> of the documentation. So, Uri, what exactly is it that I'm greatly
> mistaken about?
what you just said and what the docs suggest. you keep doing your style.
it will not help you get a better perl job if you are stubborn about
improving your style. and yes, style matters a great deal in quality code.
uri
More information about the toronto-pm
mailing list