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:

Use of uninitialized value $sv_a in concatenation (.) or string at ./s +cope_t.pl line 15. DumpValue: sv_a ...
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?

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.


In reply to Scope of package variables by rongrw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.