Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using a backref for arity

Reply
Thread Tools

Using a backref for arity

 
 
Akim Demaille
Guest
Posts: n/a
 
      09-29-2010
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.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-29-2010
Akim Demaille wrote:
>
> 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
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
 
 
 
Akim Demaille
Guest
Posts: n/a
 
      09-29-2010
On Sep 29, 12:18*pm, "John W. Krahn" <jwkr...@example.com> wrote:
> Akim Demaille wrote:
>
> > 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


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.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-29-2010
On Wed, 29 Sep 2010 03:04:14 -0700 (PDT), Akim Demaille <> wrote:

>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
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      09-30-2010
On 2010-09-29, Akim Demaille <> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting the arity of a constructor at compile time Michael Feathers C++ 3 08-04-2006 04:18 PM
overloading on the template parameter arity of a template templateparameter Howard Gardner C++ 4 07-19-2006 11:10 PM
help: negative lookahead and backref in regex? stenor@bayarea.net Perl Misc 2 12-28-2004 11:59 PM
Regexp backref w/o consumption? trans. (T. Onoma) Ruby 0 11-13-2004 02:34 PM
Regex backref returns extra data Dan Rawson Perl Misc 3 09-25-2003 06:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57