--- Peter Cline <[EMAIL PROTECTED]> wrote:>
> >ok, so the routine is in main:: namespace?
> 
> Is it?  Does require put its arguments into the namespace from which
> it was  called?

Yep, I think so. 
  require 'x.pl';
is much the same as
  eval `cat x.pl`;

Yes?

> > > use NYT::Cnxdb;
> > > my $cnxdb =
> > >
> Cnxdb->new($conf{cnxdbUser},$conf{cnxdbPort},$conf{cnxdbTimeout});
> > > die("Unable to connect to database: " . $cnxdb->getlasterror())
> > >     unless $cnxdb->getok();a
> > > #cnxdb is the object in question.  It is in the main package of
> the
> > > module file
> >
> > You say "main package"....
> > Is there a package statement in the file?
> > It's a my() variable, but if it's a reference to some dataspace,
> > then returning the reference makes the dataspace accessible through
> > the reference.....
>
> There are several package statements in the file, but not one to
> declare main.  I thought this was the default if no other package has
> been declare.

I believe so.

> sub new_request_form lives in a separate namespace, allocated by 
> a package declaration.
> 
> > aha?
> > $cnxdb is a my() variable created in another function. It doesn't
> > exist here unless it was passed in, which it doesn't look like it
> > was. Does this file also have a "use strict;"?
> 
> This is curious because, as you can see in the code I sent (see
> below)  I use the $cnxdb object in this namespace without having
> passed it in. 

But is it the same $cnxdb?
If there's no strict pragma in effect, then using it would
"auto-vivify" it into the current namespace, but no value.
The $cnxdb created in the other function was lexical -- a variable
created with my(), whose existence is *ONLY* in that namespace. You
could pass that value elsewhere, and that would increase it's reference
count, which would keep it from being garbage-collected....

Let's look at an example.

  sub new {
      {}
  }

This returns an anonymous hash reference.

 sub tst {
    my $h = new();
 }

This creates a variable named $h and assignes it the reference returned
from new(). Then the function exits, *and $h goes away*.

 $h->{x} = 1;

This is NOT the $h from tst()!
Add a "use strict" statement to the file, and it'll tell you
  "Global symbol "$h" requires explicit package name".....

A variable created with my() doesn't exist beyond it's current scope.
When you create an object and return it, you return a *reference*, and
so the reference count of the object is incremented. When you go out of
that scope, the count is decremented, but still won't be zero, because
you returned a reference that something is still using. 

> The only place it is declared is as a lexical in the main::namespace

Nope. Lexicals are in NO namespace. They're lexicals. Only globals are
in namespaces, which is why they're global.

> of the same file.  This subroutine lives in the file in which the
> object was created.  

But in another function. $the variable doesn't exist outside that
function unless explicitly passed, since it was created with my().
 
> > > sub select_people {
> > >    my ($cnxdb,$responsibility) = @_;  #lexical copies
> >
> >Here you passed it in, so if you had it to pass, I think it'd work.
> 
> So why is it that I can use an object in a namespace in which it was
> not declared, but cannot pass the same object from there?

Because where you used it, it was expressly passed to you.
The constructor does just that; it creates a scoped variable, then
passes back a reference. In effect, the scalar that we call the object
is really only holding the address of where to find it, and some info
about it. The actual memory space is still in the original scope of the
constructor. =o)

Blessing it is a way for methods to be called on it, because they're
stored in a package, a namespace, and the bless() adds information to
the object that tells Perl where to find it's methods. There's other
stuff going on, but that's a simple explanation. Somebody'll correct me
on the imprecisions. ;o]

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to