Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl OO - combined static & instance method.

Reply
Thread Tools

Perl OO - combined static & instance method.

 
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-19-2010
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
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      10-19-2010
>>>>> "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 ---------
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      10-19-2010
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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-20-2010
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
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-20-2010
>>>>> "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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-20-2010
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_W...f_a_programmer
(for anyone who started to learn Perl in the last 5 seconds).
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      10-20-2010
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
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-20-2010
>>>>> "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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use windows and forma authentication combined kazazic@gmail.com ASP .Net 1 07-18-2005 01:50 PM
anyone else find errors in MeasureUp combined 70-292 / 70-296 cd simulations? Beoweolf MCSE 0 12-28-2004 07:40 PM
Decomposing combined characters Ian Pilcher Java 1 08-06-2004 05:08 PM
Combined repaints? Peter J. Seymour Java 6 07-12-2004 07:34 AM
GRRR! Pages submitting with Enter key, combined with .ASCX pages - help! Mike Kingscott ASP .Net 0 12-05-2003 02:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57