| 1 | # ----------------------------------------------------------------------------- | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Quiq::ExportFile - Manipuliere Exportdatei | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||
| 10 | |||||||||||||
| 11 | L<Quiq::Hash> | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||
| 14 | |||||||||||||
| 15 | Die Klasse definiert Operationen auf Exportdateien der Art: | ||||||||||||
| 16 | |||||||||||||
| 17 | per_id|per_vorname|per_nachname | ||||||||||||
| 18 | 1|Rudi|Ratlos | ||||||||||||
| 19 | 2|Elli|Pirelli | ||||||||||||
| 20 | 3|Susi|Sorglos | ||||||||||||
| 21 | 4|Kai|Nelust | ||||||||||||
| 22 | 5|Lieschen|Müller | ||||||||||||
| 23 | 6|Mark|Kiese | ||||||||||||
| 24 | 7|Andreas|Kreuz | ||||||||||||
| 25 | 8|Annie|Malisch | ||||||||||||
| 26 | 9|Kurt|Schluss | ||||||||||||
| 27 | 10|Franz|Brötchen | ||||||||||||
| 28 | 11|Klara|Fall | ||||||||||||
| 29 | 12|Carmen|Bär | ||||||||||||
| 30 | 13|Nico|Laus | ||||||||||||
| 31 | 14|Vitali|Sierend | ||||||||||||
| 32 | 15|Hella|Wahnsinn | ||||||||||||
| 33 | 16|Knut|Schmund | ||||||||||||
| 34 | 17|Ernst|Fall | ||||||||||||
| 35 | 18|Rainer|Zufall | ||||||||||||
| 36 | 19|Erika|Mustermann | ||||||||||||
| 37 | |||||||||||||
| 38 | =cut | ||||||||||||
| 39 | |||||||||||||
| 40 | # ----------------------------------------------------------------------------- | ||||||||||||
| 41 | |||||||||||||
| 42 | package Quiq::ExportFile; | ||||||||||||
| 43 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | |||||||||||||
| 45 | 1 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | |||||||||||||
| 49 | 1 | our $VERSION = '1.240'; | |||||||||||
| 50 | |||||||||||||
| 51 | 1 | ▶ | use Quiq::Path; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | 1 | ▶ | use Quiq::FileHandle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | |||||||||||||
| 54 | # ----------------------------------------------------------------------------- | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 METHODS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 Konstruktor | ||||||||||||
| 59 | |||||||||||||
| 60 | =head3 new() - Instantiiere Objekt | ||||||||||||
| 61 | |||||||||||||
| 62 | =head4 Synopsis | ||||||||||||
| 63 | |||||||||||||
| 64 | $exf = $class->new($file); | ||||||||||||
| 65 | |||||||||||||
| 66 | =head4 Arguments | ||||||||||||
| 67 | |||||||||||||
| 68 | =over 4 | ||||||||||||
| 69 | |||||||||||||
| 70 | =item $file | ||||||||||||
| 71 | |||||||||||||
| 72 | Pfad der Exportdatei. | ||||||||||||
| 73 | |||||||||||||
| 74 | =back | ||||||||||||
| 75 | |||||||||||||
| 76 | =head4 Returns | ||||||||||||
| 77 | |||||||||||||
| 78 | Objekt | ||||||||||||
| 79 | |||||||||||||
| 80 | =head4 Description | ||||||||||||
| 81 | |||||||||||||
| 82 | Instantiiere ein Objekt der Klasse und liefere dieses zurück. | ||||||||||||
| 83 | |||||||||||||
| 84 | =cut | ||||||||||||
| 85 | |||||||||||||
| 86 | # ----------------------------------------------------------------------------- | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 89 | 0 | my ($class,$file) = @_; | |||||||||||
| 90 | |||||||||||||
| 91 | 0 | return $class->SUPER::new( | |||||||||||
| 92 | file => $file, | ||||||||||||
| 93 | ); | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | # ----------------------------------------------------------------------------- | ||||||||||||
| 97 | |||||||||||||
| 98 | =head2 Objektmethoden | ||||||||||||
| 99 | |||||||||||||
| 100 | =head3 addColumn() - Füge Kolumne hinzu | ||||||||||||
| 101 | |||||||||||||
| 102 | =head4 Synopsis | ||||||||||||
| 103 | |||||||||||||
| 104 | $exf->addColumn($column); | ||||||||||||
| 105 | $exf->addColumn($column,$value); | ||||||||||||
| 106 | |||||||||||||
| 107 | =head4 Arguments | ||||||||||||
| 108 | |||||||||||||
| 109 | =over 4 | ||||||||||||
| 110 | |||||||||||||
| 111 | =item $column | ||||||||||||
| 112 | |||||||||||||
| 113 | Name der Kolumne, die hinzugefügt werden soll. | ||||||||||||
| 114 | |||||||||||||
| 115 | =item $value (Default: '') | ||||||||||||
| 116 | |||||||||||||
| 117 | Setze den Wert der Kolumne auf $value. | ||||||||||||
| 118 | |||||||||||||
| 119 | =back | ||||||||||||
| 120 | |||||||||||||
| 121 | =head4 Description | ||||||||||||
| 122 | |||||||||||||
| 123 | Füge Kolumne $column mit Wert $value zur Exportdatei hinzu. | ||||||||||||
| 124 | |||||||||||||
| 125 | =head4 Example | ||||||||||||
| 126 | |||||||||||||
| 127 | $ perl -MQuiq::ExportFile -E \ | ||||||||||||
| 128 | 'Quiq::ExportFile->new("person.dat")->addColumn("per_geburtstag")' | ||||||||||||
| 129 | |||||||||||||
| 130 | =cut | ||||||||||||
| 131 | |||||||||||||
| 132 | # ----------------------------------------------------------------------------- | ||||||||||||
| 133 | |||||||||||||
| 134 | 0 | ▶ | sub addColumn { | ||||||||||
|
Subroutine
addColumn: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 135 | 0 | my ($self,$column,$value) = @_; | |||||||||||
| 136 | |||||||||||||
| 137 | 0 | ▶ | $value //= ''; | ||||||||||
|
Condition0%0/2
$value //= ""
MC/DC0%0/1
$value //= ""
$value
Truth table0%0/2
A
A= $value
| |||||||||||||
| 138 | |||||||||||||
| 139 | 0 | my $inFile = $self->file; | |||||||||||
| 140 | 0 | my $tmpFile = "$inFile.tmp"; | |||||||||||
| 141 | |||||||||||||
| 142 | 0 | my $p = Quiq::Path->new; | |||||||||||
| 143 | |||||||||||||
| 144 | 0 | my $fhIn = Quiq::FileHandle->new('<',$inFile); | |||||||||||
| 145 | 0 | my $fhOut = Quiq::FileHandle->new('>',$tmpFile); | |||||||||||
| 146 | |||||||||||||
| 147 | 0 | my $titles = <$fhIn>; | |||||||||||
| 148 | 0 | chomp $titles; | |||||||||||
| 149 | 0 | my @titles = split /\|/,$titles; | |||||||||||
| 150 | 0 | my $width = @titles; | |||||||||||
| 151 | |||||||||||||
| 152 | 0 | my $index = -1; | |||||||||||
| 153 | 0 | ▶ | for (my $i = 0; $i < @titles; $i++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | 0 | ▶ | if ($titles[$i] eq $column) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 0 | $index = $i; | |||||||||||
| 156 | 0 | last; | |||||||||||
| 157 | } | ||||||||||||
| 158 | } | ||||||||||||
| 159 | 0 | ▶ | if ($index >= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | # Kolumne existiert bereits -> nichts tun | ||||||||||||
| 161 | |||||||||||||
| 162 | 0 | $fhIn->close; | |||||||||||
| 163 | 0 | $fhOut->close; | |||||||||||
| 164 | |||||||||||||
| 165 | 0 | return; | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | 0 | push @titles,$column; | |||||||||||
| 169 | 0 | say $fhOut join('|',@titles); | |||||||||||
| 170 | |||||||||||||
| 171 | 0 | my $line = 1; | |||||||||||
| 172 | 0 | ▶ | while (<$fhIn>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 173 | 0 | $line++; | |||||||||||
| 174 | 0 | chomp; | |||||||||||
| 175 | 0 | my @arr = split /\|/,$_,-1; | |||||||||||
| 176 | 0 | ▶ | if (@arr != $width) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 177 | 0 | $self->throw( | |||||||||||
| 178 | 'EXPORTFILE-00002: Wrong column width', | ||||||||||||
| 179 | Column => $column, | ||||||||||||
| 180 | Width => scalar(@arr), | ||||||||||||
| 181 | ExpectedWidth => $width, | ||||||||||||
| 182 | Line => $line, | ||||||||||||
| 183 | ); | ||||||||||||
| 184 | } | ||||||||||||
| 185 | 0 | push @arr,$value; | |||||||||||
| 186 | 0 | say $fhOut join('|',@arr); | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | 0 | $fhIn->close; | |||||||||||
| 190 | 0 | $fhOut->close; | |||||||||||
| 191 | |||||||||||||
| 192 | 0 | $p->rename($tmpFile,$inFile); | |||||||||||
| 193 | |||||||||||||
| 194 | 0 | return; | |||||||||||
| 195 | } | ||||||||||||
| 196 | |||||||||||||
| 197 | # ----------------------------------------------------------------------------- | ||||||||||||
| 198 | |||||||||||||
| 199 | =head3 dropColumn() - Entferne Kolumne | ||||||||||||
| 200 | |||||||||||||
| 201 | =head4 Synopsis | ||||||||||||
| 202 | |||||||||||||
| 203 | $exf->dropColumn($column); | ||||||||||||
| 204 | |||||||||||||
| 205 | =head4 Arguments | ||||||||||||
| 206 | |||||||||||||
| 207 | =over 4 | ||||||||||||
| 208 | |||||||||||||
| 209 | =item $column | ||||||||||||
| 210 | |||||||||||||
| 211 | Name der Kolumne, die entfernt werden soll. | ||||||||||||
| 212 | |||||||||||||
| 213 | =back | ||||||||||||
| 214 | |||||||||||||
| 215 | =head4 Description | ||||||||||||
| 216 | |||||||||||||
| 217 | Entferne Kolumne $column aus der Exportdatei. | ||||||||||||
| 218 | |||||||||||||
| 219 | =head4 Example | ||||||||||||
| 220 | |||||||||||||
| 221 | $ perl -MQuiq::ExportFile -E \ | ||||||||||||
| 222 | 'Quiq::ExportFile->new("person.dat")->dropColumn("per_geburtstag")' | ||||||||||||
| 223 | |||||||||||||
| 224 | =cut | ||||||||||||
| 225 | |||||||||||||
| 226 | # ----------------------------------------------------------------------------- | ||||||||||||
| 227 | |||||||||||||
| 228 | 0 | ▶ | sub dropColumn { | ||||||||||
|
Subroutine
dropColumn: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 229 | 0 | my ($self,$column) = @_; | |||||||||||
| 230 | |||||||||||||
| 231 | 0 | my $inFile = $self->file; | |||||||||||
| 232 | 0 | my $tmpFile = "$inFile.tmp"; | |||||||||||
| 233 | |||||||||||||
| 234 | 0 | my $p = Quiq::Path->new; | |||||||||||
| 235 | |||||||||||||
| 236 | 0 | my $fhIn = Quiq::FileHandle->new('<',$inFile); | |||||||||||
| 237 | 0 | my $fhOut = Quiq::FileHandle->new('>',$tmpFile); | |||||||||||
| 238 | |||||||||||||
| 239 | 0 | my $titles = <$fhIn>; | |||||||||||
| 240 | 0 | chomp $titles; | |||||||||||
| 241 | 0 | my @titles = split /\|/,$titles; | |||||||||||
| 242 | 0 | my $width = @titles; | |||||||||||
| 243 | |||||||||||||
| 244 | 0 | my $index = -1; | |||||||||||
| 245 | 0 | ▶ | for (my $i = 0; $i < @titles; $i++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 246 | 0 | ▶ | if ($titles[$i] eq $column) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 247 | 0 | $index = $i; | |||||||||||
| 248 | 0 | last; | |||||||||||
| 249 | } | ||||||||||||
| 250 | } | ||||||||||||
| 251 | 0 | ▶ | if ($index < 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 252 | 0 | $self->throw( | |||||||||||
| 253 | 'EXPORTFILE-00001: Column not found', | ||||||||||||
| 254 | Column => $column, | ||||||||||||
| 255 | ); | ||||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | 0 | splice @titles,$index,1; | |||||||||||
| 259 | 0 | say $fhOut join('|',@titles); | |||||||||||
| 260 | |||||||||||||
| 261 | 0 | my $line = 1; | |||||||||||
| 262 | 0 | ▶ | while (<$fhIn>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 263 | 0 | $line++; | |||||||||||
| 264 | 0 | chomp; | |||||||||||
| 265 | 0 | my @arr = split /\|/,$_,-1; | |||||||||||
| 266 | 0 | ▶ | if (@arr != $width) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 267 | 0 | $self->throw( | |||||||||||
| 268 | 'EXPORTFILE-00002: Wrong column width', | ||||||||||||
| 269 | Column => $column, | ||||||||||||
| 270 | Width => scalar(@arr), | ||||||||||||
| 271 | ExpectedWidth => $width, | ||||||||||||
| 272 | Line => $line, | ||||||||||||
| 273 | ); | ||||||||||||
| 274 | } | ||||||||||||
| 275 | 0 | splice @arr,$index,1; | |||||||||||
| 276 | 0 | say $fhOut join('|',@arr); | |||||||||||
| 277 | } | ||||||||||||
| 278 | |||||||||||||
| 279 | 0 | $fhIn->close; | |||||||||||
| 280 | 0 | $fhOut->close; | |||||||||||
| 281 | |||||||||||||
| 282 | 0 | $p->rename($tmpFile,$inFile); | |||||||||||
| 283 | |||||||||||||
| 284 | 0 | return; | |||||||||||
| 285 | } | ||||||||||||
| 286 | |||||||||||||
| 287 | # ----------------------------------------------------------------------------- | ||||||||||||
| 288 | |||||||||||||
| 289 | =head1 VERSION | ||||||||||||
| 290 | |||||||||||||
| 291 | 1.240 | ||||||||||||
| 292 | |||||||||||||
| 293 | =head1 AUTHOR | ||||||||||||
| 294 | |||||||||||||
| 295 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||
| 296 | |||||||||||||
| 297 | =head1 COPYRIGHT | ||||||||||||
| 298 | |||||||||||||
| 299 | Copyright (C) 2026 Frank Seitz | ||||||||||||
| 300 | |||||||||||||
| 301 | =head1 LICENSE | ||||||||||||
| 302 | |||||||||||||
| 303 | This code is free software; you can redistribute it and/or modify | ||||||||||||
| 304 | it under the same terms as Perl itself. | ||||||||||||
| 305 | |||||||||||||
| 306 | =cut | ||||||||||||
| 307 | |||||||||||||
| 308 | # ----------------------------------------------------------------------------- | ||||||||||||
| 309 | |||||||||||||
| 310 | 1 | 1; | |||||||||||
| 311 | |||||||||||||
| 312 | # eof | ||||||||||||