Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

A function-based API can be (almost) automatically generated from methods -- all you need is a customized import sub. A default object is created during import and each package using the module has a private default object. This approach usually works a bit better than Flexx's because (1) it avoids creating lots of temporary objects, and (2) there's a long-lived object around to hold module state, so all the state-setting methods still work.

I wouldn't recommend the CGI.pm module for studying this technique. CGI.pm is difficult to understand because of its long and glorious history. Take a look at my code below, read the docs on packages and symbolic references, look at Exporter.pm and then you're ready to crack open CGI.pm.

## MyClass.pm package MyClass; use strict; my @export = qw(verbose isFile); sub new { my ($class, %args) = @_; my $self = {verbose => $args{verbose} || 0}; return bless $self, $class; } sub verbose { my $self = shift; $self->{verbose} = shift if @_; return $self->{verbose}; } sub isFile { my ($self, $file) = @_; my $found = -f $file; if ($self->verbose) { print STDERR "$file ", ($found ? 'found' : 'not found'), "\n"; } return $found; } sub import { my $module = shift; my $mode = shift || ':methods'; if ($mode eq ':functions') { no strict 'refs'; my ($package) = caller(); my $object = MyClass->new(@_); foreach my $name (@export) { my $sub = $MyClass::{$name}; *{$package.'::'.$name} = sub { &{$sub}($object, @_) }; } } } 1; ## test.pl package verbose_main; use strict; use MyClass (':functions', verbose => 1); sub test { if (isFile('MyClass.pm')) { print "ok\n"; } if (verbose()) { print "verbose mode on\n"; } } package quiet_main; use strict; use MyClass (':functions'); sub test { if (isFile('MyClass.pm')) { print "ok\n"; } verbose(1); if (isFile('NotFound')) { print "ok\n"; } } package oo_main; use strict; use MyClass; my $env = MyClass->new(verbose => 1); sub test { if ($env->isFile('MyClass.pm')) { print "ok\n"; } } package main; use strict; verbose_main::test(); quiet_main::test(); oo_main::test();

In reply to Custom import for alternate module APIs by blssu
in thread How to morph a plain module to OO by Rudif

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found