Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Default Scope Behaviour

by NeoPerl (Acolyte)
on Jan 01, 2001 at 00:25 UTC ( [id://49122]=perlquestion: print w/replies, xml ) Need Help??

NeoPerl has asked for the wisdom of the Perl Monks concerning the following question:

I have learnt that by default, the scope of all variables is global. If you want to use local scope, either use my() or local() function. If you use use strict, then you must use my() for each variable.

Now is there any way this behaviour be altered in such a way that
1) By default all the variables have local scope (no need of my()).
2) We should be able to use a keyword (say global()) to declare a variable having global scope() ?

I have seen Perl to be a flexible language with everything else, so I expect there must be some way to do this too. But which?

Replies are listed 'Best First'.
Re: Default Scope Behaviour
by dchetlin (Friar) on Jan 01, 2001 at 06:54 UTC
    While Dominus is in part right that the discussion on perl6-language-strict could have been more productive, there are indeed issues that make this less than easy to do.

    I'm working on a paper for this, so I don't want to give everything away early, but the basic upshot is that there are three languages often mentioned in the same breath as Perl that do make variables lexical by default, each in different ways. And, as you might imagine, each of them has their own problems.

    Python has only two real scopes -- global and nearest-enclosing-block lexical. If Perl went this way, we would lose a *lot* of flexibility.

    Ruby does a better job with scopes, but it is very easy to create action-at-a-distance problems. I.e. you write a function that has a tightly enclosed lexical scope. A year later, you add a variable 300 lines above it that happens to have the same name as a variable in that tight lexical scope, and all of the sudden the two variables are the same -- you've inadvertently changed the scope of a lexical.

    Tcl gives the most flexibility of the three, but you end up with all kinds of `upvar' nonsense to try and place variables in the scopes you want them. Easy to screw up and also very verbose.

    In sum, there are possibly better ways to do it than the way Perl does it now, but figuring out the semantics that are best is far from easy.

    I hope to finish my paper sometime soon, and if people are interested I'll post it here for comments before I make it public. It will hopefully be able to suggest a reasonable semantic for Perl 6.

    -dlc

Re: Default Scope Behaviour
by Dominus (Parson) on Jan 01, 2001 at 02:06 UTC
    Says NeoPerl:
    Now is there any way this behaviour be altered in such a way that

    1) By default all the variables have local scope (no need of my()).

    2) We should be able to use a keyword (say global()) to declare a variable having global scope() ?

    There was some discussion of this around Perl 6, but it was abandoned. My recollection was that it was dumped partly because it presents real technical problems and partly out of hidebound conservatism and pigheadedness, but I forget.

    The discussion is archived here. In my opinion, the discussion was unusually thoughtless, and can stand as a good example of what was wrong with the Perl 6 RFC discussion in general.

      Thanks for pointing that out. I found the discussion thread with less than 5-6 messages in it. Haven't yet even the first thread fully, but I can see a paragraph stating something like "How stupid and complicated will this addition make the above{} 5 lines of code look like...." Well, this kind of discussion (which concerns of things looking stupid rather than useful) should not fit a RFC discussion IMHO.
Re: Default Scope Behaviour
by epoptai (Curate) on Jan 01, 2001 at 20:53 UTC
    2) We should be able to use a keyword (say global()) to declare a variable having global scope()?

    Isn't this what use vars is for?

      I looked up "use vars", and it should be able to provide much convenience for that sort of "laziness". :-)
      (However, it is nearly half the solution anyway. You still have to explicitly declare each variable as local scope.)

      Thanks for pointing that out.

        Tastes may differ. Personally I consider the combination of strict, vars, and my to be enough structure to be useful as a spellchecker without having enough bureaucracy to be annoying.

        Which means that if you did get the feature that you want, I would want the option of being able to turn it off.

Re: Default Scope Behaviour
by coreolyn (Parson) on Jan 01, 2001 at 00:55 UTC

    Sounds like a good example of 'intent of laziness', but it kinda defeats Perl's Laziness.

    --- Insert Monk Level Disclaimer Here --- IMH(low expirienced, but perl devoted) opinion: --- End Disclaimer Here ---

    To do as you say defeats the beauty of Perl laziness at the system level. The default Perl laziness should be the one that gives the most control over the OS as the default.

    coreolyn Two cents is worth at the least, two cents.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-20 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found