Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^13: the "our" declaration ?!! (special vars)

by Sandy (Curate)
on Jan 21, 2009 at 20:56 UTC ( #737950=note: print w/ replies, xml ) Need Help??


in reply to Re^12: the "our" declaration ?!! (special vars)
in thread the "our" declaration ?!!

Sorry if I misunderstand what you say

What vars does is declare a certain global in a certain package so that strict will not complain about it. If you have a package that crosses files, the declaration will cover both (assuming it happens in the first one Perl compiles).
foo.pl
#!/usr/bin/perl use strict; use warnings; package Foo; use Bar; our $one; our $two; use vars qw($three $four); $one = 'one'; $two = 'two'; $three = 'three'; $four = 'four'; print "$one $two $three $four\n"; Foo::switch(); print "$one $two $three $four\n";
Bar.pm
#!/usr/bin/perl use strict; use warnings; package Foo; our $one; our $two; use vars qw($three $four); sub switch { $one = 1; $two = 2; $three = 3; $four = 4; } 1;
result
[sandy][~] perl foo.pl one two three four 1 2 3 4
I'm not sure I see the difference between our and use vars

UPDATE

Thank you ikegami. Now I get it.


Comment on Re^13: the "our" declaration ?!! (special vars)
Select or Download Code
Replies are listed 'Best First'.
Re^14: the "our" declaration ?!! (special vars)
by ikegami (Pope) on Jan 21, 2009 at 21:22 UTC

    Try commenting out

    use vars qw($three $four);

    in foo.pl. Then try commenting out

    our $one; our $two;

    in foo.pl

      Yeah but this saves just one line in foo.pl, a benefit restricted to cases where I don't wanna use the import() method!

      Sandy thanks for the code-example, makes it much clearer! : )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2015-07-07 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls