Clever implementation for s///r

Discussion in 'Perl Misc' started by Tim McDaniel, Apr 16, 2012.

  1. Tim McDaniel

    Tim McDaniel Guest

    So I read that Perl 5.14 (and maybe earlier?) has this nice feature
    s///r:

    If the /r (non-destructive) option is used then it runs the
    substitution on a copy of the string and instead of returning the
    number of substitutions, it returns the copy whether or not a
    substitution occurred. The original string is never changed when
    /r is used. The copy will always be a plain string, even if the
    input is an object or a tied variable.

    and similarly for tr///r.

    So I gather that

    my @newargs = map { split ' ', tr/;,/ /r } @_;

    would split each element of @_ at semicolon/comma/whitespace (multiple
    delimiters in a row are the same as one, leading/trailing delimiters
    are ignored), but not change @_.

    Alas that I have Perl 5.8.8 at one place, and it knoweth not s///r and
    tr///r. Is there any clever way to implement it? I can see no
    way other than to declare a variable and assign to it, like

    my @t = @_;
    my @newargs = map { tr/;,/ /; split } @t;

    or

    my @newargs = map { my $t = $_; $t =~ tr/;,/ /; split(' ', $t) } @_;

    or even -- Lord forfend --

    my @newargs = map { local $_ = $_; tr/;,/ /; split } @_;

    none of which are as nice (and that last, while legal, would probably
    make my cow-orkers break out in hives and bring out pitchforks. Note
    that "my $_" wasn't possible in 5.8.8).

    Is there an efficient decent way to produce a copy of an array like @_
    and break the magic link back to the original? I ask because I had
    been thinking that one way to do it would be if
    (@_)
    or
    +@_
    would produce a temp copy of @_, but they don't. And
    reverse reverse @_
    is just too cutesy.

    --
    Tim McDaniel,
    Tim McDaniel, Apr 16, 2012
    #1
    1. Advertising

  2. (Tim McDaniel) writes:
    > So I read that Perl 5.14 (and maybe earlier?) has this nice feature
    > s///r:
    >
    > If the /r (non-destructive) option is used then it runs the
    > substitution on a copy of the string and instead of returning the
    > number of substitutions, it returns the copy whether or not a
    > substitution occurred. The original string is never changed when
    > /r is used. The copy will always be a plain string, even if the
    > input is an object or a tied variable.
    >
    > and similarly for tr///r.
    >
    > So I gather that
    >
    > my @newargs = map { split ' ', tr/;,/ /r } @_;
    >
    > would split each element of @_ at semicolon/comma/whitespace (multiple
    > delimiters in a row are the same as one, leading/trailing delimiters
    > are ignored), but not change @_.
    >
    > Alas that I have Perl 5.8.8 at one place, and it knoweth not s///r and
    > tr///r. Is there any clever way to implement it? I can see no
    > way other than to declare a variable and assign to it


    The obvious other idea would be to use a function:

    sub cpy { return @_; }
    Rainer Weikusat, Apr 16, 2012
    #2
    1. Advertising

  3. Tim McDaniel

    Tim McDaniel Guest

    In article <>,
    Rainer Weikusat <> wrote:
    >The obvious other idea would be to use a function:
    >sub cpy { return @_; }


    That does file off the specialness. Thanks! My test was (in a
    different test, not the example purpose what I was asking about)

    my @bpbpbp = map { s/^/f/; $_ } sub{@_}->(@a);

    --
    Tim McDaniel,
    Tim McDaniel, Apr 16, 2012
    #3
  4. Tim McDaniel

    Guest

    On Apr 16, 1:28 pm, (Tim McDaniel) wrote:
    > Alas that I have Perl 5.8.8 at one place, and it knoweth not s///r and
    > tr///r.  Is there any clever way to implement it?  I can see no
    > way other than to declare a variable and assign to it, like
    >
    >     my @t = @_;
    >     my @newargs = map { tr/;,/ /; split } @t;



    How about:

    my @newargs = map { tr/;,/ /; split } @{ [@_] };

    Would this work for you?

    -- Jean-Luc
    , Apr 17, 2012
    #4
  5. Tim McDaniel

    C.DeRykus Guest

    On Monday, April 16, 2012 12:28:34 PM UTC-7, Tim McDaniel wrote:
    > So I read that Perl 5.14 (and maybe earlier?) has this nice feature
    > s///r:
    >
    > If the /r (non-destructive) option is used then it runs the
    > substitution on a copy of the string and instead of returning the
    > number of substitutions, it returns the copy whether or not a
    > substitution occurred. The original string is never changed when
    > /r is used. The copy will always be a plain string, even if the
    > input is an object or a tied variable.
    >
    > and similarly for tr///r.
    >
    > So I gather that
    >
    > my @newargs = map { split ' ', tr/;,/ /r } @_;
    >
    > would split each element of @_ at semicolon/comma/whitespace (multiple
    > delimiters in a row are the same as one, leading/trailing delimiters
    > are ignored), but not change @_.
    >
    > Alas that I have Perl 5.8.8 at one place, and it knoweth not s///r and
    > tr///r. Is there any clever way to implement it? I can see no
    > way other than to declare a variable and assign to it, like
    >
    > my @t = @_;
    > my @newargs = map { tr/;,/ /; split } @t;
    >
    > or
    >
    > my @newargs = map { my $t = $_; $t =~ tr/;,/ /; split(' ', $t) } @_;
    >
    > or even -- Lord forfend --
    >
    > my @newargs = map { local $_ = $_; tr/;,/ /; split } @_;
    >
    > none of which are as nice (and that last, while legal, would probably
    > make my cow-orkers break out in hives and bring out pitchforks. Note
    > that "my $_" wasn't possible in 5.8.8).
    >
    > Is there an efficient decent way to produce a copy of an array like @_
    > and break the magic link back to the original? I ask because I had
    > been thinking that one way to do it would be if
    > (@_)
    > or
    > +@_
    > would produce a temp copy of @_, but they don't. And
    > reverse reverse @_
    > is just too cutesy.
    >


    my @newargs = map { tr/;,/ /; split } qq{@_}

    Still cute and probably dodgy but brief :)

    --
    Charles DeRykus
    C.DeRykus, Apr 17, 2012
    #5
  6. Tim McDaniel

    Tim McDaniel Guest

    In article <18524419.1360.1334646956189.JavaMail.geo-discussion-forums@vbgx21>,
    C.DeRykus <> wrote:
    >my @newargs = map { tr/;,/ /; split } qq{@_}
    >
    >Still cute and probably dodgy but brief :)


    I've never before reacted to a comp.lang.perl.* posting with LOLing!

    Yeah, dodgy -- it depends on $" alias $LIST_SEPARATOR being a space
    (the default) or at worst ;,space. But if you guarantee that, and you
    REALLY shouldn't be changing it globally anyway, Bob's your uncle.

    --
    Tim McDaniel,
    Tim McDaniel, Apr 17, 2012
    #6
    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. -
    Replies:
    3
    Views:
    944
    Grant Wagner
    Jul 15, 2003
  2. Brian J. Sayatovic

    Clever serialization broken by CORBA RMI

    Brian J. Sayatovic, Sep 30, 2004, in forum: Java
    Replies:
    0
    Views:
    360
    Brian J. Sayatovic
    Sep 30, 2004
  3. Replies:
    14
    Views:
    660
  4. Konrad Billewicz

    Clever export from MySQL

    Konrad Billewicz, Aug 16, 2005, in forum: Java
    Replies:
    0
    Views:
    365
    Konrad Billewicz
    Aug 16, 2005
  5. Anonymous user
    Replies:
    2
    Views:
    375
Loading...

Share This Page