I have the following pm file :
use strict ;
use warnings ;
package FOO ;
sub Principal::bof {
my $fic = "toto" ;
bof2() ;
}
sub bof2 {
print $fic
}
1;
Which is loaded by a script like this :
use strict ;
use warnings ;
package Principal ;
require ("ot.pm") ;
Principal::bof() ;
This fails with Global symbol "$fic" requires explicit package name .
I tried to do print $FOO:fic or print $Principal::fic in the bof2 sub but then it fails with Use of uninitialized value in print
I don't understand the namespaces relations in this . Is there a way i can access the value from bof2() without passing it as parameter ?
Thanks if you can help .
Lexical (my) variables aren't accessible outside of the curlies or file they are in. Use package variables instead. You probably want to use local in addition to our to prevent the clobbering of any existing value.
use strict ;
use warnings ;
package FOO ;
sub Principal::bof {
our $fic; # Save us from typgin $FOO::fic.
local $fic = "toto" ; # 'local' causes $fic to revert back when we exit this sub.
bof2() ;
}
sub bof2 {
our $fic; # Save us from typgin $FOO::fic.
print $fic
}
1;
I beleive you can just declare $fix in the scope of the FOO package instead of the sub. The following appears to do what you want.
use strict;
use warnings;
{
package FOO;
my $fic;
sub Principal::bof {
$fic = "toto";
bof2();
}
sub bof2 {
print $fic;
}
}
Principal::bof();
perlmonks.org content © perlmonks.org and eric256, ikegami, secret
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03