import works by making an alias between parts of the symbol table. For example:
# export $MyPackage::var to $YourPackage::var *YourPackage::var = \$MyPackage::var;
After this assignment, $YourPackage::var and $MyPackage::var now access the same scalar variable. The left hand side is a glob. I've used a reference to a scalar on the right hand side; this makes an alias only for the scalar. If the right-hand-side were also a glob, all the types (array, hash, etc.) for the name 'var' would have been aliased.

 

This is how Exporter's import method works, except that it uses symbolic references to specify the names of the variables. For example:
$callpackage = 'YourPackage; $pkg = 'MyPackage'; $sym = 'var'; ${"${callpkg}::$sym"} = \${"${pkg}::$sym"};
This doesn't work for lexical variables, i.e. those declared with my, because they are not in the symbol table.

 

However, it is possible to make an alias from the symbol table to a lexical variable:
#!perl -l { my $var = 7; *Foo::var = \$var; print $Foo::var; foo(); print $var; } sub foo { # changes both $Foo::var and the lexical $var $Foo::var = 8; }
So, you can write your own import method that exports lexical variables to the calling package, but you will have to either hard code the variable names or use eval, since the usual approach of symbolic references won't work.

 

BTW, local does not declare a variable, as far as strict is concerned. In order to satisfy strict, variables without explicit package names must be declared with my or vars (or, in 5.6.0, our [offsite link]).

In reply to Re: use strict and exporting package variables by chipmunk
in thread use strict and exporting package variables by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.