On Sat, Jun 26, 2010 at 6:08 PM, David Mertens <[email protected]> wrote:
> On Sat, Jun 26, 2010 at 3:47 PM, P Kishor <[email protected]> wrote:
>>
>> <snip>
>> On a related note... if I have my own function
>>
>> sub foo {
>>    my ($a, $b, $c, $piddle) = @_;
>>    do something to every element in $piddle based on $a, $b, $c
>> }
>>
>> I want to invoke it as a method call on my piddle $p, like so
>>
>> $p->foo($a, $b, $c)
>>
>> How do I construct that?
>
> This is question about writing object-oriented Perl. This is a whole new can
> of worms, but here are the basics.
>
> First, the method must take the piddle as its first argument. Second, the
> subroutine must be defined in the PDL package. Using these two ideas, you
> would instead have this:
>
> sub PDL::foo {
>   my ($piddle, $a, $b, $c) = @_;
>   # do something here
> }
>
> Defining the cod as such will allow you to write this code:
>
> @results = $piddle->foo($a, $b, $c);
>
> You may want foo to be both a function and a method. That is, you may want
>
> foo($piddle, $a, $b, $c)
>
> and
>
> $piddle->foo($a, $b, $c)

Thanks David. This is exactly what I wanted to know. I know how to
create an Perl class and write OO code, but I wasn't sure if
PDL-wizards had already provide some funky shortcut for doing so.

As it is, PDL continues to blow my mind. I frankly don't understand
why PDL is not a part of core Perl (besides being a pain-in-ass to
install), as I can't really imagine working with normal arrays ever
again, if I can help it.

Anyway, yes, now that I know that I have to write the OO code myself,
but once I write my method, PDL will automatically apply it to every
element in my piddle, that is do-able magic!

Thanks again. And, thanks to Glazebrook and Co. who created this. It
is waaaay more fun than anything else in Perl.


>
> to invoke your function. In that case, you need foo to be defined in both
> PDL's package as well as your current package. This is a big topic that will
> take more than a simple email to explain, so I will refrain. To learn more,
> check out this article, and then find a copy of Learning Perl or the camel
> book (Programming Perl) and read about Packages as well as Object Oriented
> Perl.
>
> Fun stuff! Keep your questions coming!
>
> David
>
> --
> Sent via my carrier pigeon.
>



-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
-----------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
=======================================================================

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to