Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: standard perl module and BEGIN block.

by BrowserUk (Patriarch)
on Aug 16, 2004 at 21:47 UTC ( [id://383469]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: standard perl module and BEGIN block.
by eric256 (Parson) on Aug 17, 2004 at 06:35 UTC

    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

      Yes. And in this code:

      package Some:Module; use vars q/test/; ## Here "Global symbol "$test" requires explicit package name". $test = 1; sub inHere { our $test; # ok. our $tset; # typos/thinkos causes errors. my $test; # my variable "$test" overrides variable with the same name at differe +nt scope... } 1; Some::Other::Module; sub inThis { our $test; ## is an error. }

      If it is possible to get this behaviour, changing use vars; to work this way would probably cause backward compatibility issues, so maybe use globals qw/$var @var %var/; would make more sense.

      That said, maybe something like $var : global = 1; would be more in keeping with the latest trends and prehaps more compatible with P6.


      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
Re^5: standard perl module and BEGIN block.
by ikegami (Patriarch) on Jun 12, 2010 at 19:00 UTC

    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.

Re^5: standard perl module and BEGIN block. (declaring vs. using globals)
by tye (Sage) on Jun 12, 2010 at 16:53 UTC

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

    - tye        

      Great idea.

      Really?

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

      I think it is still too early to say. The P6 spec seems more undefined now than it was when I posted that.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://383469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found