in reply to Print the contents of a hash without looping.

Hi

> say "@{[%hash]}";

> since it appears to be dereferencing an array.

That's because the [...] creates an anonymous array ref and the %hash is expanded to a list inside.

I'd say this is cargo cult, say "%hash"; should have the exact same effect.

edit

I was wrong, it helps adding white-space through $"

#!/usr/bin/perl use strict; use warnings; use feature 'say'; my %hash = (); $hash{name} = 'Michelle'; $hash{age} = 22; $hash{city} = 'Austin'; say "1: @{[%hash]}"; say "2: %hash"; say "3: ",%hash; say "4: ", join $",%hash;
1: city Austin name Michelle age 22 2: %hash 3: cityAustinnameMichelleage22 4: city Austin name Michelle age 22

but I'd certainly prefer Data::Dumper or Data::Dump here, or at least say join " ", %hash

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Print the contents of a hash without looping.
by shmem (Chancellor) on Dec 02, 2017 at 11:40 UTC

    Just for completenes -

    say "%hash"; should have the exact same effect.

    Arguably it should, but it doesn't. A hash (or a construct with sigil %) is never interpolated in double quoted strings or qq(). I guess it is because, at list expansion of a hash, the sequence of key/value tuples is random. And then, the char % is meaningful for sprintf. However, hash slices are interpolated (their sigil is @). So,

    my $hash = \%hash; say "5: %$hash"; say "6: @hash{keys %hash}"; say "7: @$hash{keys $hash}";
    5: %HASH(0x1185900) 6: Michelle Austin 22 7: Michelle Austin 22
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Print the contents of a hash without looping.
by BillKSmith (Monsignor) on Dec 02, 2017 at 04:23 UTC
    Since you bring up the subject of special variables, we should point out that your case 3 is essentially the same as case 1, but uses $, instead of $".
    do{ local $, = $"; say "5:", %hash;}
    Bill
A reply falls below the community's threshold of quality. You may see it by logging in.