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

  • Re: reflection in perl
    ... > 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? ... my $class = shift(); ... sub print { ...
    (perl.beginners)
  • Re: Soft Reference to object properties
    ... and it cannot really be compared to what you can do in dynamically typed languages like JavaScript and Perl. ... Reflection in C# is indeed a neat and useful feature, but it comes at a price: you avoid compile time type check, and in many cases you will pay a noticable performance penalty. ... Well, if you need some kind of config scheme where an external config file tells what classes to instansiate or which namespaces to search in, reflection is probably the way to go. ... Maybe someone has fought with a similar problem before and cooked up a design pattern for it: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Table to list tool
    ... project investigatoraward body ... reflection of what I've used before. ... If you knew Perl you would not ... Python may well be the new Perl, so you could skip a generation. ...
    (uk.net.web.authoring)
  • Re: Data::Dumper for Java?
    ... Sam wrote or quoted: ... > e.g. in Perl: ... representation of the contents of the object. ... it's a case of using reflection to access the data. ...
    (comp.lang.java.programmer)
  • Re: Subject: Question: Dynamically Raise Event using Reflection API
    ... I tried to find a way to dynamically raise event using Reflection API. ... the GetRaiseMethod of the EventInfo object ...
    (microsoft.public.dotnet.framework)