Hi Monks, I am trying to understand the scope of our variables using below simple code.

#!/usr/bin/perl -w #use strict; our $hometown = "rajkot"; our $state = "gujarat"; print "Main hometown : $hometown\n"; print "Main state : $state\n"; package dushyant; print "dushyant first hometown : $hometown\n"; print "dushyant first state : $state\n"; print "dushyant main hometown : $main::hometown\n"; print "dushyant main state : $main::state\n"; package parmar; our $hometown = "parmar_rajkot"; our $state = "parmar_gujarat"; print "parmar hometown : $hometown\n"; print "parmar state : $state\n"; print "parmar main hometown : $main::hometown\n"; print "parmar main state : $main::state\n"; package suresh; print "suresh hometown : $hometown\n"; print "suresh state : $state\n"; print "suresh main hometown : $main::hometown\n"; print "suresh main state : $main::state\n"; package main; print "Main hometown : $hometown\n"; print "Main state : $state\n";

Output of the code is :

Main hometown : rajkot Main state : gujarat dushyant first hometown : rajkot dushyant first state : gujarat dushyant main hometown : rajkot dushyant main state : gujarat parmar hometown : parmar_rajkot parmar state : parmar_gujarat parmar main hometown : rajkot parmar main state : gujarat suresh hometown : parmar_rajkot suresh state : parmar_gujarat suresh main hometown : rajkot suresh main state : gujarat Main hometown : parmar_rajkot Main state : parmar_gujarat

Last four line of output is confusing. What i am thinking is that Either Suresh main hometown should be printed as parmar_rajkot or Main hometown should be printed as rajkot. So please help me to understand whats going on here.


In reply to Scope of "our" Variable in package by dushyant

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.