Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Examples fo Where "our" is really needed

by ihb (Deacon)
on May 05, 2005 at 14:50 UTC ( #454391=note: print w/replies, xml ) Need Help??


in reply to Re: Examples fo Where "our" is really needed
in thread Examples fo Where "our" is really needed

I prefer

$VERSION = '1.00';
over any of your alternatives above. Now you might wonder how I handle strict. Fact is I don't. I don't have stricture enabled until I have done the assignment to $VERSION and other globals I don't plan to type again.

being able to localise access to scary things like globals to just where I need them

Just a clarification. I've seen people declare the same variable twice but one should not do

sub foo { our $foo; ... } sub bar { ... } sub baz { our $foo; # BAD! ... }
to avoid access to $foo inside &bar. Use some other scope trick for that if it's needed. One misses a great point of having the declaration if one does it more than once for the same variable.

ihb

See perltoc if you don't know which perldoc to read!

Replies are listed 'Best First'.
Re^3: Examples fo Where "our" is really needed
by TimToady (Parson) on May 05, 2005 at 17:49 UTC
    The whole point of the word "our" in English is that it indicates shared property, and more than one person us allowed to say it about the same object without an argument breaking out. In Perl the word "our" does not declare a variable, but a private view of a public variable. It is quite properly limited to the lexical scope that uses the view. Sure, you can throw two functions into the one artificial lexical (or class) scope to share one "our" declaration, but that's a trick that doesn't scale well to multiply intersected sharings. Even Fortran got that one right.

      In Perl the word "our" does not declare a variable, but a private view of a public variable.

      That's why I said "declare the same variable twice" and meant exactly the same as you do but with different words. Regardless how one choose to paint the issue I don't like to do the same thing twice if I don't have to or have a good reason not to. If I can't use regular ways of scoping access to a global then I can't, but if I can, as in my example above, I really prefer to not repeat myself. The day I refactor my code and/or mistype the name in one "repeated" our I might very well be hunting reasons for my uninitialized warnings instead of getting the help from the compiler I could've gotten. I'm unfortunately not just hypothesizing.

      our has its uses, but I think that scattering out ours to tighten down the access scope for globals instead of create new scopes when that's possible/not inconvenient is an abuse rather than a use of our.

      ihb

      See perltoc if you don't know which perldoc to read!

Re^3: Examples fo Where "our" is really needed
by adrianh (Chancellor) on May 05, 2005 at 15:06 UTC
    Fact is I don't. I don't have stricture enabled until I have done the assignment to $VERSION and other globals I don't plan to type again.

    Fair enough. I just can't be bothered to spend my limited brain power wondering about which globals I'll just use once, or bother spending time switching between methods if I change my mind later - so I just use our pretty much all of the time :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-10-31 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (286 votes). Check out past polls.

    Notices?