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

Re: declaring accessor methods from a list

by Trizor (Pilgrim)
on Apr 26, 2007 at 22:38 UTC ( [id://612284]=note: print w/replies, xml ) Need Help??


in reply to declaring accessor methods from a list

sub $name {} isn't working the way you expect it to because named subs are picked up during the compilation of the BEGIN block.

What you want to do is edit the GLOB with the name of your accessor in a fasion similar to this:

BEGIN { my @accessors = qw(one two foo); foreach my $methname (@accessors) { no strict 'refs'; *$methname = sub { # accessor sub }; } }

As an alternative to rolling your own autogenerator you could look on the CPAN as there are several mentioned elsewhere in this thread.

Replies are listed 'Best First'.
Re^2: declaring accessor methods from a list
by Bro. Doug (Monk) on Apr 26, 2007 at 22:53 UTC
    Thank you.

    I tried that and it worked. And yes, I could be looking for prior art. I'm also just interested in knowing how this thing is done (a perlmonks party trick?).

    After I get this working and tested from a test.pl, I'll post it up with corrections.

    Peace monks,
    Bro. Doug :wq
      perlmonks is full of prior art

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://612284]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found