Hi everybody, I am creating a pkg say "mypkg" which holds a global hash reference.say GLOBALVARREF. this variable is initialized to undef. once sub new on my pkg is called GLOBALVARREF starts pointing to self (blessed reference). At this point other pkgs can use this reference to fetch values from the hash. the whole thing mentioned above looks like
package mypkg; use vars qw (@ISA @EXPORT); @ISA =qw(Exporter); @EXPORT = qw( $GLOBALVARREF); our GLOBALVARREF = undef; sub new { GLOBALVARREF is intiliazed to $self. } sub get { return $someval{shift}; } pkg B; use mypkg; my $val = $GLOBALVARREF->get{'NAME'}; #my $val = $GLOBALVARREF->get{'NAME'} if defined($GLOBALVARREF)
now when i do perl -e 'use mypkg;' i get error saying can't call get on undefined value. if I put a condition saying if defined as shown in the commented line above it works fine.But the problem is "get()" gets called plenty of times in different modules.Can't add check everywhere. Is there a clean way to handle this scenario.

In reply to GLOBAL VARIABLE HANDLING by kaushal_k1

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.