package MyTest; my $lexvar = 7; # This function is called automatically whenever # anyone 'use's (or 'require's, I'm pretty sure) # our module. Note that this happens at COMPILE TIME. sub import { # symbol table manipulation usually requires # the use of symbolic references... no strict 'refs'; # Figure out where to export to. # Check `perldoc -f caller` for info on caller. my $pkg_to_export_to = (caller)[0]; # Make the scalar part of the entry in the symbol # table of the calling package for 'lexvar' into a hard # reference to our lexical variable. This is called # typeglob aliasing. *{$pkg_to_export_to . '::lexvar'} = \$lexvar; }