Perl OO - combined static & instance method.

Discussion in 'Perl Misc' started by RedGrittyBrick, Oct 19, 2010.

  1. An instance method in traditional Perl OO might be like this.

    Package Foo;
    ...
    sub bar {
    my $self = shift;
    my ($x, $y, $z) = @_;
    ...
    }

    I'd like to be able to invoke this either as a (static) class method
    $result = Foo::bar(1,2,3);

    or as an instance method
    my $foo = Foo->new(Foo::INVERTED);
    $result = $foo->bar(1,2,3);

    What is the usual idiom for checking for the presence of the class ref?.

    --
    RGB
     
    RedGrittyBrick, Oct 19, 2010
    #1
    1. Advertising

  2. RedGrittyBrick

    Uri Guttman Guest

    >>>>> "R" == RedGrittyBrick <> writes:

    R> An instance method in traditional Perl OO might be like this.
    R> Package Foo;
    R> ...
    R> sub bar {
    R> my $self = shift;
    R> my ($x, $y, $z) = @_;
    R> ...
    R> }

    R> I'd like to be able to invoke this either as a (static) class method
    R> $result = Foo::bar(1,2,3);

    that is not a method at all. that is a plain sub call. the only way to
    tell if it isn't a method is to check the first arg and see if it is a
    blessed object of that class.

    R> or as an instance method
    R> my $foo = Foo->new(Foo::INVERTED);

    that is a class method call

    R> $result = $foo->bar(1,2,3);

    that is an instance or object method call. those you can differentiate
    by looking at the first arg. but it is a BAD idea for one method to
    support both styles. there are idioms for allowing the new() (or other)
    methods work both ways but it is better to have different methods for
    each api style

    R> What is the usual idiom for checking for the presence of the class ref?.

    there is no standard way to tell a method call from a plain sub call as
    you never want to use the same sub both ways. if you could call a method
    without its object, what is the point of making it a method to begin
    with?

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Oct 19, 2010
    #2
    1. Advertising

  3. RedGrittyBrick

    Willem Guest

    RedGrittyBrick wrote:
    ) An instance method in traditional Perl OO might be like this.
    )
    ) Package Foo;
    ) ...
    ) sub bar {
    ) my $self = shift;
    ) my ($x, $y, $z) = @_;
    ) ...
    ) }
    )
    ) I'd like to be able to invoke this either as a (static) class method
    ) $result = Foo::bar(1,2,3);
    )
    ) or as an instance method
    ) my $foo = Foo->new(Foo::INVERTED);
    ) $result = $foo->bar(1,2,3);
    )
    ) What is the usual idiom for checking for the presence of the class ref?.

    That there above is not a static class method call. This is:

    $result = Foo->bar(1,2,3);


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Oct 19, 2010
    #3
  4. On 19/10/2010 18:01, Uri Guttman wrote:
    >>>>>> "R" == RedGrittyBrick<> writes:

    >
    > R> An instance method in traditional Perl OO might be like this.
    > R> Package Foo;
    > R> ...
    > R> sub bar {
    > R> my $self = shift;
    > R> my ($x, $y, $z) = @_;
    > R> ...
    > R> }
    >
    > R> I'd like to be able to invoke this either as a (static) class method
    > R> $result = Foo::bar(1,2,3);
    >
    > that is not a method at all. that is a plain sub call. the only way to
    > tell if it isn't a method is to check the first arg and see if it is a
    > blessed object of that class.
    >
    > R> or as an instance method
    > R> my $foo = Foo->new(Foo::INVERTED);
    >
    > that is a class method call
    >
    > R> $result = $foo->bar(1,2,3);
    >
    > that is an instance or object method call. those you can differentiate
    > by looking at the first arg. but it is a BAD idea for one method to
    > support both styles. there are idioms for allowing the new() (or other)
    > methods work both ways but it is better to have different methods for
    > each api style
    >
    > R> What is the usual idiom for checking for the presence of the class ref?.
    >
    > there is no standard way to tell a method call from a plain sub call as
    > you never want to use the same sub both ways. if you could call a method
    > without its object, what is the point of making it a method to begin
    > with?
    >


    Thanks for clearing up my misunderstanding.

    The background is that I have a handful of programs, the earlier ones
    have some subroutines in common so these were moved into a common
    procedural module. A set of programs written later use an OO version of
    the module. I wanted to merge the OO and procedural modules whilst
    delaying the refactoring of the earlier programs.

    --
    RGB
     
    RedGrittyBrick, Oct 20, 2010
    #4
  5. >>>>> "RedGrittyBrick" == RedGrittyBrick <> writes:

    RedGrittyBrick> The background is that I have a handful of programs, the
    RedGrittyBrick> earlier ones have some subroutines in common so these
    RedGrittyBrick> were moved into a common procedural module. A set of
    RedGrittyBrick> programs written later use an OO version of the
    RedGrittyBrick> module. I wanted to merge the OO and procedural modules
    RedGrittyBrick> whilst delaying the refactoring of the earlier programs.

    You should have said this first. The solution is simple:

    Use a global substitution on the older programs to replace
    "Your::Module" with "Your::Module::Legacy". Then create a subroutine in
    that package that invokes the correct method in "Your::Module".

    Simple.

    Most people work too hard. :)

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
     
    Randal L. Schwartz, Oct 20, 2010
    #5
  6. On 20/10/2010 14:05, Randal L. Schwartz wrote:
    >>>>>> "RedGrittyBrick" == RedGrittyBrick<> writes:

    >
    > RedGrittyBrick> The background is that I have a handful of programs, the
    > RedGrittyBrick> earlier ones have some subroutines in common so these
    > RedGrittyBrick> were moved into a common procedural module. A set of
    > RedGrittyBrick> programs written later use an OO version of the
    > RedGrittyBrick> module. I wanted to merge the OO and procedural modules
    > RedGrittyBrick> whilst delaying the refactoring of the earlier programs.
    >
    > You should have said this first.


    I should. The trouble was I half remembered something of the sort that
    Sherm helpfully pointed out. Plus I hadn't thought through the implications.


    > The solution is simple:
    >
    > Use a global substitution on the older programs to replace
    > "Your::Module" with "Your::Module::Legacy". Then create a subroutine in
    > that package that invokes the correct method in "Your::Module".
    >
    > Simple.


    My thoughts had turned to writing wrappers, thanks for the additional
    organisational hints.


    > Most people work too hard. :)


    I obviously still have to work on increasing my laziness, impatience and
    hubris†.

    --
    RGB
    † http://en.wikipedia.org/wiki/Larry_Wall#Virtues_of_a_programmer
    (for anyone who started to learn Perl in the last 5 seconds).
     
    RedGrittyBrick, Oct 20, 2010
    #6
  7. RedGrittyBrick

    Willem Guest

    Randal L. Schwartz wrote:
    )>>>>> "RedGrittyBrick" == RedGrittyBrick <> writes:
    )
    )RedGrittyBrick> The background is that I have a handful of programs, the
    )RedGrittyBrick> earlier ones have some subroutines in common so these
    )RedGrittyBrick> were moved into a common procedural module. A set of
    )RedGrittyBrick> programs written later use an OO version of the
    )RedGrittyBrick> module. I wanted to merge the OO and procedural modules
    )RedGrittyBrick> whilst delaying the refactoring of the earlier programs.
    )
    ) You should have said this first. The solution is simple:
    )
    ) Use a global substitution on the older programs to replace
    ) "Your::Module" with "Your::Module::Legacy". Then create a subroutine in
    ) that package that invokes the correct method in "Your::Module".

    Why not use a global substitution to replace
    "Your::Module::(\w+)" with "Your::Module->$1" ?

    This should work as long as there are no module variables in use, no ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Oct 20, 2010
    #7
  8. >>>>> "Willem" == Willem <> writes:

    Willem> Why not use a global substitution to replace
    Willem> "Your::Module::(\w+)" with "Your::Module->$1" ?

    Might miss imports. Mine'll work either way.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
     
    Randal L. Schwartz, Oct 20, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    9
    Views:
    11,277
    Jezuch
    Jul 3, 2003
  2. Sam
    Replies:
    3
    Views:
    254
    Gerard Flanagan
    Mar 10, 2008
  3. David Garamond
    Replies:
    5
    Views:
    271
    Ara.T.Howard
    Jun 8, 2004
  4. Raj Singh
    Replies:
    2
    Views:
    212
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    263
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page