in reply to Scope of package variables
It is in scope. It's just currently set to undef. You're executing this line:
$t_obj->dump_sv();
... before you execute this line:
my $sv_a = "scalar variable A";
So $sv_a has no value yet.
This, for example, works:
#!/usr/local/bin/perl use 5.010; use strict; use warnings; my $sv_a; MainFunction: { $sv_a = "scalar variable A"; my $t_obj = ScopeTest->new(); $t_obj->dump_sv(); } package ScopeTest; sub dump_sv { print("DumpValue: sv_a ... $sv_a\n"); } sub new { my $obj_hv = {}; bless($obj_hv, $_[0]); } 1;
Being pedantic, variables declared with my are not "package variables", but "lexical variables". Package variables are those declared with our (in fact, these are lexical aliases to package variables), or those that are not declared at all.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scope of package variables
by rongrw (Acolyte) on Dec 21, 2013 at 03:42 UTC |