On Sat, Jun 26, 2010 at 6:18 PM, P Kishor <[email protected]> wrote:

> 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!
>

No no no! I misunderstood you. You were asking how to write a function that
would automatically thread. That is not trivial. At the moment we don't have
a way to auto-thread a Perl function. The only way to make a Perl function
'threadable' is to make sure of all its operations use threaded functions.
To write a (PDL-)threaded function, you have to use PDL::PP.

Sorry for the confusion.

David

P.S. As for being in the core, I think it would have issues with not working
on nearly all the platforms that pure Perl does. If we could manage that,
then maybe we could get PDL's core into Perl's core. But it would take work.
:-)

-- 
Sent via my carrier pigeon.
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to