in reply to RE: (3) CGI.pm for CSS, tables, pet tricks (use vars vs my)
in thread (code)) CGI.pm - CSS, tables... (deprecated by node 50167)

zdog's tests would seem to indicate that lexical variables are faster than package variables, so if speed is important to you go with my, otherwise I don't know of any reason to choose one in preference to the other so just go with whatever you feel most comfortable with.

--
<http://www.dave.org.uk>

European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/>
  • Comment on RE: RE: (3) CGI.pm for CSS, tables, pet tricks (use vars vs my)

Replies are listed 'Best First'.
RE: RE: RE: (3) CGI.pm for CSS, tables, pet tricks (use vars vs my)
by tye (Sage) on Aug 14, 2000 at 19:52 UTC

    The general rule I follow is to use my unless there is a reason to use our (or use vars) to make package variables (A.K.A globals). There is only one reason to use globals: because you want to access them outside of their lexical scope. Reasons you'd want to do that include:

    • You want to export them from your package.
    • You want to allow external code to access them by giving a fully qualified name (this usually isn't a good idea).
    • You want to use them higher up in the file than you declared them at (this usually isn't a good idea).
    • You want to declare them within a block but use them outside of that block (this usually isn't a good idea -- though I can see it in rare cases involving BEGIN blocks).
    • You want to access them via a "symbolic reference" (this usually isn't a good idea).

    Note that using our won't let you do any of these except the first and last one.

            - tye (but my friends call me "Tye")