Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Accessing the package variables by fully qualified name

by vinoth.ree (Parson)
on Jul 30, 2009 at 12:42 UTC ( #784587=perlquestion: print w/ replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I go through some of the projects, there they used the variables of the package with fully qualified name, even though that package has been imported into the current package.

lets say,

use package_name;<br> $package_name::main::variablename=10;

It will be useful in case of if I have the same variable name in the current package which I included this package, Any other advantage is there? Is it the safe mode of accessing the package variables ?

Update :

Title Updated

Comment on Accessing the package variables by fully qualified name
Download Code
Re: Accessing the package variables by filly qualified name
by mzedeler (Pilgrim) on Jul 30, 2009 at 12:51 UTC

    Importing a package with use doesn't automatically export all package variables (and subs) to your namespace, so they may be accessing the variable using fully qualified names because it is the only way to access that particular variable.

      Yes, you are right thanks for an idea, but they used fully qualified name for when they change the value of that variable.

        ...sorry I don't understand. Is it that the variable has been exported, but when its being changed, its addressed using its fully qualified name?

Re: Accessing the package variables by fully qualified name
by cdarke (Prior) on Jul 30, 2009 at 13:33 UTC
    Any other advantage is there? Is it the safe mode of accessing the package variables ?

    Since the variables have been exported there is no need to use the fully qualified name. The upside of using the fully qualified name is that you can see where the variable originated, the downside is that it is confusing if an unqualified name is used as well, or by accident. It is an argument against exporting variables - better for the module to provide accessor subroutines IMHO.
      cdarke's humble opinion is correct, the package variables should stay where they are and be accessed through getters and setters, if these are available, use them, if not, well be careful. $instance_of_package->set_named_variable("new_value"); is more readable I believe and for "class" variables Package::Name->set_class_variable("new Global"); is more legible to me than direct assignment, provided the accessors are half-way sensibly named.
Re: Accessing the package variables by fully qualified name
by Anonymous Monk on Jul 31, 2009 at 02:35 UTC
    Why don't you just ask them to explain their reasons?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://784587]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-09-30 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls