Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

glob'ing and variable setting

by Bloodnok (Vicar)
on Dec 31, 2013 at 14:59 UTC ( [id://1068802]=perlquestion: print w/replies, xml ) Need Help??

Bloodnok has asked for the wisdom of the Perl Monks concerning the following question:

Greetings and a HNY for 2014 to all you fellow monasterians ,

I have a module that should be capable of auto-exporting, via the import() sub, an initialized variable, $VAR, via a typeglob i.e. not via Exporter, into the useing packages namespace. At its simplest (I'm afraid that I'm not at liberty to divulge the exact details), my module code is along the lines of ...

use warnings; use strict; package Package; my $Val; sub import { Val = qw/some_val/; my $caller = caller; no strict qw/refs/; *{"$caller\::VAR"} = *Val; } 1;
The variable is, indeed, set i.e. I see "Some_val" c/w "Something's wrong", if the declaration is made thus...
use Package; our $VAR; warn $VAR;
My question now is, what have I overlooked such that cases where the variable declaration is made in a list work e.g. neither of the following 2 cases work...
use Package; our ($VERSION, $VAR); warn $VAR;
or
use package; our ($VAR, $VERSION); warn $VAR;
In both of the above cases, I see the "Something's wrong" warning.

Any thoughts/insights etc. gratefully received

TIA ,

Further update:

Code will never run without relaxing <C>strict refs<C/> ... doh!!

Update:

Add clarification thanx to choroba who was good enough to point out the lack of clarity in the OP.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re: glob'ing and variable setting
by choroba (Cardinal) on Dec 31, 2013 at 15:02 UTC
    What do you mean by "auto-exporting"? Do you use Exporter? If so, please show us how. If not, please show us how you export the variable; and also, how you check that the variable was "indeed" exported.

    Update: The following works for me. Note, though, that exporting variables is not recommended:

    Local.pm:

    package Local; use warnings; use strict; use Exporter 'import'; our @EXPORT_OK = qw( $VAR ); our $VAR = 12; __PACKAGE__

    script.pl:

    #!/usr/bin/perl use warnings; use strict; use Local qw( $VAR ); print "$VAR\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-19 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found