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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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?

#!/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;

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;

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;

Charles K. Clarkson

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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2020-10-29 01:57 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (266 votes). Check out past polls.