rev has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out the extent of of perl's introspective and reflective features. So far, I've only got a few leads from people on #perl, enumerating the symbol table (e.g., to find all the methods in a class/pkg) and autoload. Can one deduce (decompile from bytecode or AST) the texual source of a sub, without using a text file originally containing the code? (which may not exist) Any other interesting code snippits illustrating features along these lines?
  • Comment on Perl's Reflective and Introspective Capabilities?

Replies are listed 'Best First'.
Re: Perl's Reflective and Introspective Capabilities?
by gellyfish (Monsignor) on Mar 23, 2002 at 10:26 UTC
Re: Perl's Reflective and Introspective Capabilities?
by Dice (Beadle) on Mar 23, 2002 at 14:50 UTC
    You should take a look at Schwern's Module::Info module.

    Here's the synopsis for the most recent version (0.12). Does anything here look interesting to you?

    SYNOPSIS
             use Module::Info;
    
             my $mod = Module::Info->new_from_file('Some/Module.pm');
             my $mod = Module::Info->new_from_module('Some::Module');
             my $mod = Module::Info->new_from_loaded('Some::Module');
    
             my @mods = Module::Info->all_installed('Some::Module');
    
             my $name    = $mod->name;
             my $version = $mod->version;
             my $dir     = $mod->inc_dir;
             my $file    = $mod->file;
             my $is_core = $mod->is_core;
    
             # Only available in perl 5.6.1 and up.
             # These do compile the module.
             my @packages = $mod->packages_inside;
             my @used     = $mod->modules_used;
             my @subs     = $mod->subroutines;
             my @isa      = $mod->superclasses;
             my @calls    = $mod->subroutines_called;
    
             # Check for constructs which make perl hard to predict.
             my @methods   = $mod->dynamic_method_calls;
             my @lines     = $mod->eval_string;    *UNIMPLEMENTED*
             my @lines     = $mod->gotos;          *UNIMPLEMENTED*
             my @controls  = $mod->exit_via_loop_control;      *UNIMPLEMENTED*
             my @unpredictables = $mod->has_unpredictables;    *UNIMPLEMENTED*
    

    Cheers,
    Richard

Re: Perl's Reflective and Introspective Capabilities?
by dws (Chancellor) on Mar 23, 2002 at 19:09 UTC
    "Reflection" is usually understood to be about the ability to query a class about its methods and invoke those methods symbolically. Perl does this just fine, with one significant limitation: Perl methods don't have "signatures" in the same way that Java or C++ methods do. Using base Perl functionality, you can't query a class and gather information about the arguments that a method expects.

Re: Perl's Reflective and Introspective Capabilities?
by jerrygarciuh (Curate) on Mar 23, 2002 at 17:52 UTC
    I'm not sure if this is in the vein of your question but I was reading this the other day:
    "If you have the __END__ token in your program, Perl stops reading source code at that point but leaves the file open. You can read subsequent lines from the file from the DATA filehandle OR you can seek() back on that file to the start of your program's source code. seek(DATA, 0, 0); Self-aware programs. Not self-modifying, though." Quote from : http://prometheus.frii.com/~gnat/yapc/2000-stages/index.html
    HTH
    jg
    _____________________________________________________
    Think a race on a horse on a ball with a fish! TG