Re: finding methods and attributes in a name space



On 8/31/07, Hunter Barrington <godsrock37@xxxxxxxxx> wrote:
how would i find out what attributes and methods are in a name space?
for example i have an object in my code and im not sure what type it is,
what attributes are associated with it, what methods etc etc how can i
find out. in python there was dir() and type() which was a big help.
anything similar in perl?

If you have an object you can call ref on it to find its class:

my $obj = IO::File->new;
my $class = ref $obj; #$class is now 'IO::File'

There is no one way to implement objects in Perl (besides the fact
that they must be blessed), so there is no one way to find all
attributes, but the common method of creating objects is to use a
blessed hashref. In those cases you can often, but not always since
there are many ways to create objects even with blessed hashrefs, get
the attributes by saying

my @attributes = keys %$obj;

To my knowledge there is no way to find what functions are defined in
a package but to walk the symbol table. There are a few modules in
CPAN that make this easier, but in the end the answer to your question
is "read the documentation". Every decent module comes packaged with
complete documentation. Try typing

perldoc IO::File

where IO::File is the name of the class you want info about. The dir
function in Python is so useful because Python has a REPL; Perl 5 does
not yet have a fully functional REPL*. Perl 6 has a REPL (and
interpretor and compiler) named Pugs**, but since Perl 6 is still in
development and differs greatly from Perl 5 I cannot recommend using
it to learn Perl.

* there are a few, but in general I don't think they are true REPLs
yet. You can find a list of them here
http://use.perl.org/article.pl?sid=07/08/30/1729255

** http://www.pugscode.org/
.



Relevant Pages

  • Re: how to launch perl
    ... > There are also some Perl REPLs* out there that allow you to use a Perl ... > * REPL stands for Read, Eval, Print, Loop. ... pugs> $c + 1 ... command history, ability to save the current buffer, etc.). ...
    (perl.beginners)
  • Re: closer-to-lisp interactivity for Perl?
    ... >> I know there are some perl hackers among the Lisp community ... >> interactive programming environments than perl users,so I figured this ... > which gives you a Perl repl to type at. ...
    (comp.lang.lisp)
  • Re: caadr qn
    ... is called the read-eval-print loop, or REPL, not the PERL. ... Because I've heard of lots of people programming in Perl, ...
    (comp.lang.lisp)
  • Re: Python or PHP?
    ... If it's one of the things for which Python ... every language here and there more ways to do something. ... you make Perl more complicated than it is:-D. ... Not the programmer. ...
    (comp.lang.python)
  • Recommend an E-book Meeting the Following Criteria (Newbie, Long)
    ... I know several programming languages namely Java, Perl and C in this order ... Now I'm wondering which Python book I should get as there are so many out ... I'd like to mostly concentrate on language features but some pointers ...
    (comp.lang.python)