J. Gleixner said:
perldoc AutoLoader
To find your favorite search engine and search for "perl autoloader".
After looking over Autoloader, it seems like this would have the
opposite effect of my intended goal. I was looking to move the
generation of the subroutines into a pair of loops for two reasons.
First, repeating nearly exact code over and over again seems like
asking for trouble, both from a debugging and a reuse standpoint, and
secondly if I were able to iterate across a list of variable names,
adding more variables or changing the internal logic of every
setter/getter subroutine would be trivial.
I may have misunderstood Autoloader, but it seems like I would still
have to define each subroutine individually, then run autosplit to
create a bunch of .al files containing the pre-declarations. This seems
like I am getting more complicated, rather than less.
I'm also confused by this:
"If any module's own AUTOLOAD subroutine has no need to fallback to the
AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit
subroutines), then that module should not use AutoLoader at all."
I really don't need to load these subs dynamically, as I intend to call
every one of them, so I'm not sure how I would go about doing this with
Autoload.
What I did notice that caught my eye was the example AUTOLOAD sub,
which has some sort of glob magic going on:
*$AUTOLOAD = sub { $val }; # same as: eval "sub $AUTOLOAD { $val }"
goto &$AUTOLOAD;
which seems like it might do something similar to what I am trying to
accomplish, but I have exactly _zero_ experience with globs.
I was thinking I might be able to do something like this, but I'd hate
to make it into the SAQ's....
my @regvars = qw/var1 var2 var3/;
my @derefvars = qw/var4 var5 var6/;
foreach my $var (@regvars) {
sub $var {
my $self = shift;
if (@_) { $self->{uc("-".$var)} = shift }
return $self->{uc("-".$var)};
}
}
foreach my $var (@derefvars) {
sub $var {
my $self = shift;
if (@_) { $self->{uc("-".$var)} = shift }
if (ref($self->{uc("-".$var)}) {
return ${$self->{uc("-".$var)}};
} else {
return $self->{uc("-".$var)}
}
}
}
But, I don't have access to my code, nor access to the Internet where
my code is, so I can't just try it out (short of writing another short
little test app.) My goal, however, wasn't just to get it to work, it
works now and I could probably get it to work with some sort of looping
structure, but I'd rather do it _right_!!! This is my first attempt at
dabbling with OO programming in perl and I'm basically just working
from perltoot at this point.
So, is there a correct/less dangerous/more efficient etc. way of doing
this. I'm sure someone out there can put me in my place/point me in the
right direction and I'll probably slap myself for not seeing it in the
first place.
Thanks in advance!!!
Pete