Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl package Hash::AsObj; my %sub_class; sub new { my $class = shift; my $href = { @_ }; my $sub_class; # Keep hashes with the same set of keys in # the same package my $class_key = join "~", sort keys %$href; if ( exists $sub_class{$class_key} ) { $sub_class = $sub_class{$class_key}; } else { ( $sub_class = "$href" ) =~ tr/()/__/; $sub_class{$class_key} = $sub_class; } @{"${class}::Data::${sub_class}::ISA"} = "Hash::AsObj::Data"; bless $href, "${class}::Data::$sub_class"; } package Hash::AsObj::Data; use Carp (); sub DESTROY { 1 } sub AUTOLOAD : lvalue { my ( $class, $method ) = $AUTOLOAD =~ /^(.*)::(.+)$/ or Carp::croak "Invalid call to $AUTOLOAD"; Carp::croak "Can't locate object method $method via package $class" unless exists $_[0]->{$method}; *$AUTOLOAD = sub : lvalue { my $self = shift; if (@_) { $self->{$method} = shift; return $self; } $self->{$method}; }; goto &$AUTOLOAD; $Hash::AsObj::foo; } package main; use strict; use warnings; my $hobj = Hash::AsObj->new( a=>1, b=>2, c=>3 ,e=>5 ); print $hobj->a, "\n"; # It's an lvalue method! $hobj->a++; print $hobj->a, "\n"; # Or supply an argument to set values # I like method chaining - Your opinion may differ $hobj ->b(5) ->c(6); print "b: ", $hobj->b, " c:", $hobj->c, "\n"; # Error - this key doesn't exist print $hobj->d;

In reply to Hash::AsObj by runrig

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2023-11-28 16:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found