Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Creating objects within objects being strange...

by mr.nick (Chaplain)
on Sep 07, 2001 at 16:04 UTC ( [id://110867]=note: print w/replies, xml ) Need Help??


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

This comment is unrelated to your question, but I thought I'd mention something quickly that I've picked up recently and found very useful.

Consider writing your new functions something like this:

sub new { my $proto=shift; my $class=ref $proto || $proto; my $self={ @_ }; ## ... bless $self,$class; }

Then create your instance like this:

my $inst=new MyClass( name => 'mr.nick', rank => 'novice', sleep_level => 'not enough' );
I found that by directly putting the class parameters on the new() line like that makes for cleanliness, and readability. And, it makes for a real quick method of stuffing values into the instance of the object.

mr.nick ...

Replies are listed 'Best First'.
Re: Re: Creating objects within objects being strange...
by demerphq (Chancellor) on Sep 07, 2001 at 18:54 UTC
    I would MUCH prefer to see the below.
    my $inst=MyClass->new( name => 'mr.nick', rank => 'novice', sleep_level => 'not enough' );
    The notation is easier to read, and less susceptable to errors. Also it is more intuitive. It is clear the function is called is 'new' and that new() resides in the class/package 'MyClass' and that 'MyClass' will be passed to new() as its first parameter, along with the parameters supplied.

    The other notation is much less clear. When I see a bareword followed by parameters in parenthesis my first thought is that it is a function being called. Which is as we all know NOT what is happening.

    BTW: This is one of my pet peeves, can you tell? :-)

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

      Heh, well, yes, you certainly aren't alone in your feeling about that ;) I guess me writing it as "new Object" instead of "Object->new()" is a leftover from my C++ days and from the fact that I (apparently) am a Java developer now :-/ (which, one of these days, I'm going to write a nice meditation on the comparitive differences between the two ... I have to admit, I like Java... it makes OO programming in perl seem very clunky).

      mr.nick ...

        Id be interested in reading a comparison. Ive only perused Java, not used it and quite frankly it seemed on such a brief overview to be quite clunky at least in a pedantic way. All the get/set nonsense and the like. I really like the way in perl a sub can metamorphize itself depending on the context and usage. I admit that its implementation is open to serious error if care is not taken, but on the other hand flexibility does have a cost. :-)

        As ive said though my experience of Java is minimal to say the least so an overview, maybe written from the point of view of moving from perloo to javaoo would I am sure be very much apreciated by myself and the rest of the non-java using monk community.

        In short i look forward to reading it.. /msg me when its done please? :-)

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

      Okay, I will change it - I see what you mean. It wont solve my problem though... and the classname is not being passed for some reason when I call it from within the damn routine.

      I hope to make a simple example I can post tonight and hopefully solve the (i assume) stupid mistake I am making

      Later

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-16 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found