| 1 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Text::Unpack::Auto; | ||||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: automatically generates unpack strings | ||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use parent 'Exporter'; # inherit all of Exporter's methods | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use List::Util qw(reduce pairmap max min); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | our @EXPORT = qw(guess_unpack auto_unpack); | |||||||||
| 11 | 3 | our @EXPORT_OK = qw(); # symbols to export on reques | |||||||||
| 12 | |||||||||||
| 13 | 19 | ▶ | sub rle_encode { shift =~ s/(.)\1*/$1 . ":" . length($&) . " "/grse } | ||||||||
|
Subroutine
rle_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | ▶ | sub rle_decode { shift =~ s/(\d+):(.) /$2 x $1/grse } | ||||||||
|
Subroutine
rle_decode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | |||||||||||
| 17 | 90 | ▶ | sub rle_to_unpack { join '', pairmap { ($a ? 'a' : 'x') . $b } (map { split ":", $_ } split ' ', shift) } | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Subroutine
rle_to_unpack: called CC=1 SCAR=0.0
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 18 | |||||||||||
| 19 | 18 | ▶ | sub close_gaps { | ||||||||
|
Subroutine
close_gaps: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 20 | 18 | my ($str, $n) = @_; | |||||||||
| 21 | 18 | ▶ | return $str unless $n > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 16 | my $min_gap = $n - 1; | |||||||||
| 23 | 16 | $str =~ s/ | |||||||||
| 24 | (?<=1) # preceded by a data column (lookbehind) | ||||||||||
| 25 | ( # capture the gap | ||||||||||
| 26 | 0{1,$min_gap} # whitespace columns, up to $min_gap wide | ||||||||||
| 27 | ) # gaps wider than this are real column separators | ||||||||||
| 28 | (?=1) # followed by a data column (lookahead) | ||||||||||
| 29 | 9 | /1 x length($1)/gex; | |||||||||
| 30 | 16 | return $str; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 17 | ▶ | sub guess_unpack { | ||||||||
|
Subroutine
guess_unpack: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 34 | 17 | ▶ | my $opts = ref $_[0] eq 'HASH' ? shift : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 17 | my @zeros = map { my $z = s/\S/1/gr; $z =~ s/\s/0/g; $z } @_; | |||||||||
| 37 | |||||||||||
| 38 | 17 | ▶ | my $result = reduce { $a | $b } @zeros; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 17 | ▶ | $result = close_gaps($result, $opts->{minimum_gap}) if ($opts->{minimum_gap}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 17 | my $unpack = rle_to_unpack(rle_encode($result)); | |||||||||
| 41 | 17 | return $unpack; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 7 | ▶ | sub auto_unpack { | ||||||||
|
Subroutine
auto_unpack: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | 7 | ▶ | my $opts = ref $_[0] eq 'HASH' ? shift : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 7 | my @lines = @_; | |||||||||
| 48 | 7 | my $unpack = guess_unpack($opts, @lines); | |||||||||
| 49 | 7 | my $ml = max map { length($_) } @lines; | |||||||||
| 50 | |||||||||||
| 51 | 7 | return map { [ map { s/^\s+|\s+$//gr } unpack $unpack, sprintf "%-${ml}s", $_ ] } @lines | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 3 | 1; | |||||||||
| 55 | |||||||||||
| 56 | =head1 NAME | ||||||||||
| 57 | |||||||||||
| 58 | Text::Unpack::Auto - automatically generate unpack strings from fixed-width text | ||||||||||
| 59 | |||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||
| 61 | |||||||||||
| 62 | use Text::Unpack::Auto; | ||||||||||
| 63 | |||||||||||
| 64 | my @rows = auto_unpack(@lines); | ||||||||||
| 65 | for my $row (@rows) { | ||||||||||
| 66 | say join ', ', @$row; | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | my $fmt = guess_unpack(@lines); | ||||||||||
| 70 | |||||||||||
| 71 | my @rows = auto_unpack({ minimum_gap => 3 }, @lines); | ||||||||||
| 72 | |||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||
| 74 | |||||||||||
| 75 | Detects fixed-width column boundaries in plain text and unpacks lines into | ||||||||||
| 76 | fields. | ||||||||||
| 77 | |||||||||||
| 78 | =head1 FUNCTIONS | ||||||||||
| 79 | |||||||||||
| 80 | L<Text::Unpack::Auto> exports L</"guess_unpack"> and L</"auto_unpack"> by | ||||||||||
| 81 | default. | ||||||||||
| 82 | |||||||||||
| 83 | =head2 guess_unpack | ||||||||||
| 84 | |||||||||||
| 85 | my $fmt = guess_unpack(@lines); | ||||||||||
| 86 | my $fmt = guess_unpack(\%opts, @lines); | ||||||||||
| 87 | |||||||||||
| 88 | Returns an L<unpack|perlfunc/unpack> template string derived from the column | ||||||||||
| 89 | boundaries detected in C<@lines>. | ||||||||||
| 90 | |||||||||||
| 91 | =head2 auto_unpack | ||||||||||
| 92 | |||||||||||
| 93 | my @rows = auto_unpack(@lines); | ||||||||||
| 94 | my @rows = auto_unpack(\%opts, @lines); | ||||||||||
| 95 | |||||||||||
| 96 | Unpacks each line into an arrayref of trimmed fields. Returns one arrayref per | ||||||||||
| 97 | input line. | ||||||||||
| 98 | |||||||||||
| 99 | =head2 Options | ||||||||||
| 100 | |||||||||||
| 101 | =over 2 | ||||||||||
| 102 | |||||||||||
| 103 | =item minimum_gap | ||||||||||
| 104 | |||||||||||
| 105 | minimum_gap => 3 | ||||||||||
| 106 | |||||||||||
| 107 | Gaps narrower than this are treated as part of the surrounding column rather | ||||||||||
| 108 | than as column separators. | ||||||||||
| 109 | |||||||||||
| 110 | =back | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | =head1 AUTHOR | ||||||||||
| 114 | |||||||||||
| 115 | Simone Cesano | ||||||||||
| 116 | |||||||||||
| 117 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 118 | |||||||||||
| 119 | Copyright (C) 2026 Simone Cesano | ||||||||||
| 120 | |||||||||||
| 121 | This library is free software; you may redistribute it and/or modify it under | ||||||||||
| 122 | the same terms as Perl itself. | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||
| 126 | 3 | 1; | |||||||||