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

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???

Replies are listed 'Best First'.
Re^18: the "our" declaration ?!! (special vars)
by tilly (Archbishop) on Jan 22, 2009 at 14:58 UTC
    I never said that you can't use our for the same task. I said that it was worse.

    In example 1, with our it is a lot easier to make and a lot harder to catch a mistake where in one file you have declared $with_foo while in the other you've named it $for_foo. This makes our worse.

    In the second example, to avoid problems you are requiring people to have a habit that most people don't have. Furthermore it is not enough to put curlies around your package declarations, you also have to put curlies around your original program. However taking that step means that if you discover that you need a small auxiliary package, you have to make a much, much bigger change. Again our is worse.

    Summary: from a software engineering standpoint, in Perl 5 our is the same or worse than vars. Its only win is a slight convenience/aesthetic advantage.

    (My attitude is that Perl 6, if it ever arrives in production, will be a different language that needs to be judged on its principles.)