in reply to keys on scalar warnings and lack thereof

See Mini-Tutorial: Dereferencing Syntax.

The curlies in a circumfix dereference can only be omitted if they contain a simple scalar like $NAME (or $BLOCK), not something more complicated like $_[0].

%$_[0] should be %{ $_[0] } (circumfix notation) or $_[0]->%* (postfix notation).

Replies are listed 'Best First'.
Re^2: keys on scalar warnings and lack thereof
by Anonymous Monk on May 14, 2026 at 18:07 UTC
    I didn't include %{$_[0]} because it's obviously ok:
    perl -MDevel::Peek -e 'Dump(%{$_[0]})' SV = PVHV(0x157010280) at 0x15700ae40 REFCNT = 1 FLAGS = (SHAREKEYS) ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7 But what is this: perl -MDevel::Peek -e 'Dump(%$_[0])' SV = NULL(0x0) at 0x100939eb8 REFCNT = 2147483641 FLAGS = (READONLY,PROTECT) and this: perl -MDevel::Peek -e 'Dump(%$^[0])' SV = IV(0x159825cf0) at 0x159825d00 REFCNT = 1 FLAGS = (PADTMP,IOK,pIOK) IV = 5796572328 perl -MDevel::Peek -e 'Dump(%{$^[0]})' Number found where operator expected (Missing operator before "0"?)
    And all the other weird looking results?

      What exactly do you expect from broken code?

      $ perl -E' $_[0] = {}; say keys %$_[0]' Experimental keys on scalar is now forbidden at -e line 1. Type of arg 1 to keys must be hash or array (not index/value array sli +ce) at -e line 1, at EOF Execution of -e aborted due to compilation errors.

      Please note the not index/value array slice , because that's what's fed into keys.

      Try keys %{$_}[0] and compare the error messages...

      Update

      In other words an array slice of a hash ... WT(sic)F?

      Update

      Without keys the error is clearer %$X[0] is a less common dereferencing syntax, an alternative notation to $X->{0} but here it's an array index [0].

      That's nonsense, but the fact that keys' error message is kicking in earlier is masking the issue.

      Additionally is $_ undef and leaving room for auto vivification.

      Quite a mess man...

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