Using a backref for arity

A

Akim Demaille

Hi all,

The following does not work. This was expected, but since pre is
sometimes surprisingly powerful, I meant to make sure. The idea is to
handle with the s operator strings of x's preceded by their number
(and for instance to replace them with X). For instance "{2}xxx"
should give "XXx". So I tried this:

echo "{2}xxx" | perl -p -e 's/\{(\d+)\}x{\1}/"X" x $1/ge

Sure it does not work (I get the original string back). Yet, I would
have expected Perl to complain about my use of the backref as an
argument for the arity (this is 5.10.0). What the heck did it
understand?

Thanks in advance.
 
J

John W. Krahn

Akim said:
The following does not work. This was expected, but since pre is
sometimes surprisingly powerful, I meant to make sure. The idea is to
handle with the s operator strings of x's preceded by their number
(and for instance to replace them with X). For instance "{2}xxx"
should give "XXx". So I tried this:

echo "{2}xxx" | perl -p -e 's/\{(\d+)\}x{\1}/"X" x $1/ge

$ echo "{2}xxx" | perl -p -e 's/\{(\d+)\}(x+)/substr $a = $2, 0, $1, "X"
x $1; $a /ge'
XXx



John
 
A

Akim Demaille

$ echo "{2}xxx" | perl -p -e 's/\{(\d+)\}(x+)/substr $a = $2, 0, $1, "X"
x $1; $a /ge'
XXx

Thanks for this. I have to add a check to make sure that we don't
overrun the available space (see for instance:

$ echo "{4}xx" | perl -p -e 's/\{(\d+)\}(x+)/substr $a = $2, 0, $1,
"X" x $1; $a /ge'
XXXX

)

I know I can do that, no problem. But still, I am really curious to
know what Perl understood here, since it did not complain about this
weird arity argument.
 
S

sln

Hi all,

The following does not work. This was expected, but since pre is
sometimes surprisingly powerful, I meant to make sure. The idea is to
handle with the s operator strings of x's preceded by their number
(and for instance to replace them with X). For instance "{2}xxx"
should give "XXx". So I tried this:

echo "{2}xxx" | perl -p -e 's/\{(\d+)\}x{\1}/"X" x $1/ge

Sure it does not work (I get the original string back). Yet, I would
have expected Perl to complain about my use of the backref as an
argument for the arity (this is 5.10.0). What the heck did it
understand?

Thanks in advance.

Using Perl 5.10.0 --

Windows:
echo "{2}xxx" | perl -p -e "s/\{(\d+)\}(??{\"x{$1}\"})/\"X\" x $1/ge"

Nix (untested):
echo "{2}xxx" | perl -p -e 's/\{(\d+)\}(??{"x{$1}"})/"X" x $1/ge'

From perlre:
(??{ code })
WARNING: This extended regular expression feature is considered experimental,
and may be changed without notice. Code executed that has side effects may
not perform identically from version to version due to the effect of future
optimisations in the regex engine.

This is a "postponed" regular subexpression. The code is evaluated at
run time, at the moment this subexpression may match. The result of evaluation
is considered as a regular expression and matched as if it were inserted
instead of this construct. Note that this means that the contents of capture
buffers defined inside an eval'ed pattern are not available outside of the
pattern, and vice versa,
there is no way for the inner pattern to refer to a capture buffer defined outside.
---------------------------------

" there is no way for the inner pattern to refer to a capture buffer defined outside. "
^^^
Strangely enough, in this example, the inner pattern refers to an outside capture buffer.
So the feature may have changed thus, validating "may be changed without notice" ...

Expanded:
s/ \{ (\d+) \} (??{ "x{$1}" }) /"X" x $1/xge


-sln
 
I

Ilya Zakharevich

Hi all,

The following does not work. This was expected, but since pre is
sometimes surprisingly powerful, I meant to make sure. The idea is to
handle with the s operator strings of x's preceded by their number
(and for instance to replace them with X). For instance "{2}xxx"
should give "XXx". So I tried this:

echo "{2}xxx" | perl -p -e 's/\{(\d+)\}x{\1}/"X" x $1/ge

Sure it does not work (I get the original string back). Yet, I would
have expected Perl to complain about my use of the backref as an
argument for the arity (this is 5.10.0). What the heck did it
understand?

This is a horrible backward-compatibility hack (I suspect introduced
about Perl3 for Perl2-compatibility - or somesuch). If it is not in
EXACTLY the documented syntax, { is interpreted as a literal.

Yours,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top