Perl Monk, Perl Meditation | |
PerlMonks |
Re: Method for reducing tedium of transferring object properties to scalarsby shmem (Chancellor) |
on Apr 21, 2017 at 22:00 UTC ( [id://1188591]=note: print w/replies, xml ) | Need Help?? |
The easiest way is creating a subroutine which takes the object as first argument, then an array of methods, calls those methods and returns something convenient - either a list (key/value pairs, or just the values) or a reference thereof:
Of course, that only works for method calls without arguments. If your methods need arguments, you'd tweak that sub to take named arguments (i.e. it is passed a list of key/value pairs where the key is the method, and the value is a reference to whatever the method takes), or a hash reference holding that. Don't use that stub as is. There's no check for error conditions etc. update: It really depends on what your objects are, and what you are up to. If you are the Master of the Class/Package you are using, and if your objects are just scalar references (as any well behaved object should be), you could use overload to provide a shortcut:
Ah well... TIMTOWTDI - there are so much ways to wrap laziness into magic... ;-)
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
In Section
Seekers of Perl Wisdom
|
|