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

Re: Importing variables (not functions) from in-file packages

by AnomalousMonk (Archbishop)
on Feb 12, 2014 at 16:12 UTC ( #1074644=note: print w/replies, xml ) Need Help??


in reply to Importing variables (not functions) from in-file packages

Your original code seems to work as long as the scalar  $globalVar is exported using its correct name, i.e., using the  $ sigil.

c:\@Work\Perl\monks>perl -wMstrict -le "MyPackage->import; print $::globalVar; $::globalVar=1; print $::globalVar; myfunction(); BEGIN{ package MyPackage; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($globalVar myfunction); our $globalVar = 42; sub myfunction { print 'hi from ', __PACKAGE__; } } " 42 1 hi from MyPackage

Replies are listed 'Best First'.
Re^2: Importing variables (not functions) from in-file packages
by puterboy (Scribe) on Feb 12, 2014 at 19:14 UTC
    Cool. It does indeed work. But why does strict cause the compiler crap-out on $globalVar but accept $::globalVar. Isn't $globalVar (when not defined as a lexical variable) just a shortcut for the (main) package variable $::globalVar aka $main::globalVar? Or is the "shortcut" only known at runtime...
      Isn't $globalVar (when not defined as a lexical variable) just a shortcut for the (main) package variable $::globalVar aka $main::globalVar?

      It is a 'shortcut' only if you make the lexical declaration
          our $globalVar;
      Then if the first access of the package variable  $globalVar is after the declaration, everybody's happy; otherwise, the variable name must be fully qualified. See our.

      Because that is what you asked it to do with  use strict 'vars';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-06-06 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (29 votes). Check out past polls.

    Notices?