Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Accessing a scalar value in another subroutine?

by tobyink (Abbot)
on Sep 23, 2012 at 20:39 UTC ( #995249=note: print w/ replies, xml ) Need Help??


in reply to Accessing a scalar value in another subroutine?

Why do you want to do this rather than just use POD? ("I heart Python" is not a good reason.)

That having been said, there's actually a really easy way to do this for object-oriented code (i.e. subs designed to be called as methods rather than functions).

use v5.14; package Foobar { sub new { bless [] => shift; } sub quux { return "documentation" unless @_; say "The method has been called!"; } } my $obj = Foobar->new; $obj->quux; # calls the method print Foobar::quux(); # prints the documentation
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Accessing a scalar value in another subroutine?
Download Code
Re^2: Accessing a scalar value in another subroutine?
by LanX (Canon) on Sep 23, 2012 at 21:01 UTC
    > Why do you want to do this rather than just use POD?

    POD is normally used to describe the public API of a module and many people oppose weaving POD snippets close to subs.

    Furthermore automatically finding the corresponding POD-part for a function isn't trivial.

    Docstrings OTOH are closely tied and great for introspection in an interactive system, like REPLs or applications with embedded scripting.

    I'm inspecting LISP docstrings in emacs every day when browsing thru functions.

    Cheers Rolf

      If you are hell-bent on doing that, and having doc-strings for your functions, the following style may be to your taste:

      use vars %documentation; sub doc($$) { my ($for,$doc) = @_; $documentation{ $for } = $doc; }; doc('mysub1',<<'=cut'); =head2 C<< mysub1 >> This documents mysub1. An example: mysub1(); =cut sub mysub1 { ... }; doc('frobnicate',<<'=cut'); =head2 C<< frobnicate >> This documents frobnicate. An example: frobnicate(); =cut sub frobnicate { ... };

      You will note from history that this style never caught on.

      If you adhere to a strict documentation style, you may simply parse the POD and extract the function definitions from that. Using "docstrings" and parsing the module documentation are equivalent in that sense.

        well far from being DRY... :)

        Cheers Rolf

      Furthermore automatically finding the corresponding POD-part for a function isn't trivial.

      Yes, it is absolutely trivial, provided the pod actually documents the function. Pod::Select/Pod::Coverage ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-18 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (44 votes), past polls