- Create a directory under the current directory for your module
- Create a file called yourmodule.pm
- Put the following line at the top:
package yourmodule; - Now paste the following and edit the require code to include all the libraries your package needs
use 5.0;
#use strict;
use warnings;
require Win32::GuiTest, HTTP::Request, XML::Smart, Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw() ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );
our $VERSION = '1.00'; - Create a constructor subroutine; it needs to bless a reference to the object as its last line (put whatever you like before that)
sub new {
my $self = { FOO => 1, BAR => 0};
bless $self, "yourmodule";
} - Load the module in your Perl file
use lib './yourmodule'; # this adds the module directory to your include path
use yourmodule; # this loads your module - You can now call your module from your perl file:
my $barf = yourmodule->new();
print $barf->get_stuff() . "\n";
exit(0); - Remember! when you call your package's methods using an object ($object->method()), the first argument is a reference to the object-- you can restore 'normal' operation (if you just pasted the sub routine from within a normal perl file) by just adding my $self=shift; to the top of your sub routine.
Thursday, May 14, 2009
Quickly Create an Object Oriented Perl Module
Subscribe to:
Post Comments (Atom)
Labels
- Java (34)
- Oracle (27)
- javascript (24)
- NIX administration (19)
- Reporting (18)
- XML (17)
- Web Graphics (10)
- perl (10)
- CSS (9)
- Tomcat (8)
- Android (7)
- Matlab (7)
- XSL (7)
- HTML (6)
- SQL (6)
- XForms (6)
- browser quirks (6)
- Orbeon XForms (5)
- Solaris (5)
- AJAX (4)
- Mirth Project (4)
- PHP (4)
- Video (4)
- Arduino (3)
- Eclipse (3)
- JPA (3)
- JSP (3)
- JSTL (3)
- LAMPS (3)
- SSH (3)
- SVN (3)
- Hibernate (2)
- Netbeans (2)
- Networking (2)
- Python (2)
- Windows (2)
- Wordpress (2)
- XHTML (2)
- Alfresco (1)
- Architecture (1)
- ArduPilot (1)
- Arduino Yun (1)
- Arduplane (1)
- Audio Recording (1)
- Betaflight (1)
- CouchDB (1)
- DIY (1)
- Design (1)
- FPV (1)
- JSON (1)
- JUnit (1)
- Mobile Development (1)
- Printing (1)
- RC Airplane (1)
- REST (1)
- Scalability (1)
- Struts (1)
- Tools (1)
- Virtualization (1)
- Web services (1)
- camera (1)
- canon (1)
- gphoto2 (1)
- jQuery (1)
- ubuntu (1)
- unix (1)
No comments:
Post a Comment