[vienna.pm] Aufruf namentlich bekannter Methoden

Peter J. Holzer hjp-vienna-pm-list at hjp.at
Tue May 21 15:36:59 CDT 2002


Angenommen, ich habe eine Klasse mit vielen Getter/Setter-Methoden, und
möchte jetzt viele davon aufrufen. 

Das kann ich natürlich sequentiell machen:

    my $apple = Fruit->new();

    print "Diameter: ", $apple->diameter();
    print "Color: ",    $apple->color();
    print "Taste: ",    $apple->taste();
    print "Has worm: ", $apple->has_worm();
    ...

was aber ziemlich unelegant ist. 

besser gefällt mir da schon:

    for ((
	[ diameter => 'Diameter' ],
	[ color    => 'Color'    ],
	[ taste    => 'Taste'    ],
	[ has_worm => 'Has worm' ],
    )) {
	my ($method, $label) = @$_;
	print "$label: ", eval "\$f->$method", "\n";
    }

aber daß ich da den Methodenaufruf in einem String zusammenbasteln und
dann eval'uieren muss, stört mich rein ästhetisch und könnte bei manchen
Applikationen auch negative Auswirkungen auf die Performance haben. 

Hat jemand eine bessere Idee?

	hp

-- 
   _  | Peter J. Holzer     | > Benötigt man für Linux einen Virenscanner?
|_|_) | Schriftführer LUGA  | Nein.
| |   | hjp at luga.at         | Linux bootet und läuft auch ohne Virenscanner
__/   | http://www.luga.at/ | einwandfrei.	-- Heimo Schoen in al
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://mail.pm.org/archives/vienna-pm/attachments/20020521/56a3a795/attachment.bin


More information about the Vienna-pm mailing list