in reply to Re: standard perl module and BEGIN block.
in thread standard perl module and BEGIN block.

Note the trick of putting the special variables before strict.pm, eliminating any need to bother declaring them.

Doesn't that "trick" make sections of the code position dependant? Doesn't that at least warrent a comment explaining that?

I think that either our or use vars is preferable.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^3: standard perl module and BEGIN block.
by tilly (Archbishop) on Aug 16, 2004 at 21:18 UTC
    And here I was under the impression that my pointing out the trick was a comment.. :-D

    Seriously now, you're right that it does make that section of the code depend on position. Which I don't see as a horrible thing - no worse than a lexically scoped variable. And certainly not in boilerplate initialization code which many people who use it won't expect to understand.

    If you're uncomfortable with that fact you can always declare the variables as is usually done. In fact I pointed out that I'd done it so that people could make that choice.

    Also I deliberately didn't comment on the mechanics. If I thought that the mechanics required commenting on within the code, then I'd first try to solve that by changing the mechanics, not commenting the code.

      My thinking that a comment in the boiler plate code would be appropriate as once code gets into a module, those coming along after may not appreciate it's source or significance.

      Avoiding globals all together would be the best route, but that's not currently possible for the likes of these "special variables".

      For a long time I didn't really appreciate your concerns regarding our, but having quite recently been bitten by a typo in an our var, they can indeed cause mysterious problems.

      I guess what I would really like, whilst doing away with globals completely is not an option, would be a combination of the two; use vars; and our.

      The former would indicate those globals I intended to use and disable the warnings/strictures on them--but only within those scopes where I had used our for that same variable.

      Using our for a var that hadn't previously been declared with use vars would raise an error. As would attempting to use a global mentioned in use vars, without also having scoped it using our.

      I'm not really a S&M type, and that does sound like "belt & braces" even to me, but given my propensity for typos, and the number of saves that "Global symbol "$x" requires explicit package name.." makes on my behalf, I would welcome this ability.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

        I'm just curious here, but your looking for something that would look like...

        package Some::Package; use vars qw/test/; sub inHere { our $test; }

        If so I think that would be far more intuitive than the current setup. After a year of perl I still have no concept of our.


        ___________
        Eric Hodges

        Using our for a var that hadn't previously been declared with use vars would raise an error. As would attempting to use a global mentioned in use vars, without also having scoped it using our.

        The difference between use vars and our is that the former allows a module's variables to be used from outside the module.

        # Would warn if Data::Dumper used 'our' instead of 'use vars'. local $Data::Dumper::Useqq = 1;

        Your idea would reverse the purpose of use vars. Instead of allowing a variable to be used outside the module, it would prevent the variable from being used outside the module.

        The underlying idea is good, but you'd need to use something other than use vars.

        Great idea. I'm pretty sure Perl 6 didn't adopt something like this.

        - tye