T
Tim McDaniel
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.
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.