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

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?

Replies are listed 'Best First'.
Re^5: how to export a method
by AnomalousMonk (Archbishop) on Oct 22, 2019 at 06:36 UTC
    ... 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!
Re^5: how to export a method
by wanna_code_perl (Friar) on Oct 22, 2019 at 05:56 UTC

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