Hello my esteemed fellow monks
Iīve been coding Perl for a couple years already, but now I feel itīs time to convert my Perl4-style custom libraries into real .pm modules. The main reason is because Iīm implementing mod_perl and it seems Apache::Reload is only guaranteed to track updates on module-like 'libraries', called with use.
Iīve been through the manpages and also the Camel book, but the OO literature just feeks like Greek to me. Just canīt figure out how to do it! I get lost in the therminology real fast. So I wonder if you guys can give me and applied explanation, say, about how would be a module created after library that looks like this:
[mylibrary1.pl]
sub half {
my $number = shift;
return $number / 2;
}
The main issues are:
1) Ok, I know I have to save the file as .pm and declare first thing the name of the package
package Mypackage;. This one is clear for me, ok.
2) Then I have to provide the list of functions I want to export, right?
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(half);
Ok, no problem with this one either.
3) Now is where I start to get lost. I don't know how to handle the
new thing. I mean, there is something that shall be there, in order I can in the main say
my $m = new Mymodule; in the main, right? I tried
sub new {return bless {}; } but this feels so strange.
4) Then, with all set up, how shall I call the functions from the main?
$m->half(10); ? This doesnīt work, since the output is
Mymodule=HASH(0xbd5068) and not just 5! What am I doing wrong? Do I have to modify something in the half() function (and all of them), now thatīs a module-like library??
I really thank you if you can help me out on this. Sorry if this is too OO-newbie, but I think it may be an interessting material for the community, as I couldnīt find any didatic explanation on this library-module conversion over the web.
Thanks a lot!
Andre
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.