Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Exploiting a post with this question, as somebody who knows Perl 5, I'd like to ask people that knows Perl 6:

Will I be able to write a dictionary of dictionaries or arrays without looking at the documentation every time? Did Perl 6 syntax for references got sane?



I totally understand your question, as I was struggling to do this kind of things in Perl 5 some time ago. I hear that it's been made easier in Perl 5 lately, though. In any case, it's indeed much easier in Perl 6, as in Perl 6, there is no more references, or rather, everything is a reference.

so for instance a dictionary of dictionaries or arrays looks like:

    my %dict = {
       'foo' => {
                    'bar' => [ 'an', 'array' ],
                    'a deeply nested word' => "tada!"
               },
       'an empty dict' => {},
       'an empty array' => []
    };
    say %dict{'foo'}{'a deeply nested word'};  # tada!


Little syntax error: for hashes, you would say:

    my %hash = (
    # your key/values
    );
and not,

    my %hash = {
   # your key/values
    };
I think that may be one of the problems people have with Perl - the syntax gets a little whacky and it's hard, sometimes, to wrap your mind around Perl-style references.

My personal way to solve this is to almost exclusively use references in complex data structures, so:

		my $hashref = { 
		another_hashref => { 
			key_name => [qw(
				value
				is
				an 
				array_ref
			)], 
		},
	};
Is a hashref, that contains another hashref, which contains an array ref. Easy to bring up the 3rd thingy in the arrayref:

    print $hashref->{another_hashref}->{key_name}->[2]; #an
This should be somewhat palatable to do actually, if you're slinging JSON all day.


Oh I was sure the syntax was ok in both forms. I'm guessing it's a GLR revision. It's indeed better to to reserve {} for scalar containers.

Notice how in Perl 6 you get a nice error message:

    $ perl6 -e 'my %h = { foo => "bar" };'
    Potential difficulties:
        Useless use of hash composer on right side of hash assignment;
        did you mean := instead?
        at -e:1
        ------> my %h = { foo => "bar" }⏏;


That's good, because in Perl 5, it does DWIM, but that sometimes means returning undef, which - erm, thanks Perl ;)


I really don't have any idea what you are talking about..

Are you talking about something like this:

  $hash_of_hash = {
    hash1 => {key1 => 'value1'},
    hash2 => {key1 => 'value1'}
  };
  say $hash_of_hash->{hash1}{key1};
Seems easy to me...

I think reference syntax in Perl6 is gone. Which personally I don't like.


I imagine the gripe has something to do with either hash/array sigil variance, that you assign to hashes and arrays with lists (as opposed to ref constructors as you've done), or both.

Both are cleared up by really understanding context as it applies in Perl 5, which is probably the single largest difference to other languages that is (sometimes unfortunately) easy to get along without understanding because of the DWIMiness of the language. Or references, but I tend to think most people should be able to understand them without too much trouble.


As an example, this won't run:

%hash = {key1 => 'value1'}

$hash_of_hash->{hash1} = %hash

Now, this will work (or something like it, I don't remember the syntax):

%hash = {key1 => 'value1'}

$hash_of_hash->{hash1} = \%hash

The syntax makes a lot of sense when you read the specs. It does create lots of problems when you try to create complex data structures in practice. It's also complex enough (with enough corner cases) that I can't remember it if I don't use it every week - but maybe that's just me.

By the way, I don't remember whether I must read the above structure as:

$hash_of_hash->{hash1}{key1}

or

$($hash_of_hash->{hash1}){key1}

I also don't remember if it is equal to the one you defined. I know it's in the documentation, and it's very clear there. It's just some corner case that I never bothered to memorize for any long time.


The quick way to remember this is that hashes naturally consume and supply lists. When a hash consumes a list, it alternates to assigning list items as keys and values. When a hash is supplied on the right hand side of an assignment, it's keys and values are supplies as a list in alternating order. Values for a hash or array must be a regular variable (scalar), or a reference to some item (such as an object, hash, array or scalar).


It also helps to know that '=>' is really just syntactic sugar for a comma that also implicitly quotes the word to its left, and not a special operator for hashes. These 2 lines are the same (in Perl 5):

    %hash = (hello => 'there');
    %hash = ('hello', 'there');
This is also perfectly valid (though non-idiomatic) Perl 5:

    print Hello => ' ' => there => "\n";


Yeah, I had quite a bit more in my reply originally, including that and code snippets with assignment back and forth between a hash and array, but decided I didn't need to turn this article into a Perl 5 tutorial ;)


That depends on the person. I imagine anyone who looks at the docs for a few minutes would have a pretty good idea for themselves




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: