Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

detecting pod in files?

by edwyr (Sexton)
on Aug 17, 2005 at 14:13 UTC ( #484436=perlquestion: print w/replies, xml ) Need Help??

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

I want to detect if pod is embedded into a file or not. Is there an easier way to detect the embedded pod other than the sample code below?
my $file = '/tmp/file'; + open(IN, "<$file") or die "$0: cannot read file '$file': $!"; + my $pod = 1 if scalar(grep(/^=pod/o, <IN>)) > 0; + close(IN); + pod2html($file) if $pod;

Replies are listed 'Best First'.
Re: detecting pod in files?
by xdg (Monsignor) on Aug 17, 2005 at 14:46 UTC

    Pod::Find

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: detecting pod in files?
by davidrw (Prior) on Aug 17, 2005 at 15:18 UTC
    You can use the program podchecker (see its man page) and check the status code returned on the the shell (2 means no pod found). Looking at its source, it uses the podchecker() function from Pod::Checker which returns -1 if no valid pod found.
    use Pod::Checker; my $rc = podchecker($filepath, $outputpath, %options); if( $rc > 0 ){ # errors }elsif( $rc < 0 ){ # no pod found }else{ # ok! }
      The suggestion to use podchecker really helped. Thanks a lot.
Re: detecting pod in files?
by Ven'Tatsu (Deacon) on Aug 17, 2005 at 14:30 UTC
    I wouldn't call it easyer but at the first detection of a pod directive I would set the flag an bail out of the loop instead of searching the rest of the file. It is also posible that a file will not contain '=pod' but still have pod in it. I would use something like this:
    my $file = '/tmp/file'; open(IN, "<$file") or die "$0: cannot read file '$file': $!"; my $pod = 0; while (<IN>) { $pod = 1, last if /^=\w/ } #/^=(pod|head\d|over|item|back|cut)/ might be more accurate #=begin, =end, and =for are ommited because they would #usualy be ignored by pod2* close(IN); pod2html($file) if $pod;

      You can't do it this simply. Although a pod directive starts at the beginning of a line with an =, that has to happen when Perl isn't expecting something else. (Curiously, BBEdit gets this wrong too).

      # this isn't pod $time =time ;
      Or
      $string =<<"HERE"; =value =item HERE

      You can can't just look for a particular pod directive (such as =pod, in the original post, because it might not show up. Indeed, I have rarely used =pod.

      There are plenty of Pod modules, so use those instead of rolling your own. :)

      --
      brian d foy <brian@stonehenge.com>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-09-27 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?