[erlang-questions] api to retrieve module documentation (comparison to python shell)

Todd t.greenwoodgeer@REDACTED
Tue May 10 08:58:38 CEST 2011


Is it possible to retrieve the documentation or the source for a given 
method?

1. I'd can to enumerate the loaded modules:

(nitrogen@REDACTED)18> erlang:loaded(). 

[sys_expand_pmod,eunit_autoexport,security_handler,
  route_handler,role_handler,identity_handler,cache_handler,
  eval_bits,cerl_trees,lib,action_hide,action_jquery_effect,
  action_show,element_link,net_adm,process_registry_handler,
  session_handler,element_placeholder,element_listitem,
  element_bind,element_list,action_update,wf_validation,
  element_span,processes,epp,log_handler,file_not_found_page,
  re|...]

2. And I can get the exported methods for a given module:

(nitrogen@REDACTED)25> [{exports, Exports}, _, _, _] = 
net_adm:module_info().
[{exports,[{host_file,0},
            {localhost,0},
            {names,0},
            {names,1},
            {dns_hostname,1},
            {ping_list,1},
            {world,0},
            {world,1},
            {world_list,1},
            {world_list,2},
            {module_info,0},
            {module_info,1},
            {ping,1}]},
  {imports,[]},
  {attributes,[{vsn,[82065340652727412467812120004278067483]}]},
 
{compile,[{options,[{cwd,"/home/user/erlang/otp_src_R14B02/lib/kernel/src"},
 
{outdir,"/home/user/erlang/otp_src_R14B02/lib/kernel/src/../ebin"},
 
{i,"/home/user/erlang/otp_src_R14B02/lib/kernel/src/../include"},
                      debug_info]},
            {version,"4.7.3"},
            {time,{2011,4,26,3,21,35}},
 
{source,"/home/user/erlang/otp_src_R14B02/lib/kernel/src/net_adm.erl"}]}]
(nitrogen@REDACTED)26> Exports. 

[{host_file,0},
  {localhost,0},
  {names,0},
  {names,1},
  {dns_hostname,1},
  {ping_list,1},
  {world,0},
  {world,1},
  {world_list,1},
  {world_list,2},
  {module_info,0},
  {module_info,1},
  {ping,1}]

3. But now I'd like to get the docstring and/or the source for a given 
module:export. Is there an existing api method that does this?

BTW - this is the sort of thing that python is really good at. I love 
being able to simply type "dir()" and navigate through the local 
environment and into objects.

For example, running the python shell:

a) look at the local environment

 >>> dir()
['__builtins__', '__doc__', '__name__', '__package__']

b) examine an entry

 >>> dir (__doc__)
['__class__', '__delattr__', '__doc__', '__format__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__']

c) create an object
 >>> a = [1,2,3]

d) examine the object
 >>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__delslice__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', 
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']


e) get the help on this object
 >>> help(a)

Help on list object:

class list(object)
  |  list() -> new list
  |  list(sequence) -> new list initialized from sequence's items
  |
  |  Methods defined here:
  |
  |  __add__(...)
  |      x.__add__(y) <==> x+y
<snip>

f) get help on a method:
 >>> help(a.pop)
Help on built-in function pop:

pop(...)
     L.pop([index]) -> item -- remove and return item at index (default 
last).
     Raises IndexError if list is empty or index is out of range.


This is amazingly useful. Is there anyway to have a similar experience 
in the erlang shell?



More information about the erlang-questions mailing list