in reply to Re^3: My globals are visable; but undef'ed
in thread My globals are visable; but undef'ed

I grant that it looks prettier, but if you're going to be sharing variables between scripts, it is distinctly worse.

Within a script it doesn't matter which you use, but it is probably better to use my instead.

  • Comment on Re^4: My globals are visable; but undef'ed

Replies are listed 'Best First'.
Re^5: My globals are visable; but undef'ed
by Joost (Canon) on Jul 31, 2008 at 21:37 UTC
    I don't want this to turn into a long discussion, and I agree that using globals/package vars for sharing data is usually not a good idea (though it does work very well for most situations where you'd otherwise use "singletons" - especially when you can Export the variables). But I don't see what lexicals have to do with the post you referenced above.

      If you are using our to access data within one package only, why are you making it a global when you could trivially make it a lexical?

      Here are the rules I follow. I use my unless there is a specific reason not to. I will use our for certain standard globals that need to be global, such as @EXPORT_OK. When I need to share variables across multiple files I try to use Exporter to export it from one place to all of the places that need it. If that solution won't work, then I will "use vars" for the declaration. I never use our for any variables that I've invented within my code.

      I used to not use our for those standard globals. Instead I used to just initialize them before my use strict line. But it has been so long since anyone I care about has been using Perl 5.005 that I no longer worry about that.

        I think we're more or less on the same frequency here:
        If you are using our to access data within one package only, why are you making it a global when you could trivially make it a lexical?
        I don't do that, and I didn't mean to imply that I did.

        Here are the rules I follow. I use my unless there is a specific reason not to. I will use our for certain standard globals that need to be global, such as @EXPORT_OK. When I need to share variables across multiple files I try to use Exporter to export it from one place to all of the places that need it.
        I'm with you so far.
        If that solution won't work, then I will "use vars" for the declaration. I never use our for any variables that I've invented within my code. ...
        I really don't see why you'd make that distinction. I just use our() for all package variables.