http://qs321.pair.com?node_id=11125489


in reply to Dynamically generate setter/getter methods

You could use AUTOLOAD to avoid hacking the symbol table.

Update:
One of our fellow monks asked for an example. So here is very simplified one.

#!/usr/bin/perl use v5.12; use warnings FATAL => 'all'; package Attr; use Carp; my %attr = (one => 1, two => 2); sub new { my $class = shift; bless {}, $class; } our $AUTOLOAD; # Generic setter/getter sub AUTOLOAD { my $self = shift; my $called = $AUTOLOAD =~ s/.*:://r; croak "$AUTOLOAD: no such method" unless exists $attr{$called}; if (@_ > 0) { $self->{$called} = shift; } else { return $self->{$called}; } } package main; my $attr = Attr->new; $attr->one('uno'); say $attr->one; eval {say $attr->three}; say $@ if $@; __DATA__ uno Attr::three: no such method at /home/jo/Programs/play-scripts/pm-11125 +489.pl line 38.

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

Replies are listed 'Best First'.
Re^2: Dynamically generate setter/getter methods [Updated]
by Arunbear (Prior) on Dec 20, 2020 at 16:36 UTC
    The AUTOLOAD approach has some caveats to be aware of:

    1. It will be marginally slower because each call to the accessor will search the inheritance tree before calling AUTOLOAD

    2. Querying the object with can will return a false result, which is probably not what users of the object would expect.

      Thanks Arunbear. I thought about this a bit more, and I almost feel I dabbled with AUTOLOAD quite some years ago, but found deficiencies that prevented me from using it. That said, I very well could be off my rocker and thinking about something else. I will give it a try though and see how it goes.

Re^2: Dynamically generate setter/getter methods [Updated]
by pryrt (Abbot) on Dec 20, 2020 at 21:54 UTC
    I learned and used the AUTOLOAD methodolgy for the first time when I was developing the Editor component for Win32::Mechanize::NotepadPlusPlus.

    For that, I was mapping a pre-defined list of function prototypes I was stealing from drawing on from the API for a Notepad++ plugin which used some other language for automating Notepad++, and mapping those prototypes to the underlying Windows messages for the Scintilla component of Notepad++; there were about 10 common mappings from function requirements to message requirements. So, when I saw I had hundreds (over a thousand, I think but I didn't re-count) of functions which were just going to follow the same 10 or so patterns, I wanted to obey DRY, so decided I had to finally figure out what AUTOLOAD was, and whether it would help me.

    If AUTOLOAD hadn't done what I wanted, I would have just manipulated the symbol table myself... but since AUTOLOAD was a feature of Perl that I hadn't explored, I wanted to see if I could make it work. In the end, AUTOLOAD wasn't that hard for me to learn, and it accomplished my goal pretty handily.

    I wouldn't call myself an AUTOLOAD expert, nor do I know all the times when it's benefits would really shine, but I found it a useful tool -- and now I have an example implementation that I should theoretically understand a few years down the road, the next time I want to try to AUTOLOAD.

Re^2: Dynamically generate setter/getter methods [Updated]
by stevieb (Canon) on Dec 20, 2020 at 15:52 UTC

    What's the fun in living if you're not living dangerously? ;)

    In all seriousness, thanks for posting this. In all my years, I never looked into AUTOLOAD, so 20 years in, and I'm still learning new things!

    Cheers,

    -stevieb

      ... I never looked into AUTOLOAD ...

      Have a look at this node for a slightly funny use of AUTOLOAD.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'