The Sidhekin wrote:
>   The packages you declare "internal" are not what you think they are.
> Observe:
>
> sidhekin@bluebird[07:57:50]~$ perl -le '$t{ __PACKAGE__ }++; print keys %t;'
> __PACKAGE__

I noticed that in the debugger, but didn't know if it was a literal string or a shortcut similar to Data::Dumper...


> sidhekin@bluebird[07:57:52]~$ perl -le '$t{ +__PACKAGE__ }++; print keys
> %t;'
> main
> sidhekin@bluebird[07:57:53]~$ perl -le '$t{ (__PACKAGE__) }++; print keys

Thank you. The second form is per RTFM -- I didn't pay enough attention and missed the parentheses. The fixed demonstration program follows.


I recently tripped over hash key context (or whatever it's call) when I attempted to use constants for hash keys. Now I can add something else to the shopping list:

2011-01-17 13:04:07 dpchrist@p43400e ~/sandbox
$ nl -b a hash-key-context.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  use warnings;
     4  use Data::Dumper;
     5  use constant KEY => 1;
     6  my %hash;
     7  $hash{ KEY           } = __LINE__;
     8  $hash{ KEY()         } = __LINE__;
     9  $hash{ __PACKAGE__   } = __LINE__;
    10  $hash{ (__PACKAGE__) } = __LINE__;
    11  print Data::Dumper->Dump([\%hash], [qw(*hash)]);

2011-01-17 13:04:16 dpchrist@p43400e ~/sandbox
$ perl hash-key-context.pl
%hash = (
          'KEY' => '7',
          '1' => '8',
          '__PACKAGE__' => '9',
          'main' => '10'
        );

Camel book 3 "Scalar and List Context" (pp. 69-70) and "Hashes" (pp. 76-78) aren't much help. Where is hash key context explained?


> %t;'
> main
> sidhekin@bluebird[07:57:54]~$
>
>   Careful with those barewords, now. ;-)

I can't find __PACKAGE__ in the Camel book 3 index (?). STFW I found it in the online Perl Cookbook, where it's called a symbol:

    http://docstore.mik.ua/orelly/perl/cookbook/ch12_06.htm

The online Camel book calls it a special literal token:

    http://docstore.mik.ua/orelly/perl4/prog/ch02_06.htm


David



2011-01-17 12:19:55 dpchrist@p43400e ~/sandbox
$ nl -b a carp-internal2.pl
     1  #!/usr/bin/perl
     2  
     3  package Foo;
     4  use Carp;
     5  $Carp::Internal{ (__PACKAGE__) }++;
     6  sub myconfess {
     7      eval {
     8          Carp::confess @_;
     9      };
    10      print $@, "\n";
    11  }
    12  
    13  package Bar;
    14  use Carp;
    15  sub myconfess {
    16      local $Carp::CarpLevel = 2;
    17      eval {
    18          Carp::confess @_;
    19      };
    20      print $@, "\n";
    21  }
    22  
    23  package Baz;
    24  use Carp;
    25  $Carp::CarpInternal{ (__PACKAGE__) }++;
    26  sub myconfess {
    27      eval {
    28          Carp::confess @_;
    29      };
    30      print $@, "\n";
    31  }
    32  
    33  package main;
    34  sub run {
    35      Foo::myconfess('goodbye, cruel world!');
    36      Bar::myconfess('goodbye, cruel world!');
    37      Baz::myconfess('goodbye, cruel world!');
    38  }
    39  run();

2011-01-17 12:20:04 dpchrist@p43400e ~/sandbox
$ perl carp-internal2.pl
goodbye, cruel world! at carp-internal2.pl line 35
        main::run() called at carp-internal2.pl line 39

goodbye, cruel world! at carp-internal2.pl line 36
        main::run() called at carp-internal2.pl line 39

goodbye, cruel world! at carp-internal2.pl line 37
        main::run() called at carp-internal2.pl line 39

Reply via email to