Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: converting libraries into modules

by Andre_br (Pilgrim)
on Mar 05, 2006 at 18:34 UTC ( [id://534591]=note: print w/replies, xml ) Need Help??


in reply to Re^2: converting libraries into modules
in thread converting libraries into modules

Just to document an adaptation on Cody's approach, that I´ve just tested with success:

Instead of

sub half { my $number; if (ref($_[0]) eq 'MyPackage') { my $self = shift(); $number = shift(); } else { $number = shift(); } return $number / 2; }
We can make the adaptation of all perl4-ish functions possible just by adding a single line, on the top of each function's code:

sub half { if (ref($_[0]) =~ /MyPackage/ ) { my $self = shift; } # as the former Perl4-like functions don´t need the $self object, this + line's role is to clean up the first element of the @_ array! And, s +o, make things ready for the shifts your code already is set to! my $number = shift; return $number / 2; }
Detail: I call all my methods libraries MyPackage, Mypackage2 etc, so by testing with this regexp, I don't even have to worry to adapt this one-line according to the library I´m converting into module! Just adding it all over the subs, and finally enter the OO world!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://534591]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found