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)
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 <http://www.webreference.com/programming/perl/modules/>,
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.
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl