Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Creating objects within objects being strange...

by Vuud (Novice)
on Sep 07, 2001 at 08:13 UTC ( [id://110827]=note: print w/replies, xml ) Need Help??


in reply to Re: Creating objects within objects being strange...
in thread Creating objects within objects being strange...

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"

  • Comment on Re: Re: Creating objects within objects being strange...

Replies are listed 'Best First'.
Re: Re: Re: Creating objects within objects being strange...
by demerphq (Chancellor) on Sep 07, 2001 at 13:59 UTC
    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)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found