Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Our, use vars, and magic, oh my!

by arhuman (Vicar)
on Aug 23, 2001 at 17:58 UTC ( [id://107325]=note: print w/replies, xml ) Need Help??


in reply to Our, use vars, and magic, oh my!

The only thing that bothers me is that we have at the same time :
  • 'use vars' and 'our' are equivalent in the sense that they make a variable reachable from outside of the package.
  • 'our' make a variable lexically scoped.
I have no problem with the first point (I've checked it several times).

But I thought that lexically scoped vars couldn't be accessed from the outside
beccause they weren't in the package's namespace (but were in a 'scratchpad').

Did I wrongly assume that 'lexically scoped' implied the use of a 'scratchpad'
(was it rather 'my var' implies use of scratchpad)
or does it mean that despite being lexically scoped a 'our' variable is also 'exported' in the namespace ?

Anyone to clear this point ?


"Only Bad Coders Code Badly In Perl" (OBC2BIP)

Replies are listed 'Best First'.
Re: Re: Our, use vars, and magic, oh my!
by arturo (Vicar) on Aug 23, 2001 at 18:14 UTC

    The fact that a variable is lexically scoped doesn't imply anything about the internal implementation. Lexical scoping is more a matter of syntax than anything else.

    According to the man page perlfunc:our or perldoc -f our, you can see that what the package Foo; our $foo; declaration does is get the interpreter to see occurrences of $foo unadorned, as it were, as occurrences of $Foo::foo, so it's a plain ol' package global:

    An "our" declaration declares a global variable that will be visible across its entire lexical scope, even across package boundaries. The package in which the variable is entered is determined at the point of the declaration, not at the point of use.

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found