Using a backref for arity

Discussion in 'Perl Misc' started by Akim Demaille, Sep 29, 2010.

  1. 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.
     
    Akim Demaille, Sep 29, 2010
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Sep 29, 2010
    #2
    1. Advertising

  3. On Sep 29, 12:18 pm, "John W. Krahn" <> 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.
     
    Akim Demaille, Sep 29, 2010
    #3
  4. Akim Demaille

    Guest

    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
     
    , Sep 29, 2010
    #4
  5. 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
     
    Ilya Zakharevich, Sep 30, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard Gardner
    Replies:
    4
    Views:
    368
    Howard Gardner
    Jul 20, 2006
  2. Michael Feathers
    Replies:
    3
    Views:
    296
    Roland Pibinger
    Aug 4, 2006
  3. trans.  (T. Onoma)

    Regexp backref w/o consumption?

    trans. (T. Onoma), Nov 13, 2004, in forum: Ruby
    Replies:
    0
    Views:
    105
    trans. (T. Onoma)
    Nov 13, 2004
  4. Dan Rawson

    Regex backref returns extra data

    Dan Rawson, Sep 25, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    170
    Uri Guttman
    Sep 25, 2003
  5. Replies:
    2
    Views:
    213
    Anno Siegel
    Dec 29, 2004
Loading...

Share This Page