## create an anonymous block to limit the scope of the package
{
package globtut;
$var = "a string";
@var = qw( a list of strings );
sub var { }
}
use Data::Dumper;
print Dumper(\%globtut::);
__output__
$VAR1 = {
'var' => *globtut::var
};
####
$globtut::var = "a string";
@globtut::var = qw( a list of strings );
sub globtut::var { }
use Data::Dumper;
print Dumper(\%globtut::);
__output__
$VAR1 = {
'var' => *globtut::var
};
##
##
use Data::Dumper;
{
package globtut;
package globtut::child;
## ^^
}
##
##
use Data::Dumper;
{
package globtut;
$foo = "a string";
$globtut::{bar} = "I'm not even a glob!";
%globtut::baz:: = %globtut::;
print Data::Dumper::Dumper(\%globtut::baz::);
print "keys: ", join(', ', keys %globtut::), $/;
print "values: ", join(', ', values %globtut::), $/;
print "each: ", join(' => ', each %globtut::), $/;
print "exists: ", (exists $globtut::{foo} && "exists"), $/;
print "delete: ", (delete $globtut::{foo} && "deleted"), $/;
print "defined: ", (defined $globtut::{foo} || "no foo"), $/;
}
__output__
$VAR1 = {
'foo' => *globtut::foo,
'bar' => 'I\'m not even a glob!',
'baz::' => *{'globtut::baz::'}
};
keys: foo, bar, baz::
values: *globtut::foo, I'm not even a glob!, *globtut::baz::
each: foo => *globtut::foo
exists: exists
delete: deleted
defined: no foo
##
##
{
package globtut;
$variable = "a string";
@variable = qw( a list of strings );
sub variable { }
print $globtut::{variable}, "\n";
}
__output__
*globtut::variable
##
##
{
package globtut;
$foo = "a string";
$globtut::{variable} = *foo;
print "\$variable: $variable\n";
}
__output__
$variable: a string
##
##
$scalar = "a simple string";
print *scalar{SCALAR}, "\n";
__output__
SCALAR(0x8107e78)
##
##
$scalar = "a simple string";
print ${ *scalar{SCALAR} }, "\n";
__output__
a simple string
##
##
$scalar = "a simple string";
print $::scalar, "\n";
__output__
a simple string
##
##
$scalar = "a simple string";
print ${ *{$main::{scalar}}{SCALAR} }, "\n";
__output__
a simple string
##
##
${ *scalar{FOO} } = "the FOO data type";
__output__
Can't use an undefined value as a SCALAR reference at - line 1.
##
##
## this is fine as we're dereferencing the stored reference
${ *foo{SCALAR} } = "a string";
## this will generate a compile-time error
*foo{SCALAR} = "a string";
__output__
Can't modify glob elem in scalar assignment at - line 5, near ""a string";"
##
##
*foo = \"a scalar";
print $foo, "\n";
*foo = [ qw( a list of strings ) ];
print @foo, "\n";
*foo = sub { "a subroutine" };
print foo(), "\n";
__output__
a scalar
alistofstrings
a subroutine
##
##
use Data::Dumper;
## use a fresh uncluttered package for minimal Dumper output
{
package globtut;
*foo = "string";
print Data::Dumper::Dumper(\%globtut::);
}
__output__
$VAR1 = {
'string' => *globtut::string,
'foo' => *globtut::string
};
##
##
## put this code in Foo.pm
package Foo;
use strict;
sub import {
## find out who is calling us
my $pkg = caller;
## while strict doesn't deal with globs, it still
## catches symbolic de/referencing
no strict 'refs';
## iterate through all the globs in the symbol table
foreach my $glob (keys %Foo::) {
## skip anything without a subroutine and 'import'
next if not defined *{$Foo::{$glob}}{CODE}
or $glob eq 'import';
## assign subroutine into caller's package
*{$pkg . "::$glob"} = \&{"Foo::$glob"};
}
}
## this won't be imported ...
$Foo::testsub = "a string";
## ... but this will
sub testsub {
print "this is a testsub from Foo\n";
}
## and so will this
sub fooify {
return join " foo ", @_;
}
q;
##
##
use Data::Dumper;
## we'll stay out of the 'polluted' %main:: symbol table
{
package globtut;
use Foo;
testsub();
print "no \$testsub defined\n"
unless defined $testsub;
print "fooified: ", fooify(qw( ichi ni san shi )), "\n";
print Data::Dumper::Dumper(\%globtut::);
}
__output__
this is a testsub from Foo
no $testsub defined
fooified: ichi foo ni foo san foo shi
$VAR1 = {
'testsub' => *globtut::testsub,
'BEGIN' => *globtut::BEGIN,
'fooify' => *globtut::fooify
};