Play with it:
#!/usr/bin/perl use warnings; use strict; package Class::GetSet; sub new { my $class = shift; return bless({@_}, $class) } sub __set { $_[0]->{ $_[1] } = $_[2] } sub __get { return $_[0]->{ $_[1] } } use vars qw($AUTOLOAD); sub AUTOLOAD { no strict 'refs'; if ($AUTOLOAD =~ /^.*::(\w+)$/o && exists $_[0]->__has->{$1}->{'rea +d'} ) { my $attr = $1; *{$AUTOLOAD} = sub { my $s = shift; return ($s->__get( +$attr, @_)) }; goto &$AUTOLOAD; } elsif ($AUTOLOAD =~ /^.*::set_(\w+)$/o && exists $_[0]->__ha +s->{$1}->{'write'} ) { my $attr = $1; *{$AUTOLOAD} = sub { my $s = shift; return ($s->__set( +$attr, @_)) }; goto &$AUTOLOAD; } # subs like DESTROY return if $AUTOLOAD =~ /^.*::[A-Z]+$/; die "Unimplemented $AUTOLOAD"; } + + package Dummy; + + use base qw(Class::GetSet); sub __has { return { foo => { read => 1, write => 1 }, bar => { read = +> 1 } } } + + package main; + + my $d = Dummy->new( foo => 1, bar => 2); print "foo = '". $d->foo ."'\n"; $d->set_foo( 'xxx' ); print "foo = '". $d->foo ."'\n"; + + print "bar = '". $d->bar ."'\n"; $d->set_boo( 'xxx' );
Example above is for example of using the closures for dynamic generation of methods.

In reply to Re: understanding closures by shady
in thread understanding closures by reasonablekeith

Title:
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, details, 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, summary, 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.