Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: how to export a method

by wanna_code_perl (Friar)
on Oct 22, 2019 at 03:48 UTC ( [id://11107812]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: how to export a method
by toothedsword (Sexton) on Oct 22, 2019 at 05:45 UTC
    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:  <%-{-{-{-<

        Great! It is work and simple. Thank you so much!

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

Re^4: how to export a method
by toothedsword (Sexton) on Oct 22, 2019 at 06:05 UTC
    OK, Thank you so much !

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found