in reply to Creating objects within objects being strange...

Two things: First, you can always tell what package your object is actually blessed into with a ref $object. Second, check out perldoc -f bless. Instead of creating an object in your package you're creating it (somehow) in the 'main' package. Here's an example that replicates your problem:
#!/usr/bin/perl use strict; my $main_object = bless( {} ); print "\$main_object is of type (", ref $main_object, ")\n"; $main_object->{blah} = 'blah'; $main_object->get_session();
Running this gives me:
Can't locate object method "get_session" via package "main" (perhaps you forgot to load "main"?) at bless_main.pl line 8. $main_object is of type (main)
If you want to avoid surprises like this, always use the two-argument form of bless, like:
my $object = bless( {}, 'My::Object' );
Or more commonly:
sub new { my ( $item ) = @_; my $class = ref $item || $item; return bless( {}, $class ); }
Hope this helps.

Chris
M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: Creating objects within objects being strange...
by Vuud (Novice) on Sep 07, 2001 at 08:13 UTC
    I was using the two arguement form... the only difference is that I was not doing an my $class= ref $item || $item;

    I was just taking the $class off the @_ first. The problem is that it is coming through undefined for some reason or another.

    Here is what I am using now:

    sub new {
            my $item = shift;
    	my @args = @_;
            my $self = {};
            my $class= ref $item || $item;
            print $class;
            bless($self, $class);
            $self->Session::init (@args);
            return $self;
    };
    

    The call I am making to it is like:

    my $sessionmanager = new SessionManager();

    Still $class is undefined.

    "I'm never going to work another day in my life. The Gods told me to relax... I'm gonna be hooked up right"

      Hi Vuud,

      Theres something here that I dont grok. more specifically the line:

      $self->Session::init (@args);
      Now maybe I have missed something, but I dont understand what the above is supposed to do. Session::init I presume is a subroutine in the Session package. So why are you calling it against $self? Since you havent told us what package the new is from its hard to tell what you are up to.

      I think you have some basic misunderstandings that would be cleared up by reading perltoot perlboot or perlobj. But ill try to give you an overview here.

      In perl you can call a method in two ways. The first is a hangover to make C++ types feel more cozy the other way is the correct way to do it.

      my $obj=new Some::Module; # ok but not so cool my $obj=Some::Module->new(); # TWTDI
      You can see how this differs to the above sample from your new(). Im not even going to try to figure out how perl is interpreting your statement up there, but I am virtually certain its not doing what you think its doing

      Anyway here is how I expected your new to look (and this is straight from perltoot)

      package Session; #.... sub new { my $proto=shift; my $class=ref($proto) || $proto; my $self=bless {}, $class; # the below line assumes that this new is from # the Session class $self->init(@_); #no need to copy @_ return $self; };
      Now from this point on all the help I can give is conjecture as your OP makes assumptions about what we know about things we have never seen (ie your code, and object model). Remember we havent been fighting/living/breathing this stuff for days so things that you take for granted we dont even know... :-) For instance what happens if you dont pass a Session ID to StartSession?

      Nevertheless here is what I think you might need,

      package SessionManager; #.... sub MakeNewSession { my $self=shift; my $session=Session->new() || die "Failed to construct Session!"; my $id=$session->SessionID; die "SessionID returned an undef value!" if !defined($id); print "New session has ID of ".$i; return $session; }
      So the above sub will create a new session object print out its id and return it. I hope I havent misunderstood your question/problem and that this helps

      Dont forget to read perltoot perlboot or perlobj. PerlToot and PerlBoot are very good tutorials and much about Perl let alone OOPerl can be learned from both.

      Yves
      --
      You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: Re: Creating objects within objects being strange...
by Vuud (Novice) on Sep 07, 2001 at 08:24 UTC
    I should also note, that the Session package runs fine against my test file. Same call to it and everything.

    It tests all the functions and it runs clean - no warnings or anything.

    "I'm never going to work another day in my life. The Gods told me to relax... I'm gonna be hooked up right"

      Okay - after poking and proding it with various sticks, I have determined that when I call the new constructor in the session object, it is actually calling the new constructor in the SessionManager class.

      I cannot get it to point at the session object - no matter what I do, or syntax I use, it constantly goes at the new in the same object (as if I was doing $self->new).

      Its also not passing the class. The damn thing works fine from the test module!

      "I'm never going to work another day in my life. The Gods told me to relax... I'm gonna be hooked up right"