develooper Front page | perl.perl5.porters | Postings from July 2015

Re: scalar sort $scalar

Thread Previous | Thread Next
From:
David Nicol
Date:
July 9, 2015 19:29
Subject:
Re: scalar sort $scalar
Message ID:
CAFwScO-5UpZOGojoRUGa9g2J-p5UZpeeO8jukMPq3+saYR9Z6A@mail.gmail.com
I optimistically understand Ed Avis's l-value split proposal to essentially
be a rewrite rule accepting

    split $lsep, $lstr = @array

and emitting

     $lstr = join $lsep, @array

which is no gain at all.

More usefully, with more than two arguments,

   @{ split $lsep, $lstr }[@indexes] = @array;

a/k/a

    l_split( $lsep, $lstr, @indexes ) = @array;

could transform into

    $lstr = do {
         my @tmp = split $lsep, $lstr;
         for my $tmp_i (@indexes) {
               $tmp[$tmp_i] = $array[$tmp_i]
         };
         join $lsep, @tmp
    };

with

   ${ split $lsep, $lstr }[$index] = $scalar

possibly working as a side effect.

clearly this would not be out of place on CPAN as something that exports
an lvalue sub l_split($$;@). The challenging part would be designing a
array tie class
to be returned by l_split to accept @array and replace $_[0] after the
expected single SLICE
operation or whatever actually happens to the returned l-value. Has the
series of operations
applied to tied arrays returned from l-value subroutines changed over the
versions?



On Thu, Jul 9, 2015 at 5:22 AM, Ed Avis <eda@waniasset.com> wrote:

> Abigail <abigail <at> abigail.be> writes:
>
>
> >>How about making split an lvalue?
>
> >>    split(';', $str) = split(':', $str); # change separator
>
> >And for changing separator, what's the benefit of using the proposed
> >split() syntax over
> >
> >    $str =~ s/;/:/g;
>
> I imagine that the split-as-lvalue assignment would fail at run time if any
> of the list elements contained the ; character.  So you would not be able
> to
> accidentally end up with more elements than you expected because of
> insufficiently sanitized input.  If you assign to split(x, y) then that
> split call would always return the same list you assigned to it, until the
> string is modified.  That would make it slightly safer than s///.
>
> --
> Ed Avis <eda@waniasset.com>
>
>


-- 
Automated spelling checkers inhibit natural orthographic drift

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About