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

When I our in this block and our in that block, nothing prevents me from accidentally using slightly different variable names in the two blocks. This is a common error for me, particularly when the blocks are far apart, and I really want strict to catch it.

Therefore if I want to access a global in 2 places, I really, really want to declare that global in one place. While the ability to declare it in multiple tight scopes sounds neat, it is bad software design to do things that way.

Replies are listed 'Best First'.
Re^15: the "our" declaration ?!! (special vars)
by LanX (Saint) on Jan 22, 2009 at 04:44 UTC
    As Jenda pointed out you can still declare once with our() on top of the file, which has the same effect as "use vars"...

    Sorry, I still don't understand what you can do with "vars" which is not possible with our()¹. Could you please post a short code example to make it clearer?

    UPDATES:
    (¹) Or an explicit import() method.

      Here are two examples of things that vars does differently and better than our.

      First let's assume you haven't seen the light of packages and use Library.pm:

      use strict; use vars qw($foo); # ... code that uses $foo ... 1;
      And then you have script.pl
      use strict; use Library; # You can now use $foo freely.
      The other example goes like this:
      use vars qw($foo); # Some code here. package Bar; # Now you can't use $foo, and won't accidentally access $main::foo.
      Both examples are admittedly trivial. But in both cases what vars does is better than what our would do.

      Now where is the win with using our? It is that it lets people write this:

      package Foo; use strict; use Exporter qw(import); our @EXPORT_OK = qw(foo); # etc 1;
      instead of the 1 line longer
      package Foo; use strict; use Exporter qw(import); use vars qw(@EXPORT_OK); @EXPORT_OK = qw(foo); # etc 1;
      or the (to many people) less aesthetic
      package Foo; use Exporter qw(import); @EXPORT_OK = qw(foo); use strict; # etc 1;
      This win does not, in my eyes, qualify as important enough to make our be a major improvement in Perl. Nor does it make it a significant improvement on vars.

      That said, Larry Wall's thinking is that our was a declaration on which he could hang other syntax. Yes, this is the famous "my Dog $spot" discussion. That hasn't happened in Perl 5, nor does it seem likely to in the near future, but it is supposed to in Perl 6. At which point our would have substantially greater functionality. But that hasn't happened yet.

        use vars::i qw[ @EXPORT_OK  foo ]; vars::i

        first example:

        OK, you want extend a singular declaration for a package to multiple files. That's handy when you want to split a big package into multiple files, but you can achieve this with "require" and "do".

        #--- subfile my $file="our_reg.pm"; print "$file: $x";
        #--- basefile use strict; my $file="our_base.pl"; $\="\n"; package one; our $x=__PACKAGE__; print "$file $x"; require our_req; package two; our $x=__PACKAGE__; print "$file $x"; do "our_req.pm";
        #--- OUTPUT /usr/bin/perl -w /home/lanx/perl/pm/declare/our_base.pl our_base.pl one our_reg.pm: one our_base.pl two our_reg.pm: two
        for most other cases I can think of, it's IMHO better to use getter and setters for packvars, if you don't want to repeat the "our"-declaration.

        second example:

        Right,,in contrast to "our" the scope of "vars" is bound to "package". That's why one should use curlies around multiple packages, to clarify the scopes!!!

        That's exactly my point, you can use "our" like "my", without getting confused about different scoping rules...

        Cheers Rolf

        UPDATES:

        Let's sum it up, TIMTOWTDI, and you prefere saving code with the one variant and prefere saving some lines with the other .. ; )

        well how is it done in perl6???