in reply to Re (tilly) 1: "use vars" will remain (Re: Win32 EXPORT_OK problems)
in thread Win32 EXPORT_OK problems

I would still recommend use vars over this.

First, I don't like the idea of a module that rather sneakilly sometimes turns off use strict "vars". If your development moves to a pre-5.6 version of Perl, you could easily not notice that your innocuous use Our has now disabled your use strict.pm until you waste a couple of days chasing the kind of bug that use strict.pm makes obvious (the reason you are supposed to use strict.pm).

I certainly don't consider this risk worth the very minor speed improvement which is the only advantage to our over use vars.

Also, if you are supporting pre-5.6 versions of Perl, you will be testing with pre-5.6 which may require some development in pre-5.6 versions which you shouldn't be doing without use strict.pm.

So my problems with this approach are quite small but I consider them huge compared to the tiny, tiny "problem" that they solve -- being able to use our instead of use vars. If you are firmly into 5.6-and-later Perl and choose to use our, then these provide a nice way to be pollite to people who aren't.

I will continue to do use vars until Perl supports non-blocked compile-time code (or another soltution) for being able to conditionally use our (or until pre-5.6 Perl becomes old enough that I never run into it anymore).

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

Replies are listed 'Best First'.
Re (tilly) 2: "use vars" will remain (Re: Win32 EXPORT_OK problems)
by tilly (Archbishop) on Dec 27, 2000 at 02:20 UTC
    You are wrong that a speed improvement is the only reason to use our over use vars. (In fact I am not absolutely positive that it is always a win.)

    The difference with our is that the declaration is lexical, not global. So using our in 3 functions becomes an alternative to a global that is globally accessible, or a private block with a lexical variable.

    Personally I don't consider the win over use vars to be compelling. In fact if you are going to be sharing a variable in several places, I don't like having to synchronize the declarations. Hence for most purposes I actively prefer use vars.

    However that is an opinion decidedly at odds with many other people. Hence the above may prove useful if (unlike me) you are in a predominantly 5.6 environment and like our.

Re: (tye)Re: "use vars" will remain (Re: Win32 EXPORT_OK problems)
by chipmunk (Parson) on Dec 27, 2000 at 01:44 UTC
    Actually, I think our has one other advantage over use vars, aside from the very minor speed improvement. With our, as with my, declaration and initialization can be done in one step:
    my $num = 7; our $str = 'Hello';
    whereas with use vars, declaration and initialization must always be separate steps:
    use vars qw/ $str /; $str = 'Hello';

      declaration and initialization can be done in one step

      Ah, yes, but should they? Even I were to use our, I'd still be writing code like:

      our $var; BEGIN { $var= "Hello"; }
      to avoid the race condition between the compile-time declaration of the variable and the (possibly very delayed or non-existant) run-time initialization of the variable. This is only rarely a problem, but it still happens often enough that this habit has saved me enough time that I continue it.

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