[tpm] Perl module question
Richard Dice
rdice at pobox.com
Fri Sep 7 09:58:16 PDT 2007
Madison,
The reason why you are experiencing this is because you are confusing
procedural syntax & semantics with object-oriented syntax and semantics.
Here is object-oriented.
use My::Module;
my $object = My::Module->new;
my $result = $object->method(@args);
Internally within My/Module.pm it probably looks something like:
package My::Module;
sub new {
my $class = shift;
return bless {}, $class;
}
sub method {
my $self = shift;
my @params = @_;
... # do stuff here
return $value;
}
Notice the "my $self = shift;" and the "my $class = shift;" lines. You need
to understand what "->" means.
I could write up a huge email on this subject but really that's a waste of
effort when merlyn has already done it. Please read:
http://perldoc.perl.org/perlboot.html
It is exactly about these concepts.
Also, do you really want to do OO perl? Maybe procedure Perl modules would
work for your purposes.
Cheers,
Richard
On 9/7/07, Madison Kelly <linux at alteeve.com> wrote:
>
> Hi all,
>
> I am quite the module n00b, so please be gentle. :)
>
> I've got a few small, simple modules I've written that handle some
> simple functions I use often. What I want to be able to do though is:
>
> my $foo=My::Module->new();
>
> Then be able to call the function/subroutines in there like this:
>
> my $value=$foo->function($var1, $var2);
>
> I had tried to do:
>
> my $value=My::Module->function($var1, $var2);
>
> But the first value picked up in the function is the module name... I
> don't think I should have to say in the module
>
> sub function
> {
> my ($null, $var1, $var2)=@_;
> ...
> }
>
> In order to do this. I don't like just calling the function name (as
> I currently do) because some of the funtion names are ambiguous. For
> example, one module is used to validate form values. So the function
> 'phone', 'int_num', 'real_num' and so on are provided by 'NV::Validate'.
> If I want to validate the phone number then, I'd call:
>
> my ($valid)=phone($form_phone_num);
>
> This is fairly ambiguous, and I'd much rather say either:
>
> my $validate=NV::Validate->new();
> my ($value)=$validate->phone($form_phone_num);
>
> Or
>
> my ($value)=NV::Validate->phone($form_phone_num);
>
> But not have the first variable passed being the module name.
>
> Any tips, pointers or such? Would it help to show some code samples from
> my module?
>
> Thanks!
>
> Madison
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/toronto-pm/attachments/20070907/6431d575/attachment.html
More information about the toronto-pm
mailing list