in reply to Re: Sharing Across Packages
in thread Sharing Across Packages

I tend to think of global variables as "omnipresent variables". Variables contain data, and should be scoped to the smallest enclosing block where the data is being used. Sometimes, it just makes sense to have data available everywhere. Whether the program is running in debugging mode for instance. Or access to the toplevel window of a program. Sometimes it saves a lot of typing (and some schools say the smaller the program, the less bugs it contains) to have an omnipresent variable than pass the variable around to almost every subroutine you call.

But if you prefer simple rules and don't want to spend a few braincells wondering whether it makes sense to deviate from a guideline, you can always do:

package main; my $MW = ...; # Create your main window here. sub UNIVERSAL::MW {$MW}
After all, subroutine are (global) variables as well - but noone ever yells at you for using them.

Replies are listed 'Best First'.
Re^3: Sharing Across Packages
by polettix (Vicar) on Sep 29, 2006 at 15:59 UTC
    some schools say the smaller the program, the less bugs it contains
    ... like this? ;)
    But if you prefer simple rules...
    I'm not sure I always prefer simple rules. In case I have to stick to global variables, I'd prefer that this is clearly marked in some way, which is why I'd avoid this implementation of the "singleton". Moreover, this could be no singleton at all, due to the possible presence of other MW methods in other packages.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.