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":



  • 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.