On 5/2/06, Anthony Ettinger <[EMAIL PROTECTED]> wrote:
I want to double-check that it is correct to use "our" to import globals.
[snip] What do you mean by import? Variables aren't imported from BEGIN blocks. They're declared in BEGIN blocks, and a variable declared in a BEGIN block should be no differnt than a variable declared at the top of your script. Except, of course, that you guarantee compile-time execution and you can put the block anywhere. But none of that effects the way the variable declaration functions. If you're having trouble, get rid of the BEGIN and END blocks and just declare your variables right after 'use strict' and 'use warnings'. You can go back and put them in the BEGIN block later. Anyway, none of us can really help you unless you send real code.
If I do *not* import with "our $foo;" inside main::something(); I get the following errors:
The you've done something wrong somewhere else.
Is this the correct way to import globals?
Again, what do you mean by import? You're sample pseudo-script does not exhibit the beavior you describe, nor does it import any variables . Are you actually importing the variable from somewhere else? Another file perhaps? Are there other packages in your file you haven't shown us? What errors, if any, does the following script produce in your Perl? Perhaps your installation is buggy. This should print 'foo' and exit: #!/usr/bin/perl BEGIN { use strict; use warnings; our $foo = 'foo'; our $baz = 'baz'; } sub bar {print $foo;} bar(); __END__
$perldoc vars says...: NOTE: For variables in the current package, the functionality provided by this pragma has been superseded by "our" declarations, available in Perl v5.6.0 or later. See "our" in perlfunc.
And did you go see our in perlfuc? HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!