Patrick R. Michaud wrote:
> On Tue, Aug 26, 2008 at 01:28:22AM -0700, Carl Mäsak wrote:
>> r30537:
>> $ ./perl6 -e '$/<a><b>; say "Alive!"'
>> get_pmc_keyed() not implemented in class 'Undef'
>> [...]
>> 
>> Compare Perl 5:
>> 
>> $ perl -Mstrict -we '$_{'a'}{'b'}; print "Alive!\n"'
>> Useless use of hash element in void context at -e line 1.
>> Alive!
> 
> The Perl 5 comparison really isn't applicable here.  In Perl 5, 
> $_{'a'} refers to the %_ variable, which autovivifies as a 
> hash and thus doesn't generate any sort of error.  The 
> "useless use ..." message just comes from using a value in 
> void context, and is unrelated to indexing.  In fact, I get the 
> same warning for an unindexed use of $_ :
> 
>   $ perl -Mstrict -we '$_; print "Alive!\n"'
>   Useless use of a variable in void context at -e line 1.
>   Alive!
> 
> So, let's look at what is happening in Rakudo.  In Perl 6,
> unset variables (such as $/ above) are treated as
> undef, which IIUC should immediately throw an exception 
> when attempting to use them in a value context without
> first having tested for definedness or truth (S04:922).
> Since subscripting is attempting to use the value
> of undef (as an array or hash), I'm thinking the correct
> behavior is to immediately throw an exception here.

Your analysis is certainly right in this case, but note that it doesn't
work in other cases either:

> "a" ~~ /a/; say $<c><d>
get_pmc_keyed() not implemented in class 'Undef'
> my %h; say %h<a><b>
get_pmc_keyed() not implemented in class 'Undef'
> my %h; say %h<a><b> = 2
get_pmc_keyed() not implemented in class 'Undef'

As opposed to Perl 5, the second example should not autovivify %h<a> in
the first case, only in the second one. But neither should throw an error.
To quote S09/Autovivification/:

Autovivification will only happen if the vivifiable path is bound to a
read-write container. Value extraction (that is, binding to a readonly
or copy container) does not autovivify.

And later on brings an example:

    # This is Perl 5 code
    my %hash;
    exists $hash{foo}{bar}; # creates $hash{foo} as an empty hash reference

In Perl 6 these read-only operations are indeed non-destructive:

    my %hash;
    exists %hash<foo><bar>; # %hash is still empty


I think that's very clearly not what is implemented at the moment ;-)

-- 
Moritz Lenz
http://moritz.faui2k3.org/ |  http://perl-6.de/

Reply via email to