| 1 | package Music::NWC2MusicXML; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use version; our $VERSION = version->declare('0.003.0'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 10 | # Post-release roadmap | ||||||||||||||||||||||||||||
| 11 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 12 | # TODO: emit <time-modification>/<tuplet> for triplet and other tuplet groups | ||||||||||||||||||||||||||||
| 13 | # (durations are stored as correct rationals; only the notation element is missing) | ||||||||||||||||||||||||||||
| 14 | # TODO: read custom page dimensions from PgSetup (Width/Height) instead of | ||||||||||||||||||||||||||||
| 15 | # assuming A4 (210 x 297 mm) in _compute_page_layout | ||||||||||||||||||||||||||||
| 16 | # TODO: multi-voice staves -- split simultaneous RestChord streams into | ||||||||||||||||||||||||||||
| 17 | # MusicXML voice 2+ with independent stem directions | ||||||||||||||||||||||||||||
| 18 | # TODO: verify lyrics end-to-end against a golden NWC file that contains | ||||||||||||||||||||||||||||
| 19 | # Lyric records (_annotate_lyrics and emission code both exist) | ||||||||||||||||||||||||||||
| 20 | # TODO: verify FlowControl round-trip against a golden NWC file that contains | ||||||||||||||||||||||||||||
| 21 | # Coda/Segno/DaCapo marks (_emit_flow_control is wired but untested) | ||||||||||||||||||||||||||||
| 22 | # TODO: fill Z calculus placeholders in FORMAL SPECIFICATION POD sections | ||||||||||||||||||||||||||||
| 23 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use File::Basename qw(basename dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use Params::Get; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use Music::NWC2MusicXML::NWC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use Music::NWC2MusicXML::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | use Music::NWC2MusicXML::MusicXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | use Music::NWC2MusicXML::Diagnostics; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 39 | # Exit codes (also exported for use by the CLI script) | ||||||||||||||||||||||||||||
| 40 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 41 | 5 | Readonly::Scalar our $EXIT_OK => 0; | |||||||||||||||||||||||||||
| 42 | 5 | Readonly::Scalar our $EXIT_WARNINGS => 1; | |||||||||||||||||||||||||||
| 43 | 5 | Readonly::Scalar our $EXIT_BAD_INPUT => 2; | |||||||||||||||||||||||||||
| 44 | 5 | Readonly::Scalar our $EXIT_OUTPUT => 3; | |||||||||||||||||||||||||||
| 45 | 5 | Readonly::Scalar our $EXIT_INTERNAL => 4; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Default output extension for uncompressed MusicXML | ||||||||||||||||||||||||||||
| 48 | 5 | Readonly::Scalar my $OUTPUT_EXT => '.musicxml'; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Maximum single-file decompressed size (forwarded to NWC decoder) | ||||||||||||||||||||||||||||
| 51 | 5 | Readonly::Scalar my $INPUT_EXT => '.nwc'; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 5 | Readonly::Hash my %MESSAGES => ( | |||||||||||||||||||||||||||
| 54 | error_no_input => 'No input file or data specified', | ||||||||||||||||||||||||||||
| 55 | error_no_output => 'No output path could be determined', | ||||||||||||||||||||||||||||
| 56 | error_file_not_found => 'Input file not found: %s', | ||||||||||||||||||||||||||||
| 57 | error_decode => 'NWC decoding failed for %s: %s', | ||||||||||||||||||||||||||||
| 58 | error_parse => 'NWCTXT parsing failed for %s: %s', | ||||||||||||||||||||||||||||
| 59 | error_generate => 'MusicXML generation failed for %s: %s', | ||||||||||||||||||||||||||||
| 60 | error_write => 'Cannot write output %s: %s', | ||||||||||||||||||||||||||||
| 61 | error_mkdir => 'Cannot create output directory %s: %s', | ||||||||||||||||||||||||||||
| 62 | error_traversal => 'Path traversal rejected: %s is outside base_dir %s', | ||||||||||||||||||||||||||||
| 63 | error_internal => 'Internal error: %s', | ||||||||||||||||||||||||||||
| 64 | info_converting => 'Converting: %s -> %s', | ||||||||||||||||||||||||||||
| 65 | info_done => 'Done: %s', | ||||||||||||||||||||||||||||
| 66 | info_skipped => 'Skipped (output exists, --overwrite not set): %s', | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Music::NWC2MusicXML - Convert NoteWorthy Composer 2 C<.nwc> score files to MusicXML. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 VERSION | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0.003.0 | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Simple conversion | ||||||||||||||||||||||||||||
| 80 | use Music::NWC2MusicXML; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my $converter = Music::NWC2MusicXML->new; | ||||||||||||||||||||||||||||
| 83 | $converter->convert( | ||||||||||||||||||||||||||||
| 84 | input => 'Pilgrim.nwc', | ||||||||||||||||||||||||||||
| 85 | output => 'Pilgrim.musicxml', | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Batch conversion | ||||||||||||||||||||||||||||
| 89 | $converter->batch_convert( | ||||||||||||||||||||||||||||
| 90 | inputs => [ glob('*.nwc') ], | ||||||||||||||||||||||||||||
| 91 | output_dir => 'musicxml', | ||||||||||||||||||||||||||||
| 92 | overwrite => 1, | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | C<Music::NWC2MusicXML> is the top-level facade for the NWC-to-MusicXML conversion | ||||||||||||||||||||||||||||
| 98 | pipeline. It coordinates three independent stages: | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =over 4 | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item 1. B<NWC binary decoding> (C<Music::NWC2MusicXML::NWC>) -- reads the C<.nwc> | ||||||||||||||||||||||||||||
| 103 | binary container, verifies the magic signature, decompresses the zlib payload, | ||||||||||||||||||||||||||||
| 104 | and extracts the NWCTXT text representation. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item 2. B<NWCTXT parsing> (C<Music::NWC2MusicXML::Parser>) -- parses the NWCTXT into | ||||||||||||||||||||||||||||
| 107 | an internal representation (C<Music::NWC2MusicXML::Score>). | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item 3. B<MusicXML generation> (C<Music::NWC2MusicXML::MusicXML>) -- serialises the | ||||||||||||||||||||||||||||
| 110 | internal representation to a well-formed UTF-8 MusicXML document. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Each stage is independently testable. The facade wires them together, | ||||||||||||||||||||||||||||
| 115 | handles batch processing, and routes all diagnostics through a single | ||||||||||||||||||||||||||||
| 116 | C<Music::NWC2MusicXML::Diagnostics> instance. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 PRESERVATION PRINCIPLE | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | The guiding principle of the conversion is: | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | I<Preserve musical meaning rather than graphical appearance.> | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Priority order: notes and rhythm > voices > measures > articulations > | ||||||||||||||||||||||||||||
| 125 | dynamics > lyrics > structural markings > instrument info > graphical layout. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 130 | # new | ||||||||||||||||||||||||||||
| 131 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 new | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Construct a converter. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head3 Purpose | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Creates a configured converter instance that can be reused for multiple | ||||||||||||||||||||||||||||
| 140 | conversions without reconstructing the pipeline components each time. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head3 Arguments | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Named parameters: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =over 4 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item C<log_level> -- C<quiet>, C<normal> (default), C<verbose>, C<debug>. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item C<warnings_fh> -- filehandle for warning output (optional; defaults to | ||||||||||||||||||||||||||||
| 151 | STDERR in the C<Diagnostics> object). | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item C<validate> -- perform extended consistency checks (boolean, default 0). | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head3 Returns | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Blessed C<Music::NWC2MusicXML> object. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head3 Usage Example | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | my $c = Music::NWC2MusicXML->new(log_level => 'verbose', validate => 1); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head4 Input | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | log_level : SCALAR (optional, default 'normal') | ||||||||||||||||||||||||||||
| 170 | warnings_fh : (filehandle, optional) | ||||||||||||||||||||||||||||
| 171 | validate : SCALAR (optional, default 0) | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head4 Output | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Music::NWC2MusicXML object | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head3 MESSAGES | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | None. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 39 | my $class = shift; | |||||||||||||||||||||||||||
| 185 | 39 | ▶ | my $input = Params::Get::get_params(undef, \@_) // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params(undef, \@_) // {}
MC/DC0%0/1
Params::Get::get_params(undef, \@_) // {}
Params::Get::get_params(undef, \@_)
Truth table50%1/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||||||||||
| 186 | 39 | my $warnings_fh = delete $input->{warnings_fh}; # must be extracted before validate_strict (no glob type) | |||||||||||||||||||||||||||
| 187 | 39 | my $args = validate_strict( | |||||||||||||||||||||||||||
| 188 | input => $input, | ||||||||||||||||||||||||||||
| 189 | schema => { | ||||||||||||||||||||||||||||
| 190 | log_level => { type => 'scalar', optional => 1, default => 'normal' }, | ||||||||||||||||||||||||||||
| 191 | validate => { type => 'scalar', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 192 | }, | ||||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | 39 | ▶ | croak $@ unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 39 | $args = Object::Configure::configure($class, $args); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | my $diag = Music::NWC2MusicXML::Diagnostics->new( | ||||||||||||||||||||||||||||
| 199 | level => $args->{log_level}, | ||||||||||||||||||||||||||||
| 200 | 39 | ▶ | (defined $warnings_fh ? (warnings_fh => $warnings_fh) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | ); | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | return bless { | ||||||||||||||||||||||||||||
| 204 | _diagnostics => $diag, | ||||||||||||||||||||||||||||
| 205 | _validate => $args->{validate}, | ||||||||||||||||||||||||||||
| 206 | 39 | _decoder => Music::NWC2MusicXML::NWC->new(diagnostics => $diag), | |||||||||||||||||||||||||||
| 207 | _parser => Music::NWC2MusicXML::Parser->new(diagnostics => $diag), | ||||||||||||||||||||||||||||
| 208 | _generator => Music::NWC2MusicXML::MusicXML->new(diagnostics => $diag), | ||||||||||||||||||||||||||||
| 209 | }, $class; | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 213 | # Public: convert | ||||||||||||||||||||||||||||
| 214 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head2 convert | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Convert a single C<.nwc> file to MusicXML. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head3 Purpose | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Main single-file conversion entry point. Chains decoder -> parser -> | ||||||||||||||||||||||||||||
| 223 | generator, writes the output file, and updates the internal diagnostic counters. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head3 Arguments | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Named parameters: | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =over 4 | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item C<input> -- path to the C<.nwc> input file (required). | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =item C<output> -- path for the C<.musicxml> output file (optional). | ||||||||||||||||||||||||||||
| 234 | Defaults to the input path with the extension replaced by C<.musicxml>. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item C<overwrite> -- if false (default), skip conversion when the output file | ||||||||||||||||||||||||||||
| 237 | already exists. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head3 Returns | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Scalar string -- the output file path if conversion succeeded, or undef on | ||||||||||||||||||||||||||||
| 244 | failure. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head3 Side Effects | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Writes the output file. Updates diagnostic counters. | ||||||||||||||||||||||||||||
| 249 | Croaks on fatal errors; non-fatal issues are issued as warnings. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head3 Usage Example | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | my $out = $c->convert(input => 'Pilgrim.nwc', overwrite => 1); | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head4 Input | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | input : SCALAR (path, required) | ||||||||||||||||||||||||||||
| 260 | output : SCALAR (path, optional) | ||||||||||||||||||||||||||||
| 261 | overwrite : boolean (optional, default false) | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =head4 Output | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | SCALAR (output path) or undef | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head3 MESSAGES | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | | Code | Meaning | Resolution | | ||||||||||||||||||||||||||||
| 270 | |--------------------|--------------------------------------|-------------------------------| | ||||||||||||||||||||||||||||
| 271 | | error_no_input | C<input> parameter missing | Provide input path | | ||||||||||||||||||||||||||||
| 272 | | error_file_not_found| Input file does not exist | Check path | | ||||||||||||||||||||||||||||
| 273 | | error_decode | NWC decoding stage failed | See error detail | | ||||||||||||||||||||||||||||
| 274 | | error_parse | NWCTXT parsing stage failed | See error detail | | ||||||||||||||||||||||||||||
| 275 | | error_generate | MusicXML generation failed | See error detail | | ||||||||||||||||||||||||||||
| 276 | | error_write | Cannot write output file | Check permissions / disk space| | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =cut | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 50 | ▶ | sub convert { | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 50 | my ($self, %input) = @_; | |||||||||||||||||||||||||||
| 282 | 50 | my $args = validate_strict( | |||||||||||||||||||||||||||
| 283 | schema => { | ||||||||||||||||||||||||||||
| 284 | input => { type => 'scalar' }, | ||||||||||||||||||||||||||||
| 285 | output => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||
| 286 | overwrite => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 287 | }, | ||||||||||||||||||||||||||||
| 288 | input => \%input, | ||||||||||||||||||||||||||||
| 289 | ); | ||||||||||||||||||||||||||||
| 290 | 50 | ▶ | croak $@ unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 50 | my $in = $args->{input}; | |||||||||||||||||||||||||||
| 293 | 50 | ▶ | my $output = $args->{output} // _default_output($in); | ||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"output"} // _default_output($in)
MC/DC0%0/2
$args->{"output"} // _default_output($in)
$args->{"output"}
_default_output($in)
Truth table66%2/3
A // B
A= $args->{"output"}B= _default_output($in)
| |||||||||||||||||||||||||||||
| 294 | 50 | my $diag = $self->{_diagnostics}; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 50 | ▶ | croak _fmt_msg('error_file_not_found', $in) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | unless -f $in; | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 35 | ▶ | if (!$args->{overwrite} && -f $output) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $args->{"overwrite"} and -f $output
MC/DC100%2/2
not $args->{"overwrite"} and -f $output
not $args->{"overwrite"}
-f $output
Truth table100%3/3
not(A) and B
A= not $args->{"overwrite"}B= -f $output
| |||||||||||||||||||||||||||||
| 300 | 3 | $diag->verbose(_fmt_msg('info_skipped', $output)); | |||||||||||||||||||||||||||
| 301 | 3 | return $output; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 32 | $diag->count(outcome => 'processed'); | |||||||||||||||||||||||||||
| 305 | 32 | $diag->info(_fmt_msg('info_converting', $in, $output)); | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 32 | return $self->_single_convert($in, $output); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 311 | # Public: batch_convert | ||||||||||||||||||||||||||||
| 312 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head2 batch_convert | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Convert multiple C<.nwc> files, optionally into a separate output directory. | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head3 Purpose | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Processes a list of input files in sequence. Failures on individual files | ||||||||||||||||||||||||||||
| 321 | are caught and counted; conversion continues with remaining files. | ||||||||||||||||||||||||||||
| 322 | A summary is printed at the end. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head3 Arguments | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Named parameters: | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =over 4 | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =item C<inputs> -- arrayref of input file paths (required). | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =item C<output_dir> -- directory for output files (optional; defaults to each | ||||||||||||||||||||||||||||
| 333 | file's own directory). | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =item C<overwrite> -- overwrite existing output files (boolean, default 0). | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =item C<recursive> -- preserve relative directory structure under | ||||||||||||||||||||||||||||
| 338 | C<output_dir> (boolean, default 0). | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =item C<base_dir> -- base directory stripped when computing relative paths | ||||||||||||||||||||||||||||
| 341 | for recursive mode (optional). | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =back | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =head3 Returns | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Hashref: C<< { processed => N, successful => N, warnings => N, failed => N } >>. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head3 Side Effects | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Writes output files. Prints a summary to STDERR. | ||||||||||||||||||||||||||||
| 352 | Does not croak on per-file failures. | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head3 Usage Example | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | $c->batch_convert( | ||||||||||||||||||||||||||||
| 357 | inputs => [ glob('scores/**/*.nwc') ], | ||||||||||||||||||||||||||||
| 358 | output_dir => 'musicxml', | ||||||||||||||||||||||||||||
| 359 | recursive => 1, | ||||||||||||||||||||||||||||
| 360 | base_dir => 'scores', | ||||||||||||||||||||||||||||
| 361 | overwrite => 1, | ||||||||||||||||||||||||||||
| 362 | ); | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head4 Input | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | inputs : ARRAYREF of SCALAR paths (required) | ||||||||||||||||||||||||||||
| 369 | output_dir : SCALAR (optional) | ||||||||||||||||||||||||||||
| 370 | overwrite : SCALAR bool (optional, default 0) | ||||||||||||||||||||||||||||
| 371 | recursive : SCALAR bool (optional, default 0) | ||||||||||||||||||||||||||||
| 372 | base_dir : SCALAR (optional) | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head4 Output | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | HASHREF { processed:int, successful:int, warnings:int, failed:int } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 14 | ▶ | sub batch_convert { | ||||||||||||||||||||||||||
|
Subroutine
batch_convert: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 381 | 14 | my ($self, %input) = @_; | |||||||||||||||||||||||||||
| 382 | 14 | my $args = validate_strict( | |||||||||||||||||||||||||||
| 383 | schema => { | ||||||||||||||||||||||||||||
| 384 | inputs => { | ||||||||||||||||||||||||||||
| 385 | type => 'arrayref', | ||||||||||||||||||||||||||||
| 386 | element_type => 'string' | ||||||||||||||||||||||||||||
| 387 | }, | ||||||||||||||||||||||||||||
| 388 | output_dir => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||
| 389 | overwrite => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 390 | recursive => { type => 'boolean', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 391 | base_dir => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||
| 392 | }, | ||||||||||||||||||||||||||||
| 393 | input => \%input, | ||||||||||||||||||||||||||||
| 394 | ); | ||||||||||||||||||||||||||||
| 395 | 14 | ▶ | croak $@ unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 14 | my $diag = $self->{_diagnostics}; | |||||||||||||||||||||||||||
| 398 | 14 | my @results; | |||||||||||||||||||||||||||
| 399 | 14 | my %counts = (processed => 0, successful => 0, warnings => 0, failed => 0); | |||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 14 | for my $file (@{ $args->{inputs} }) { | |||||||||||||||||||||||||||
| 402 | 21 | $counts{processed}++; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | # A per-file failure must not abort the batch. | ||||||||||||||||||||||||||||
| 405 | # Output-path computation is inside the eval so traversal errors | ||||||||||||||||||||||||||||
| 406 | # are caught per-file rather than aborting the whole batch. | ||||||||||||||||||||||||||||
| 407 | 21 | my $output; | |||||||||||||||||||||||||||
| 408 | 21 | my $ok = eval { | |||||||||||||||||||||||||||
| 409 | $output = $self->_batch_output_path( | ||||||||||||||||||||||||||||
| 410 | input => $file, | ||||||||||||||||||||||||||||
| 411 | output_dir => $args->{output_dir}, | ||||||||||||||||||||||||||||
| 412 | recursive => $args->{recursive}, | ||||||||||||||||||||||||||||
| 413 | base_dir => $args->{base_dir}, | ||||||||||||||||||||||||||||
| 414 | 21 | ); | |||||||||||||||||||||||||||
| 415 | $self->convert( | ||||||||||||||||||||||||||||
| 416 | input => $file, | ||||||||||||||||||||||||||||
| 417 | output => $output, | ||||||||||||||||||||||||||||
| 418 | overwrite => $args->{overwrite}, | ||||||||||||||||||||||||||||
| 419 | 20 | ); | |||||||||||||||||||||||||||
| 420 | }; | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 21 | ▶ | if ($@ || !defined $ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ or not defined $ok
MC/DC100%2/2
$@ or not defined $ok
$@
not defined $ok
Truth table100%3/3
A or not(B)
A= $@B= not defined $ok
| |||||||||||||||||||||||||||||
| 423 | 8 | ▶ | $diag->info(_fmt_msg('info_done', "FAILED: $file -- $@")) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 8 | $diag->count(outcome => 'failed'); | |||||||||||||||||||||||||||
| 425 | 8 | $counts{failed}++; | |||||||||||||||||||||||||||
| 426 | } else { | ||||||||||||||||||||||||||||
| 427 | 13 | $diag->count(outcome => 'successful'); | |||||||||||||||||||||||||||
| 428 | 13 | $counts{successful}++; | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 21 | push @results, { input => $file, output => $output, ok => !!$ok }; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 14 | $counts{warnings} = scalar @{ $diag->warnings }; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 14 | $diag->summary; | |||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 14 | return { %counts, results => \@results }; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head2 diagnostics | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | Return the C<Music::NWC2MusicXML::Diagnostics> instance. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =cut | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 0 | ▶ | sub diagnostics { return $_[0]->{_diagnostics} } | ||||||||||||||||||||||||||
|
Subroutine
diagnostics: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 450 | # Private helpers | ||||||||||||||||||||||||||||
| 451 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 32 | ▶ | sub _single_convert { | ||||||||||||||||||||||||||
|
Subroutine
_single_convert: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||
| 454 | 32 | my ($self, $input, $output) = @_; | |||||||||||||||||||||||||||
| 455 | 32 | my $diag = $self->{_diagnostics}; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | # Stage 1: Decode binary NWC -> NWCTXT | ||||||||||||||||||||||||||||
| 458 | 32 | my $nwctxt = eval { $self->{_decoder}->read($input) }; | |||||||||||||||||||||||||||
| 459 | 32 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | 6 | carp _fmt_msg('error_decode', $input, $@); | |||||||||||||||||||||||||||
| 461 | 6 | $diag->count(outcome => 'failed'); | |||||||||||||||||||||||||||
| 462 | 6 | return undef; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | # Stage 2: Parse NWCTXT -> IR | ||||||||||||||||||||||||||||
| 466 | 26 | my $score = eval { $self->{_parser}->parse($nwctxt) }; | |||||||||||||||||||||||||||
| 467 | 26 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | 0 | carp _fmt_msg('error_parse', $input, $@); | |||||||||||||||||||||||||||
| 469 | 0 | $diag->count(outcome => 'failed'); | |||||||||||||||||||||||||||
| 470 | 0 | return undef; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | # Optional: extended validation | ||||||||||||||||||||||||||||
| 474 | 26 | ▶ | if ($self->{_validate}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | 1 | my $issues = $score->validate; | |||||||||||||||||||||||||||
| 476 | 1 | for my $issue (@$issues) { | |||||||||||||||||||||||||||
| 477 | 2 | carp $issue; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | # Stage 3: Generate MusicXML | ||||||||||||||||||||||||||||
| 482 | 26 | my $xml = eval { $self->{_generator}->generate($score) }; | |||||||||||||||||||||||||||
| 483 | 26 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | 1 | carp _fmt_msg('error_generate', $input, $@); | |||||||||||||||||||||||||||
| 485 | 1 | $diag->count(outcome => 'failed'); | |||||||||||||||||||||||||||
| 486 | 1 | return undef; | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | # Write output | ||||||||||||||||||||||||||||
| 490 | 25 | eval { $self->_write_output($output, $xml, $input) }; | |||||||||||||||||||||||||||
| 491 | 25 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | 0 | carp _fmt_msg('error_write', $output, $@); | |||||||||||||||||||||||||||
| 493 | 0 | $diag->count(outcome => 'failed'); | |||||||||||||||||||||||||||
| 494 | 0 | return undef; | |||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 25 | $diag->info(_fmt_msg('info_done', $output)); | |||||||||||||||||||||||||||
| 498 | 25 | $diag->count(outcome => 'successful'); | |||||||||||||||||||||||||||
| 499 | 25 | return $output; | |||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 25 | ▶ | sub _write_output { | ||||||||||||||||||||||||||
|
Subroutine
_write_output: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 503 | 25 | my ($self, $output, $xml, $source) = @_; | |||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | # Ensure parent directory exists. make_path is idempotent (no error if dir | ||||||||||||||||||||||||||||
| 506 | # already exists), so no -d pre-check is needed and the TOCTOU race is closed. | ||||||||||||||||||||||||||||
| 507 | 25 | my $dir = dirname($output); | |||||||||||||||||||||||||||
| 508 | 25 | eval { make_path($dir) }; | |||||||||||||||||||||||||||
| 509 | 25 | ▶ | croak _fmt_msg('error_mkdir', $dir, $@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 25 | ▶ | open my $fh, '>:encoding(UTF-8)', $output | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 512 | or croak _fmt_msg('error_write', $output, $!); | ||||||||||||||||||||||||||||
| 513 | 25 | print $fh $xml; | |||||||||||||||||||||||||||
| 514 | 25 | close $fh; | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 25 | return; | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 13 | ▶ | sub _default_output { | ||||||||||||||||||||||||||
|
Subroutine
_default_output: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 520 | 13 | my ($input) = @_; | |||||||||||||||||||||||||||
| 521 | 13 | (my $base = $input) =~ s/\Q$INPUT_EXT\E\z//i; | |||||||||||||||||||||||||||
| 522 | 13 | return $base . $OUTPUT_EXT; | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 21 | ▶ | sub _batch_output_path { | ||||||||||||||||||||||||||
|
Subroutine
_batch_output_path: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 526 | 21 | my ($self, %input) = @_; | |||||||||||||||||||||||||||
| 527 | 21 | my $args = validate_strict( | |||||||||||||||||||||||||||
| 528 | schema => { | ||||||||||||||||||||||||||||
| 529 | input => { type => 'scalar' }, | ||||||||||||||||||||||||||||
| 530 | output_dir => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||
| 531 | recursive => { type => 'scalar', optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 532 | base_dir => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||
| 533 | }, | ||||||||||||||||||||||||||||
| 534 | input => \%input, | ||||||||||||||||||||||||||||
| 535 | ); | ||||||||||||||||||||||||||||
| 536 | 21 | ▶ | croak $@ unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 21 | my $out_name = basename($args->{input}); | |||||||||||||||||||||||||||
| 539 | 21 | $out_name =~ s/\Q$INPUT_EXT\E\z//i; | |||||||||||||||||||||||||||
| 540 | 21 | $out_name .= $OUTPUT_EXT; | |||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 21 | ▶ | unless (defined $args->{output_dir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 543 | 0 | return File::Spec->catfile(dirname($args->{input}), $out_name); | |||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | 21 | ▶ | if ($args->{recursive} && defined $args->{base_dir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"recursive"} and defined $args->{"base_dir"}
MC/DC50%1/2
$args->{"recursive"} and defined $args->{"base_dir"}
$args->{"recursive"}
defined $args->{"base_dir"}
Truth table66%2/3
A and B
A= $args->{"recursive"}B= defined $args->{"base_dir"}
| |||||||||||||||||||||||||||||
| 547 | # Compute relative path from base_dir to preserve directory structure | ||||||||||||||||||||||||||||
| 548 | 3 | my $rel = File::Spec->abs2rel(dirname($args->{input}), $args->{base_dir}); | |||||||||||||||||||||||||||
| 549 | # Guard: any '..' component means the input sits outside base_dir; | ||||||||||||||||||||||||||||
| 550 | # writing there would escape output_dir (path traversal). | ||||||||||||||||||||||||||||
| 551 | croak _fmt_msg('error_traversal', $args->{input}, $args->{base_dir}) | ||||||||||||||||||||||||||||
| 552 | 3 | ▶ | if grep { $_ eq '..' } File::Spec->splitdir($rel); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | 2 | return File::Spec->catfile($args->{output_dir}, $rel, $out_name); | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | 18 | return File::Spec->catfile($args->{output_dir}, $out_name); | |||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | 88 | ▶ | sub _fmt_msg { | ||||||||||||||||||||||||||
|
Subroutine
_fmt_msg: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 560 | 88 | my ($key, @args) = @_; | |||||||||||||||||||||||||||
| 561 | 88 | ▶ | croak "Unknown message key: $key" unless exists $MESSAGES{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 562 | 88 | return sprintf $MESSAGES{$key}, @args; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 5 | 1; | |||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | __END__ | ||||||||||||||||||||||||||||