Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I ran into a case in a module where I had 'use vars' and I changed it to 'our' causing it to break.

It was in an export module that exports a variable to the caller's package. I had:

my $code = 'package '.$pkgnam.'; use vars qw($'.$varname.'); $'.$varname.' = "'.$value.'"; '; eval $code; $@ and die "Error in exporting var $varname: $@";
and had changed it to:
my $code = 'package ' . $pkgnam . '; our $'.$varname.' = "'.$value.'"'; eval $code; $@ and die "Error in exporting var $varname: $@";
As a sample varname, I had 'Tor_mgr' being exported back to the using prog. After the change, in the using prog, I got:
Variable "$Tor_mgr" is not imported at ./rss_rdr line 724. Variable "$Tor_mgr" is not imported at ./rss_rdr line 736. Variable "$Tor_mgr" is not imported at ./rss_rdr line 745. Global symbol "$Tor_mgr" requires explicit package name at ./rss_rdr l +ine 724. Global symbol "$Tor_mgr" requires explicit package name at ./rss_rdr l +ine 736. Global symbol "$Tor_mgr" requires explicit package name at ./rss_rdr l +ine 745. BEGIN not safe after errors--compilation aborted at ./rss_rdr line 754 +.
What I think was happening, is that the processing of the module didn't happen until after the prog had already been through the 'BEGIN' stage, so this module couldn't insert the 'our' definition in the package's namespace before its usage, so in the 2nd pass, perl saw $Tor_mgr which it now new to be defined later on, but didn't see it in an EXPORT array in the used module (I'm guessing). Anyway, since the 'use vars' was in the first version, that defined it in a non-lexical scope, which passes the 2nd pass's checks.

It may be that the module could be rewritten to not need use vars, but it doesn't look straight forward.

FWIW, the module being used, exported variable names with values corresponding to found paths of their lower-case names as executables, so any EXPORT list would be variable per usage (though fixed per program invocation).


In reply to Re^2: Is 'use vars' really obsolete? by perl-diddler
in thread Is 'use vars' really obsolete? by jnorden

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found