You should always declare variables with either our or my.

"whichever package statement is in effect".Here's how you set the package.

use strict; use warnings; # the default namespace is main # so this is a variable in main. our $movie='Chitty Chitty Bang Bang'; # set the current package package A::B::C; # We've changed the package - it is no longer main # now its A::B::C # if a variable is undefined in the current package, # then Perl looks for a variable in main of the same name # these two statements are the same and do not cause # warnings print "movie=$movie\n"; print "movie=$main::movie\n"; # but once we declare the variable in A::B::C, an # unqualified name is presumed to be in the current # package # declare a package variable in the namespace A::B::C our $movie = 'Mary Poppins'; # these print different things print "movie=$movie\n"; # prints Marry Poppins print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang { # this overrides the current package. The override # will last until the next matching curly brace package NotA::NotB; # this is $movie but in the NotA::NotB namespace # its a different variable from either $main::movie # or $A::B::C::movie my $movie = 'Pippi Longstocking'; print "movie=$movie\n"; #prints Pippi Longstocking print "movie=$A::B::C::movie\n"; #prints Mary Poppins print "movie=$main::movie\n"; #prints Chitty Chitty Bang Bang # } matches { enclosing package statement # so this ends the code block where NotA::NotB is the # package. } # now we are back to A::B::C being the current package print "movie=$movie\n"; # prints Marry Poppins print "movie=$main::movie\n"; # prints Chitty Chitty Bang Bang

Hope that helps.

Best, beth


In reply to Re^3: main package by ELISHEVA
in thread main package by manishrathi

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.