Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

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

I wrote MyBaseClass.pm to avoid having to write methods for each hash item in the derived classes. Amazingly, it works as I intended. In this example, Address.pm inherits the new method from MyBaseClass.pm and the state method is handled in the AUTOLOAD sub in MyBaseClass.pm.

This allows me to call state as a method <nobr>(print $c->state)</nobr> and avoid accessing the object's hash directly with <nobr>print $c->{address}{state}</nobr>. Further it will return undef if the method doesn't exist, allowing me to test for an additional address line, for example. I'm wondering if there is something I'm not anticipating. Is there a problem with this approach that I don't see or am I just paranoid?

testing.pl

#!/usr/bin/perl use strict; use warnings; use Address; my $c = new Address; $c->address( [ '137 PR 1101', 'Sun Valley MHP - FM 205', 'FooHaHa, TX 76401' ] ); print $c->state;

MyBaseClass.pm

package MyBaseClass; use strict; use warnings; use vars '$AUTOLOAD'; # or: our '$AUTOLOAD'; sub new { my $this = shift; my $class = ref( $this ) || $this; return bless {}, $class; } sub AUTOLOAD { my $self = shift; if ( $AUTOLOAD =~ /::(.*)$/ ) { my $method = $1; if ( $method ne 'DESTROY') { return $self->{address}{$method} if exists $self->{address +}{$method}; return undef; } } } 1;

Address.pm

package Address; use strict; use warnings; use base 'MyBaseClass'; sub address { my $self = shift; return $self->{address} unless @_; #rip apart address @{ $self->{address} }{ qw(number street) } = split ' ', ( shift @ +{ $_[0] } ), 2; my $line = pop @{ $_[0] }; @{ $self->{address} }{ qw(city state zip) } = $line =~ /([^,]),\s* +(\S+)\s*(\d+)/; $self->{address}{additional} = $_[0] if @{ $_[0] }; return $self->{address}; } 1;



HTH,
Charles K. Clarkson

In reply to Am I asking for trouble with my own AUTOLOAD sub? by CharlesClarkson

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 perusing the Monastery: (9)
As of 2024-03-28 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found