Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Parse POD of a method from perl module

by perlUser9 (Novice)
on May 13, 2014 at 21:51 UTC ( #1085954=perlquestion: print w/ replies, xml ) Need Help??
perlUser9 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to parse the POD for a particular method from a Perl module. For ex if I have a module named MyModule.pm as below:
package MyModule;
=head1 Subroutine1
My POD for Subroutine1
=cut
sub Subroutine1
{
...
...
}

=head1 Subroutine2
My POD for Subroutine2
=cut
sub Subroutine2
{
...
...
}
1;

Now I want to get the POD of Subroutine2 in a string. Can anyone please suggest if there is a Perl module which can parse the POD for the particular method?

Comment on Parse POD of a method from perl module
Re: Parse POD of a method from perl module
by LanX (Canon) on May 13, 2014 at 22:11 UTC
    Parsing POD is pretty straight forward, cause the format is simple.

    The real complication is parsing the corresponding Perl sub names.

    like

      - how to parse Perlcode? (dynamic sub names, dynamic vs static)

      - are all sub's source supposed to follow directly to the POD?

      - are subs described in head2-items following a special head1-item? (like INTERFACE)

    Could you please give us more informations about what you are trying to achieve?

    Unfortunately solutions in Perl are not very DRY (thats why I envy Python for stealing docstrings from Lisp)

    For instance one of my last employers used a POD server which automatically parsed all installed modules to create CPAN like web-pages including TOCs listing each function/method.

    Following the links you got the POD for each sub and the code was JS-folded in between.

    Thats what you want? For instance this solution would imply switching from head1 to head2 in your POD.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Hi Rolf, Thanks for your reply. I need to retrieve the POD for a specific method and want to provide that to RIDE (IDE for Robot framework I am using). I need to show the POD as a description/information about the method in RIDE. I saw couple of Perl modules like Pod::Simple, Pod::Parser but they work on the whole module. And my requirement is for the method.
        i.o.W. your input are
        • name of the head1
        • path of a module
        and you just want the POD-content, no source code?

        As mentioned ready-to-use solutions expect you to follow certain conventions!

        But a simple perl-script to parse everything between =head1 NAME and =cut is straight forwardly implemented with a flip-flop range and easily adapted if your format has variations (different conventions¹)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        ¹) e.g are you aware that your example is "invalid" POD b/c of missing empty lines?

Re: Parse POD of a method from perl module
by Anonymous Monk on May 13, 2014 at 23:31 UTC
Re: Parse POD of a method from perl module
by RonW (Friar) on May 14, 2014 at 01:13 UTC
    Try Pod::Select

      I can second that. I've had success with Pod::Select in the past.

Re: Parse POD of a method from perl module
by kcott (Abbot) on May 14, 2014 at 04:48 UTC

    G'day perlUser9,

    Welcome to the monastery.

    This seems to do what you want. Using your example code, this outputs just "My POD for Subroutine2\n". I added some extra dummy POD for test purposes.

    #!/usr/bin/env perl use strict; use warnings; my $wanted_sub = 'Subroutine2'; my $pod_string; { local $/ = "\n=head1 "; while (<DATA>) { if (/\A $wanted_sub $/mx) { ($pod_string = $_) =~ s/\A .+? ^ ( .+? ) =cut .* \z/$1/msx +; last; } } } print $pod_string; __DATA__ package MyModule; =head1 Subroutine1 My POD for Subroutine1 =cut sub Subroutine1 { ... ... } =head1 Subroutine2 My POD for Subroutine2 =head2 Dummy Heading 2 =over 4 =item whatever qwerty =item blah asdfgh =back Some text =cut sub Subroutine2 { ... ... } 1;

    Output:

    My POD for Subroutine2 =head2 Dummy Heading 2 =over 4 =item whatever qwerty =item blah asdfgh =back Some text

    -- Ken

      Hi All, Thanks a lot for your help. I wrote my code to extract the POD for a particular method and used Pod::Text module to covert the pod to plain text. Thanks again for the guidance.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1085954]
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2014-09-23 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (234 votes), past polls