Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: strict and variable scopes

by oubiwann (Sexton)
on Mar 20, 2002 at 16:12 UTC ( [id://153014]=note: print w/replies, xml ) Need Help??


in reply to strict and variable scopes
in thread strict problem

An important thing to keep in mind, is that you are defining a lexical variable with my @in. This means it will only be in scope within that block, unless you use a method like flocto mentions above. Though it can be useful to subvert perl (sometimes essential?), one should always consider what is really trying to be accomplished and whether that subversion is truly necessary.

There may be a semantics problem here, too. One of the reasons that my was introduced to perl to was to give perl true local variabls, as local was a work around using a technique of substituting globals. What you would be telling perl is "please make this a local variable, but then don't be strict with me and let me use it anywhere I want without chiding me" (I hope that doesn't sound too harsh!). If you really want global variables, you probably want to use what perl terms as "package" variables:

Instead of using my, you would declare the package that you are in (probably just "main" in this case) like so:
$main::in = ...
Hope that helps. By the way, there are all kinds of nodes here discussing local, my, our etc., and how various monks deal with scoping. Best of luck!

Replies are listed 'Best First'.
Re: Re: strict and variable scopes
by Anonymous Monk on Mar 20, 2002 at 18:17 UTC
    An important thing to keep in mind, is that you are defining a lexical variable with my @in. This means it will only be in scope within that block, unless you use a method like flocto mentions above.

    But if I call an external procedure, won't this procedure inherit the environment of it's caller - and therefor all of it's variables be within the scope of that very block?

      Short answer: No, it won't.

      Long answer: It won't inherit lexical variables. That's the point. If you localize a global name, that change will be visible to all functions called within the scope of the change.

      That won't help you here, though, as you first need to declare a global name with the vars pragma.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://153014]
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: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found