in reply to code ref

The problem here is a method is just a function - one that takes an extra first parameter. You could do something like $mw->Button(command => $self->can('func1'))->pack; but then the method will be called as a simple function without $self passed to it - nor of course any other parameters you might need. So if you want to stick by this route, you'll have to pass a closure, as tadman suggested: $mw->Button(command => sub { $self->func1($a, @_) })->pack;

Keep in mind that OO in Perl is different from OO in other languages. One advantage that Perl offers is that it has closures; using them, you can emulate just about any behaviour you want to.

I find OO in Perl is actually more flexible and powerful than OO in OO languages, with one painfully significant exception: no clean methodology to data inheritance.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: code ref
by demerphq (Chancellor) on Nov 11, 2002 at 11:49 UTC
    no clean methodology to data inheritance.

    There is Tie::SecureHash for this purpose. But it is essentially a workaround for the issue you mention.

    --- demerphq
    my friends call me, usually because I'm late....

      Unfortunately, that solution suffers from the same fundamental problem: a derived class still has to know that $self is a hashref (in this case a blessed one, but that doesn't matter for our purposes). Without using Abigail's inside out objects trick, you cannot implement a subclass in Perl without knowing at least this one detail about the implementation of the superclass - what kind of thing $self is and how to store instance data in it.

      Makeshifts last the longest.

Re^2: code ref
by adrianh (Chancellor) on Nov 11, 2002 at 12:44 UTC
    no clean methodology to data inheritance

    Abigail-II's inside out objects trick is one solution to this. I'm still not sure if this is extremely elegant or an evil hack (or both :-)

    Bring on perl6 - hopefully it will make OO vaguely sane!

      It's an elegant hack. I was aware of it, but I don't like it: it relies on the stringified value of references which you're always advised to avoid. Yet it is necessary if you want to encapsulate fully, since there's simply no alternative way to do that in Perl 5.

      Makeshifts last the longest.