Re: reflection in perl

From: Paul Johnson (paul_at_pjcj.net)
Date: 10/07/04


Date: Thu, 7 Oct 2004 12:33:18 +0200
To: Michael Seele <mseele@guh-software.de>

On Thu, Oct 07, 2004 at 11:28:44AM +0200, Michael Seele wrote:

> i'm new to perl and i want to know if perl has reflection like java?
> how can i acces reflection and how powerfull is the reflection in perl?

You might want to narrow this down a bit to get real information, but:

http://java.sun.com/docs/books/tutorial/reflect/ says

] The reflection API represents, or reflects, the classes, interfaces,
] and objects in the current Java Virtual Machine. You'll want to use
] the reflection API if you are writing development tools such as
] debuggers, class browsers, and GUI builders. With the reflection API
] you can:

] * Determine the class of an object.

ref()

] * Get information about a class's modifiers, fields, methods,
] constructors, and superclasses.
] * Find out what constants and method declarations belong to an
] interface.

You would probably use can(), @ISA and, depending of the base type of
the class, exists().

] * Create an instance of a class whose name is not known until
] runtime.

bless $obj, $class

] * Get and set the value of an object's field, even if the field name
] is unknown to your program until runtime.

$obj->{$field} = $value

] * Invoke a method on an object, even if the method is not known
] until runtime.

$obj->$method

] * Create a new array, whose size and component type are not known
] until runtime, and then modify the array's components.

my @arr;
$arr[$index] = $value

You can also access the symbol table at runtime, and use the B modules
to access a lot of internal information. But I don't think you'll need
to at first.

You'll need to learn to think the Perl way to get the best from the
language. You could program Perl as if it were Java, and do everything
you want to, but if you're using Perl, you might as well work to its
strengths.

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net


Relevant Pages