in reply to Re: how to export a method
in thread how to export a method

Thank you very much for your reply! But when I added the require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(rp); to CV.pm like this:
package PDL::CV; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(rp); sub rp { my ( $self, $input ) = @_; $self .= $input->reshape($self->dims); return $self; } sub new { my $class = shift; my $self = { _input => shift, }; bless $self, $class; return $self; } 1;
I also got the message: Can't locate object method "rp" via package "PDL" at test_pdl6.pl line 27.

Replies are listed 'Best First'.
Re^3: how to export a method
by wanna_code_perl (Friar) on Oct 22, 2019 at 03:48 UTC

    Ah, I completely missed the fact that you're mixing Exporter with OO programming. My mistake for not spotting that sooner. But the error message you're getting has nothing with Exporter, because object methods don't need to be (and should not be) exported, because there's no reason to pollute the caller's namespace when object methods can be called regardless. The real problem is evident in the error message you get:

    Can't locate object method "rp" via package "PDL" at test_pdl6.pl line 27.

    Is for the class "PDL", rather than your "PDL::CV" package where the rp method is defined. The problem must lie with your test (caller) code in test_pdl6.pl, because aside from the needless use of Exporter, your package's code looks reasonable.

    This should have been my top level reply.

      Thank you so much for your kind reply. I understand your reply. I think I should change my question, is: can we add new methods to a exist object in a package?
        ... can we add new methods to a exist object in a package?

        Update: WRT choroba's reply here, this reply addresses Perlish OO in general.

        Yes, it's a new topic, but...

        c:\@Work\Perl\monks>perl use 5.014; # needs package NAME BLOCK syntax use strict; use warnings; package Foo { use strict; use warnings; sub new { my $class = shift; return bless { 'hi' => 'lo' } => $class; } sub method { my $self = shift; my ($k) = @_; return exists $self->{$k} ? $self->{$k} : 'unknown'; } } my $of = Foo->new; printf "A: '%s' \n", $of->method('hi'); printf "B: '%s' \n", $of->method('xx'); printf "C: '%s' \n", $of->oh_by_the_way('hi'); printf "D: '%s' \n", $of->oh_by_the_way('xx'); printf "E: '%s' \n", $of->method('hi'); sub Foo::oh_by_the_way { my $self = shift; my ($k) = @_; $_ .= $_ for values %$self; return exists $self->{$k} ? $self->{$k} : $self->method($k); } __END__ A: 'lo' B: 'unknown' C: 'lolo' D: 'unknown' E: 'lolololo'
        The basic idea is that a subroutine in any package/namespace can be defined from within any other package/namespace if the newly-defined symbol is fully qualified.


        Give a man a fish:  <%-{-{-{-<

        You should probably just start a whole new question for that, as it is quite a bit different from the one you asked already.

      OK, Thank you so much !