Please bottom post....

Eternius wrote:
<answer>
i agree with you,
but for the sake of read - a - bility I would have defined
the vars here (<---) as they are valid for whole package anyway and not
just for test1();


But again you have changed the meaning, if you are going to declare them as package variables at the top then you don't need a 'local' at all and that should be an 'our', this is even worse for encapsulation and definitely shows a lack of understanding of 'local'...


use strict;

local $a = 1;   <---
local $aa = 2;    <---

test1();

sub test1
{
   test2();
}

sub test2
{
   print "a = $a\n";
   print "aa = $aa\n";
}

As an example, try:


use strict;
use warnings;


our $aa = 1; our $aaa = 2;


print "aa 1st = $aa\n"; print "aaa 1st = $aaa\n";


test1();



print "aa 3rd = $aa\n"; print "aaa 3rd = $aaa\n";


sub test1 { local $aa = 3; local $aaa = 4; test2(); }


sub test2 { print "aa 2nd = $aa\n"; print "aaa 2nd = $aaa\n"; }

Suggested reading: http://perl.plover.com/FAQs/Namespaces.html

http://danconia.org


Wiggins d'Anconia wrote:

 > Eternius wrote:
 >
 >> Joshua A. Gage wrote:
[snip]
 >>
 >> use my
 >> extract from perldoc -f my :
 >>  A "my" declares the listed variables to be local
 >>                (lexically) to the enclosing block, file, or
 >>                "eval".
 >>
 >
 > In most cases  you would be right, but in this case it appears that the
 > OP understands what 'local' does, as he is using it inside of an
 > embedded function. Do I like that, no, but it is a "correct" usage of
 > 'local'.  Your suggestion of using 'my' changes how the variables must
 > be used so is not equivalent without suggesting the interface to 'test2'
 > changes...
 >
 > sub test1 {
 >   my $aa = 1;
 >   my $aaa = 2;
 >   test2($aa, $aaa);
 > }
 >
 > sub test2 {
 >   my ($bb, $bbb) = @_;
 >   print "aa = $bb\n";
 >   print "aaa = $bbb\n";
 > }
 >
 > Is the above better, in the court of readability and encapsulation, yes,
 > in the court of elegance, maybe not...
 >
 > http://danconia.org
 >
 >
 >


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