Last active
January 13, 2017 12:48
-
-
Save kberov/b21c2b650d18e4f0de0fc28428c0f4ca to your computer and use it in GitHub Desktop.
Process all files in a directory using Mojo::File! Четене и обработка на всички файлове в дадена папка с Mojo::File!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use FindBin; | |
use lib "$FindBin::Bin/../lib/perl5"; | |
use Mojo::Base -strict; | |
use Mojo::DOM; | |
use Mojo::File qw (path); | |
Mojo::Util::getopt \@ARGV, | |
'--input_folder|i=s' => \(my $in_folder = ''), | |
'--output_file|o=s' => \my $out_file, | |
'--help|h' => \my $help; | |
usage() and exit if ($in_folder !~ m{[\w/]+} || $help); | |
my $out = ''; | |
path($in_folder)->list_tree->sort->each(sub { $out .= process_file(shift) }); | |
$out_file ? (path($out_file)->spurt($out)) : (say $out); | |
sub usage { | |
say "Usage..." | |
} | |
sub process_file {my $file = path(shift); | |
return '' unless $file =~ /\.xml/; | |
my $xml = Mojo::DOM->new('<?xml version="1.0"?>' . $file->slurp); | |
my $image_id = $xml->find('image')->first->{id}; | |
my $out = qq|\n.$image_id|; | |
#... get other nodes and get information from them and add to the output | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment