Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

A lot of good suggestions here. Some time ago, however, I went the other way round: I wrote some improved, all-new gizmo-packed object-oriented modules, that covered my previous functions. Then I wrote a compatibility layer module that presented a procedural interface for some functions to my old scripts.

Here's an (untested) quick shot to illustrate what I mean:

#!/usr/bin/perl -w use strict; package MyClass; 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 = shift; my $file = shift; # Did you really want to print the error message # when -f fails even if not in verbose mode? print STDERR "$file ", (-f $file ? 'found' : 'not found') if $self->verbose; return -f _; } package MyModule; sub isFile($;$) { my $file = shift; my $object = MyClass->new(verbose => shift || 0); return $object->isFile; }

With that approach, of course, switching to the new module could break your old scripts, should you realize that your compatibility layer just isn't. ;) Thus, extensive testing is needed before going live with the new module. I would not reccomend that for large, critical applications. On the other hand, you'd keep the old module around, so you could easily downgrade in case of problems...

What I like about this approach, is that you don't pack your OO code with old procedural interface stuff. Applications that use only the OO interface with have no overhead at all. On the other hand, of course, your old apps might suffer from the overhead of the disguised object creation and method calls.

Besides all that, I suggest to study the code of CGI.pm or some of the other CPAN modules that offer both an object and a procedural interface (no I can't think of another right now... (help me!)), if you really want to get serious on this.

BTW, does any of you brothers and sisters know of some literature/articles on this topic?

Updated: Just added a comment in the code above, since it functionally diverts from Rudif's original code.

So long,
Flexx


In reply to Re: How to morph a plain module to OO (use a compatibility layer) by Flexx
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 learning in the Monastery: (5)
As of 2024-04-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found