On 5/2/06, Jay Savage <[EMAIL PROTECTED]> wrote:
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?



Yes, I did look, it wasn't too descriptive.

What I ended up doing was reverting back to the "deprecated" method of
use vars qw($foo); at the top of the library.


using "our" within the sub routine to import globals seemed to work,
but one of my machines is < v5.6 so that won't work.

#!/usr/bin/perl -w

use vars qw($foo); #globals;
use strict;

sub foo {
       $foo = 'foo';
       my $baz = 'baz';
}

my $baz = 'pre-baz';
foo();

print $foo, "\n";
print $baz, "\n";

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to