Recursive open files in Perl -


i have perl script count how many times expression appered in file, in particular case counts found between '<' , '>' because wanted parse .xml files.

script:

#usr/bin/perl  sub by_count {    $count{$b} <=> $count{$a}; }  open(input, "<[content_types].xml"); open(output, ">output"); $bucket = qw/./;   while(<input>){    @words = split(/\</);     foreach $word (@words){             if($word=~/($bucket*>)/io){        #print output "$word";       #print output "\n\n";       $count{$1}++;}     } } foreach $word (sort by_count keys %count) {     print output "<$word occurs $count{$word} times\n\n";  }  close input; close output; 

output

<default extension="xlsx" contenttype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/> occurs 1 times  <default extension="png" contenttype="image/png"/> occurs 1 times  <override partname="/word/theme/theme1.xml" contenttype="application/vnd.openxmlformats-officedocument.theme+xml"/> occurs 1 times 

problem

i want recursively. have directory multiple subdirectories inside, , inside each subfolder there [content_types].xml file. suggestion on how parse every file name found in main directory?

example diagram:

>directory   >directory1     >[content_types].xml   >directory2     >[content_types].xml   >directory3     >[content_types].xml   .   .   .    >directory100     >[content_types].xml 

one way use module find::file traverse subdirectories find tell it. like:

#!/usr/bin/env perl  use warnings; use strict; use file::find;  find( \&wanted, shift );  sub wanted {         return unless -f $_ && m/\[content_types\]\.xml/;         open $fh, '<', $_ or {                 warn qq|warning: not open $file::find::name\n|;                 return;         };           open $ofh, '>', 'output';         $bucket = qw/./;          while ( <$fh> ) {                  ## ... code here ...         }             ## ... code here ... } 

give argument directory want search begin:

perl script.pl . 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -