<?xml version="1.0" encoding="windows-1252"?>
<node id="660528" title="alexm's scratchpad" created="2008-01-05 06:18:22" updated="2008-01-05 01:18:22">
<type id="182711">
scratchpad</type>
<author id="660050">
alexm</author>
<data>
<field name="doctext">
&lt;h2&gt;autovivification and symbolic refs&lt;/h2&gt;

&lt;p&gt;This:

&lt;code&gt;
$hash{'foo'} = 1;
$hash{'foo'}{'bar'} = 2;
&lt;/code&gt;

performs:

&lt;code&gt;
%1 = { bar =&gt; 2 };
&lt;/code&gt;

&lt;p&gt;Reference: [id://696522]&lt;/p&gt;

&lt;h2&gt;[id://698212|a way to export all constants instead of listing each one]&lt;/h2&gt;
&lt;p&gt;My response to [id://697835] didn't get much feedback, and it's one that I'm very proud of, so I'm adding it here, just in case someone finds it useful (it was quite entertaining to come up with).&lt;/p&gt;

&lt;c&gt;
##### MyConst.pm #####
package MyConst;

use warnings;
use strict;

use Readonly ();

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw();

Readonly::Scalar our $const1 =&gt; 1;
Readonly::Array  our @array  =&gt; qw(x y z);
Readonly::Hash   our %hash   =&gt; (abc =&gt; 123, def =&gt; 456);

sub foo { 'foobar' }

my $package = __PACKAGE__;

no strict 'refs';
while (my $sym = each %{ "$package\::" }) {
    # skip internals
    next if $sym =~ /^(?:BEGIN|EXPORT|ISA)$/;

    if (defined ${ $sym }) {
        push @EXPORT, "\$$sym";
    }
    elsif (defined %{ $sym }) {
        push @EXPORT, "\%$sym";
    }
    elsif (defined @{ $sym }) {
        push @EXPORT, "\@$sym";
    }
    elsif (defined &amp;{ $sym }) {
        push @EXPORT, "$sym";
    }
}

1;
__END__
&lt;/c&gt;
</field>
</data>
</node>
