Use um módulo YAML; recurse através da estrutura de dados e substitua quaisquer elementos correspondentes aqui com linhas lidas na entrada padrão.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use YAML::Tiny;
my $yaml =
YAML::Tiny->read( $ARGV[0] // die "Usage: $0 yaml-file [out-file]\n" );
mangle_description( $yaml->[0] );
$yaml->write( $ARGV[1] // "$ARGV[0].out" );
sub mangle_description {
my $what = shift;
my $type = ref $what;
if ( $type eq 'HASH' ) {
for my $key ( keys %$what ) {
if ( $key eq 'description'
and $what->{$key} eq '<*description>' ) {
$what->{$key} = set_description();
}
mangle_description( $what->{$key} ) if ref $what->{$key};
}
} elsif ( $type eq 'ARRAY' ) {
for my $entry (@$what) {
mangle_description($entry);
}
} else {
warn Dumper $what;
die "unknown type in YAML ??\n";
}
}
sub set_description {
my $next = readline *STDIN;
chomp $next;
return $next;
}
acima, salvo como parser
e com YAML válido em input
:
$ yes | perl parser input
$ grep description input.out
description: y
description: y
$