Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to use variables from other package?

by Necos (Friar)
on Apr 27, 2003 at 10:52 UTC ( [id://253469]=note: print w/replies, xml ) Need Help??


in reply to How to use variables from other package?

I know this is a bit overkill, but, another solution altogether would be to write a function that just returns the value of your my'd variables. Something like this would work (untested):
package CNLLog; my $_POST_CONVERSION = "postConversion"; my $_CORRECTIVE_SCRIPTS = "correctiveScripts"; sub postConversion_status { return $_POST_CONVERSION; }
And in your other package:
use CNLLog; $temp = CNLLog::postConversion_status; print $temp;
Exporter is a great module, but sometimes people don't want you stepping over their namespaces (functional or otherwise... And I know that you should use  @EXPORT_OK, but not everyone does that). So, using fully-qualified function calls can be very useful.

Hope that helps some,

Theodore Charles III
Network Administrator
Los Angeles Senior High
email->secon_kun@hotmail.com
perl -e "map{print++$_}split//,Mdbnr;"

Replies are listed 'Best First'.
Re: Re: How to use variables from other package?
by Vennis (Pilgrim) on Apr 28, 2003 at 07:46 UTC
    I consider this one of the most safest. Mainly because you will get an error message when you make a typo. Further i think it gives the best overview without juggling with main and module variables.

    Q: Why did the Perlmonk cross the road?
    A: He wanted to escape the match.

      Exporting the variables will lead to an error thrown for any typo under strict as well, and it is always thrown at compile time, while that isn't guaranteed for the function.

      Makeshifts last the longest.

        Good point Aristotle. The only point I was trying to make is that he could try exporting the variable value (since the name is unimportant) via function/method. It seems the safest way considering that you don't always want to step on someone else's toes with variable names.

        I've always been wary of things like  $pkg::var. If I can avoid it with the use of a function, I do. I never liked trying to remember all of the package variables from a package I didn't write. If you're using/writing a module that is OO-based, you should provide a method for getting the value of a certain attribute. And, even if you're writing something non-OO, you should _STILL_ provide some way of getting to a value without making your user remember a particular variable name (of course the exceptions to that rule are the common names, like $VERSION). At least, that's what I've always felt.

        Theodore Charles III
        Network Administrator
        Los Angeles Senior High
        email->secon_kun@hotmail.com
        perl -e "map{print++$_}split//,Mdbnr;"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found