rongrw has asked for the wisdom of the Perl Monks concerning the following question:
Dear Perl Monks,
My question is about the scope of package variables. When I run the code listed at the bottom of this post, the following is displayed:
I'm a little confused about this, because my understanding of package variables is that they are in scope within subsequent subroutine or method definitions. Why is perl interpreting $sv_a as having an uninitialized value, when it clearly is initialized inside the package?Use of uninitialized value $sv_a in concatenation (.) or string at ./s +cope_t.pl line 15. DumpValue: sv_a ...
I'm using perl 5.10.1. If anyone could advise where I'm going wrong, that would be much appreciated. Now for the code ...
#!/usr/local/bin/perl use 5.010; use strict; use warnings; MainFunction: { my $t_obj = ScopeTest->new(); $t_obj->dump_sv(); } package ScopeTest; my $sv_a = "scalar variable A"; sub dump_sv { print("DumpValue: sv_a ... $sv_a\n"); } sub new { my $obj_hv = {}; bless($obj_hv, $_[0]); } 1;
Cheers, Ron.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scope of package variables
by tobyink (Canon) on Dec 19, 2013 at 14:22 UTC | |
by rongrw (Acolyte) on Dec 21, 2013 at 03:42 UTC | |
Re: Scope of package variables
by tangent (Parson) on Dec 19, 2013 at 14:29 UTC | |
Re: Scope of package variables
by NetWallah (Canon) on Dec 19, 2013 at 14:40 UTC | |
Re: Scope of package variables
by Laurent_R (Canon) on Dec 19, 2013 at 18:11 UTC |
Back to
Seekers of Perl Wisdom