in reply to Re^2: Problem combining hashes
in thread Problem combining hashes

(\ my %all_keys)->@{keys %A, keys %Z} = ();
What is the "\ my" doing? It seems to work without the "\".

Replies are listed 'Best First'.
Re^4: Problem combining hashes
by kcott (Archbishop) on Mar 16, 2024 at 02:48 UTC
    'It seems to work without the "\".'

    That seemed odd to me so I checked it. I modified the output slightly for ease of comparison.

    With \:

    $ perl -Mstrict -Mwarnings -E ' my %A = (a => 1, b => 2, D => 3); my %Z = (z => 9, y => 8, D => 7); (\ my %all_keys)->@{keys %A, keys %Z} = (); say for "@{[sort keys %all_keys]}"; ' D a b y z

    Without \:

    $ perl -Mstrict -Mwarnings -E ' my %A = (a => 1, b => 2, D => 3); my %Z = (z => 9, y => 8, D => 7); (my %all_keys)->@{keys %A, keys %Z} = (); say for "@{[sort keys %all_keys]}"; ' D a b y z

    So I can confirm that behaviour. I then ran both of those through B::Deparse.

    With \:

    $ perl -Mstrict -Mwarnings -MO=Deparse -E ' my %A = (a => 1, b => 2, D => 3); my %Z = (z => 9, y => 8, D => 7); (\ my %all_keys)->@{keys %A, keys %Z} = (); say for "@{[sort keys %all_keys]}"; ' use warnings; use strict; use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'isa', 'modul +e_true', 'postderef_qq', 'say', 'signatures', 'state', 'unicode_strin +gs', 'unicode_eval'; my(%A) = ('a', 1, 'b', 2, 'D', 3); my(%Z) = ('z', 9, 'y', 8, 'D', 7); @(\my %all_keys){keys %A, keys %Z} = (); say $_ foreach (join $", @{[sort(keys %all_keys)];}); -e syntax OK

    No huge surprises there.

    Without \:

    $ perl -Mstrict -Mwarnings -MO=Deparse -E ' my %A = (a => 1, b => 2, D => 3); my %Z = (z => 9, y => 8, D => 7); (my %all_keys)->@{keys %A, keys %Z} = (); say for "@{[sort keys %all_keys]}"; ' use warnings; use strict; use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'isa', 'modul +e_true', 'postderef_qq', 'say', 'signatures', 'state', 'unicode_strin +gs', 'unicode_eval'; my(%A) = ('a', 1, 'b', 2, 'D', 3); my(%Z) = ('z', 9, 'y', 8, 'D', 7); @all_keys{keys %A, keys %Z} = (); say $_ foreach (join $", @{[sort(keys %all_keys)];}); -e syntax OK

    The lexical variable my %all_keys appears to have been changed in favour of the package variable %all_keys (no my). A search through perlref provided no answers; although, it's not impossible that I missed something. I don't have time to investigate further; perhaps another monk can shed some light on this.

    All of the above used:

    $ perl -v | head -2 | tail -1 This is perl 5, version 39, subversion 3 (v5.39.3) built for cygwin-th +read-multi

    I repeated everything with the earliest Perl version I have available:

    $ perl -v | head -2 | tail -1 This is perl 5, version 30, subversion 0 (v5.30.0) built for cygwin-th +read-multi

    The results were the same except the "use feature" list was slightly different: signatures out; switch in.

    use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq +', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';

    I don't believe that difference has any relevance to what's being tested.

    — Ken

      This works also.
      my %all_keys; %all_keys->@{keys %A, keys %Z} = ();
      deparser says
      my %all_keys; @all_keys{keys %A, keys %Z} = ();
      So it doesn't seem to be requiring a hash reference in that context.
Re^4: Problem combining hashes
by LanX (Saint) on Mar 15, 2024 at 21:06 UTC