http://qs321.pair.com?node_id=61819


in reply to Re: Nested Classes
in thread Nested Classes

Here's a slightly more verbose version of the same thing:
#!/usr/bin/perl -w use strict; package foo; sub new{ my $m = {}; bless $m; return $m;} package bar; sub new{ my $b = {}; bless $b; return $b;} sub corny { return 'sparky'; } package main; my $s = foo->new(); my $r = bar->new(); $s->{'spark'} = \&bar::corny; print $s->{'spark'}(); 1;
This essentially calls a method out of a different package. I added the class stuff as window dressing. If you are looking to call and actual method from an instantiated object, just call a function and use a reference to the object as a parameter (called delegation in certain circles) to call the function. Lastly, use inheritance. If these things don't help, I am not sure what you are attempting to accomplish.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re: Nested Classes
by Tyke (Pilgrim) on Mar 05, 2001 at 13:23 UTC
    The OP asked how to
    1. create an object,
    2. bless,
    3. and then bind subroutines to it (written within another objects method) as methods.

    That's what I tried to show. Sure it's better to define classes statically. But as I understood the OP, he wanted to do this on the fly