develooper Front page | perl.perl5.porters | Postings from June 2013

Re: key/value hash and index/valuse array slices syntax

Thread Previous | Thread Next
Ruslan Zakirov
June 11, 2013 12:01
Re: key/value hash and index/valuse array slices syntax
Message ID:
On Mon, Jun 10, 2013 at 2:27 PM, demerphq <> wrote:

> On 3 June 2013 14:06, Ruslan Zakirov <> wrote:
> > HI,
> >
> > I've updated patches that add key/value hash slices to perl syntax:
> >
> > my %sub_hash = %hash{qw(some keys go here)};
> Sounds cool.
> However note that it is legal, but deprecated to write this:
> $ perl -le'my $hash={foo=>"bar"}; print %$hash->{foo};'
> Using a hash as a reference is deprecated at -e line 1.
> bar
> How does your patch impact this behavior?

I was only concentrated on making the following work as key/value slices:

%{ $hash_ref }{qw(.....)}

I should write tests for this.

Syntax you mentioned is grey area and I will do whatever people decide, but
I think it should be parsed in very same way as the following code:

$ perl -le'my $hash={foo=>"bar"}; print @$hash->{foo};'

Correct way to write what you want is %$hash_ref{'foo', 'bar'} and it
should work already. Note for myself to write tests for the case.

I have "quest" [1] on subject of working with references, but it's
completly different story. My goal at this moment is to make key/value
slices fully aligned with how existing slice ops behave.


> Also what happens if I do:
> my $sub_hash= %hash{thing};
> does $sub_hash end up with the value 2 if "thing" existed, and with
> the value 0 if it didn't?

It end ups with value, not 2 or 0 and produce a warning (%hash{'thing'} is
better written as $hash{'thing'}). As aslice and hslice, kvhslice returns
list, so last element is assigned to $sub_hash which is value. I hate this
"list" thing, but I'm for consistency.

If someone does this should it warn?

It does already. Should it warn in array context? It's still open question.
Warns in the same style as aslice or hslice, so can be workarounded.

> > This feature was discussed [1] back in Feb with Brad Gilbert. We agreed
> that
> > for consistency this should work with arrays too as index/value array
> slice.
> > I've added array functionality. Also, wrote a few basic tests.
> >
> > Main question is whether should I continue or stop right there as it
> never
> > will be in the core?
> >
> > A quick review of the code [2] would be helpful too.
> >
> > [1]
> > [2]
> Thanks for working on this Ruslan!
> Yves
> --
> perl -Mre=debug -e "/just|another|perl|hacker/"

Best regards, Ruslan.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About