Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Objects in Objects

by Angel (Friar)
on Aug 23, 2003 at 02:23 UTC ( [id://285977]=perlquestion: print w/replies, xml ) Need Help??

Angel has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I was wondering can I call an object inside another object? I get an error of the method is to be called from main instead of the object.
package bar; use foo; sub new { my($type) = $_[0]; my($self) = {}; $self->{'new'} = 1; #error reporting and update vars $self->{error_type} = undef; $self->{error_string} = undef; bless($self, $type); return( $self ); } sub generate_form() { my $self = shift; my $foo = Foo->new; print $foo->do_something( "bunny" ); }

Replies are listed 'Best First'.
Re: Objects in Objects
by bobn (Chaplain) on Aug 23, 2003 at 02:59 UTC

    Not this way. You say use foo, then you call Foo->new - case matters.

    It looks sort of like you're trying to do inheritance, which is usually done more like:

    #!/usr/bin/perl -w package foo; sub new { my ($class, @stuff) = @_; my $self = {}; # do stuf with $self and @stuff... bless {}, $class; } sub do_something { my ($self, @parms) = @_; print "in do_something: $self, @parms\n"; } package bar; # if foo is in another file, also say "use foo;" here @ISA = qw/foo/; # "use base qw( foo );" can be used in place of the preceding 2 lines +in perl >= 5.004_04 (I think), certainly by 5.6.0. sub do_something_else { my ($self, @parms) = @_; print "in do_something_else, $self, @parms\n"; } package main; my $b = new bar; $b->do_something('stuff'); $b->do_something_else('stuff2'); __END__
    Results in:
    in do_something: bar=HASH(0x804ca30), stuff in do_something_else, bar=HASH(0x804ca30), stuff2
    new and do_something, not defined in bar, still work because they are inherited from foo. do_something_else is defined in bar. so it works too.

    All code given here is UNTESTED unless otherwise stated.

    --Bob Niederman, http://bob-n.com
Re: Objects in Objects
by Anonymous Monk on Aug 23, 2003 at 10:59 UTC
    Executing the code
    #!perl use strict; use warnings; package Foo; sub new { my $class = shift; bless [], $class; } sub doFoo { my $obj = shift; print "foo::doFoo => @_\n"; } package Bar; sub new { my $class = shift; bless [], $class; } sub doBar { my $obj = shift; my $nfoo = new Foo; $nfoo->doFoo(@_); print "Bar::doBar => @_\n"; } package main; my $b = new Bar; $b->doBar(qw(i feel lucky));
    I don't get your error. Do you really have the two lines
    use foo; ... my $foo = Foo->new;
    If so, just fix the case. If not, please post a small piece of code that reproduces the error you are getting (in this case probably two small modules and one small program).
    Oh, and remember: use strict; use warnings;.
    Cheers, CombatSquirrel.
      Don't forget to login before you post CombatSquirrel. :)

      This is why i tell everyone here to change their theme. GOTO your User Settings and scroll on down to 'Theme Configuration'. Change your theme to anything but the default theme. Now, when you bring up Perlmonks in your browser, if you see the default theme - you know you are not logged in. Also, while you are at your User Settings, feel free to add some custom CSS - i have added background images to mine. Know i really know if i am logged in or not!

      Another problem is accessing http://www.perlmonks.org vs. http://perlmonks.org - it is possible to set your cookie for one and not the other. This too, can cause you to think that you are logged in, but you aren't. Again, if you use another theme (especially one like the Dark Theme with a black background) you will know you are not logged in (unless you are drunk!). ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        Yeah, sorry, jeffa. I'm fiddling around with the CSS at the moment; the only problem is that I like the standard theme too much - I hate those overloaded web pages, or me, beauty lies in simplicity. I'll try to make the CSS affect only an unimportant portion of the page that I'll nevertheless notice. I just tried font[size="2"] { color:blue }, but that made all the replies colored blue - quite ugly.
        Well, thanks for your reminder anyways.
        CombatSquirrel.
        P.S.: Changed it to form { color:blue } - still sub-optimal, but a lot better.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://285977]
Approved by ybiC
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found