| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | package Bio::EnsEMBL::Utils::IO; | ||
| 21 | $Bio::EnsEMBL::Utils::IO::VERSION = '114.0.0_50'; # TRIAL | ||
| 22 | $Bio::EnsEMBL::Utils::IO::VERSION = '114.0.050'; | ||
| 23 | =pod | ||
| 24 | |||
| 25 | |||
| 26 | =head1 CONTACT | ||
| 27 | |||
| 28 | Please email comments or questions to the public Ensembl | ||
| 29 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 30 | |||
| 31 | Questions may also be sent to the Ensembl help desk at | ||
| 32 | <http://www.ensembl.org/Help/Contact>. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | Bio::EnsEMBL::Utils::IO | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | use Bio::EnsEMBL::Utils::IO qw/slurp work_with_file slurp_to_array fh_to_array/; | ||
| 45 | #or | ||
| 46 | # use Bio::EnsEMBL::Utils::IO qw/:slurp/; # brings in any method starting with slurp | ||
| 47 | # use Bio::EnsEMBL::Utils::IO qw/:array/; # brings in any method which ends with _array | ||
| 48 | # use Bio::EnsEMBL::Utils::IO qw/:gz/; # brings all methods which start with gz_ | ||
| 49 | # use Bio::EnsEMBL::Utils::IO qw/:bz/; # brings all methods which start with bz_ | ||
| 50 | # use Bio::EnsEMBL::Utils::IO qw/:zip/; # brings all methods which start with zip_ | ||
| 51 | # use Bio::EnsEMBL::Utils::IO qw/:all/; # brings all methods in | ||
| 52 | |||
| 53 | # As a scalar | ||
| 54 | my $file_contents = slurp('/my/file/location.txt'); | ||
| 55 | print length($file_contents); | ||
| 56 | |||
| 57 | # As a ref | ||
| 58 | my $file_contents_ref = slurp('/my/file/location.txt', 1); | ||
| 59 | print length($$file_contents_ref); | ||
| 60 | |||
| 61 | # Sending it to an array | ||
| 62 | my $array = slurp_to_array('/my/location'); | ||
| 63 | work_with_file('/my/location', 'r', sub { | ||
| 64 | $array = process_to_array($_[0], sub { | ||
| 65 | #Gives us input line by line | ||
| 66 | return "INPUT: $_"; | ||
| 67 | }); | ||
| 68 | }); | ||
| 69 | |||
| 70 | # Simplified vesion but without the post processing | ||
| 71 | $array = fh_to_array($fh); | ||
| 72 | |||
| 73 | # Sending this back out to another file | ||
| 74 | work_with_file('/my/file/newlocation.txt', 'w', sub { | ||
| 75 | my ($fh) = @_; | ||
| 76 | print $fh $$file_contents_ref; | ||
| 77 | return; | ||
| 78 | }); | ||
| 79 | |||
| 80 | # Gzipping the data to another file | ||
| 81 | gz_work_with_file('/my/file.gz', 'w', sub { | ||
| 82 | my ($fh) = @_; | ||
| 83 | print $fh $$file_contents_ref; | ||
| 84 | return; | ||
| 85 | }); | ||
| 86 | |||
| 87 | # Working with a set of lines manually | ||
| 88 | work_with_file('/my/file', 'r', sub { | ||
| 89 | my ($fh) = @_; | ||
| 90 | iterate_lines($fh, sub { | ||
| 91 | my ($line) = @_; | ||
| 92 | print $line; #Send the line in the file back out | ||
| 93 | return; | ||
| 94 | }); | ||
| 95 | return; | ||
| 96 | }); | ||
| 97 | |||
| 98 | # Doing the same in one go | ||
| 99 | iterate_file('/my/file', sub { | ||
| 100 | my ($line) = @_; | ||
| 101 | print $line; #Send the line in the file back out | ||
| 102 | return; | ||
| 103 | }); | ||
| 104 | |||
| 105 | # Move all data from one file handle to another. Bit like a copy | ||
| 106 | move_data($src_fh, $trg_fh); | ||
| 107 | |||
| 108 | =head1 DESCRIPTION | ||
| 109 | |||
| 110 | A collection of subroutines aimed to helping IO based operations | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | See subroutines. | ||
| 115 | |||
| 116 | =head1 MAINTAINER | ||
| 117 | |||
| 118 | $Author$ | ||
| 119 | |||
| 120 | =head1 VERSION | ||
| 121 | |||
| 122 | $Revision$ | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | use strict; | ||
| 127 | use warnings; | ||
| 128 | |||
| 129 | use base qw(Exporter); | ||
| 130 | |||
| 131 | our $GZIP_OK = 0; | ||
| 132 | our $BZIP2_OK = 0; | ||
| 133 | our $ZIP_OK = 0; | ||
| 134 | |||
| 135 | our @EXPORT_OK = qw/slurp slurp_to_array fh_to_array process_to_array work_with_file gz_slurp gz_slurp_to_array gz_work_with_file bz_slurp bz_slurp_to_array bz_work_with_file zip_slurp zip_slurp_to_array zip_work_with_file spurt filter_dir iterate_file iterate_lines move_data/; | ||
| 136 | our %EXPORT_TAGS = ( | ||
| 137 | all => [@EXPORT_OK], | ||
| 138 | slurp => [qw/slurp slurp_to_array gz_slurp gz_slurp_to_array/], | ||
| 139 | spurt => [qw/spurt/], | ||
| 140 | array => [qw/fh_to_array process_to_array slurp_to_array gz_slurp_to_array/], | ||
| 141 | gz => [qw/gz_slurp gz_slurp_to_array gz_work_with_file/], | ||
| 142 | bz => [qw/bz_slurp bz_slurp_to_array bz_work_with_file/], | ||
| 143 | zip => [qw/zip_slurp zip_slurp_to_array zip_work_with_file/], | ||
| 144 | iterate => [qw/iterate_file iterate_lines/], | ||
| 145 | ); | ||
| 146 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 147 | use Bio::EnsEMBL::Utils::Scalar qw(:assert); | ||
| 148 | use IO::File; | ||
| 149 | |||
| 150 | eval { | ||
| 151 | require IO::Compress::Gzip; | ||
| 152 | require IO::Uncompress::Gunzip; | ||
| 153 | $GZIP_OK = 1; | ||
| 154 | }; | ||
| 155 | |||
| 156 | eval { | ||
| 157 | require IO::Compress::Bzip2; | ||
| 158 | require IO::Uncompress::Bunzip2; | ||
| 159 | $BZIP2_OK = 1; | ||
| 160 | }; | ||
| 161 | |||
| 162 | eval { | ||
| 163 | require IO::Compress::Zip; | ||
| 164 | require IO::Uncompress::Unzip; | ||
| 165 | $ZIP_OK = 1; | ||
| 166 | }; | ||
| 167 | |||
| 168 | =head2 slurp() | ||
| 169 | |||
| 170 | Arg [1] : string $file | ||
| 171 | Arg [2] : boolean; $want_ref | ||
| 172 | Arg [3] : boolean; $binary | ||
| 173 | Indicates if we want to return a scalar reference | ||
| 174 | Description : Forces the contents of a file into a scalar. This is the | ||
| 175 | fastest way to get a file into memory in Perl. You can also | ||
| 176 | get a scalar reference back to avoid copying the file contents | ||
| 177 | in Scalar references. If the input file is binary then specify | ||
| 178 | with the binary flag | ||
| 179 | Returntype : Scalar or reference of the file contents depending on arg 2 | ||
| 180 | Example : my $contents = slurp('/tmp/file.txt'); | ||
| 181 | Exceptions : If the file did not exist or was not readable | ||
| 182 | Status : Stable | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub slurp { | ||
| 187 | my ($file, $want_ref, $binary) = @_; | ||
| 188 | my $contents = q{}; | ||
| 189 | work_with_file($file, 'r', sub { | ||
| 190 | my ($fh) = @_; | ||
| 191 | binmode($fh) if $binary; | ||
| 192 | my $size_left = -s $file; | ||
| 193 | while( $size_left > 0 ) { | ||
| 194 | my $read_cnt = sysread($fh, $contents, $size_left, length($contents)); | ||
| 195 | unless( $read_cnt ) { | ||
| 196 | throw "read error in file $file: $!" ; | ||
| 197 | last; | ||
| 198 | } | ||
| 199 | $size_left -= $read_cnt ; | ||
| 200 | } | ||
| 201 | return; | ||
| 202 | }); | ||
| 203 | return ($want_ref) ? \$contents : $contents; | ||
| 204 | } | ||
| 205 | |||
| 206 | =head2 spurt() | ||
| 207 | |||
| 208 | Arg [1] : string $file | ||
| 209 | Arg [2] : string $contents | ||
| 210 | Arg [3] : boolean; $append | ||
| 211 | Arg [4] : boolean; $binary | ||
| 212 | Description : Convenient method to safely open a file and dump some content into it. | ||
| 213 | $append can be set to append to the file instead of resetting it first. | ||
| 214 | $binary can be set if the content you are printing is not plain-text. | ||
| 215 | Returntype : None | ||
| 216 | Example : spurt('/tmp/file.txt', $contents); | ||
| 217 | Exceptions : If the file could not be created or was not writable | ||
| 218 | Status : Stable | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub spurt { | ||
| 223 | my ( $file, $contents, $append, $binary ) = @_; | ||
| 224 | work_with_file( | ||
| 225 | $file, | ||
| 226 | $append ? 'a' : 'w', | ||
| 227 | sub { | ||
| 228 | my ($fh) = @_; | ||
| 229 | binmode($fh) if $binary; | ||
| 230 | syswrite( $fh, $contents ); | ||
| 231 | } ); | ||
| 232 | } | ||
| 233 | |||
| 234 | =head2 gz_slurp | ||
| 235 | |||
| 236 | Arg [1] : string $file | ||
| 237 | Arg [2] : boolean; $want_ref Indicates if we want to return a scalar reference | ||
| 238 | Arg [3] : boolean; $binary | ||
| 239 | Arg [4] : HashRef arguments to pass into IO compression layers | ||
| 240 | Description : Forces the contents of a file into a scalar. This is the | ||
| 241 | fastest way to get a file into memory in Perl. You can also | ||
| 242 | get a scalar reference back to avoid copying the file contents | ||
| 243 | in Scalar references. If the input file is binary then specify | ||
| 244 | with the binary flag | ||
| 245 | Returntype : Scalar or reference of the file contents depending on arg 2 | ||
| 246 | Example : my $contents = slurp('/tmp/file.txt.gz'); | ||
| 247 | Exceptions : If the file did not exist or was not readable | ||
| 248 | Status : Stable | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub gz_slurp { | ||
| 253 | my ($file, $want_ref, $binary, $args) = @_; | ||
| 254 | my $contents; | ||
| 255 | gz_work_with_file($file, 'r', sub { | ||
| 256 | my ($fh) = @_; | ||
| 257 | local $/ = undef; | ||
| 258 | binmode($fh) if $binary; | ||
| 259 | $contents = <$fh>; | ||
| 260 | return; | ||
| 261 | }, $args); | ||
| 262 | return ($want_ref) ? \$contents : $contents; | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 bz_slurp | ||
| 266 | |||
| 267 | Arg [1] : string $file | ||
| 268 | Arg [2] : boolean; $want_ref Indicates if we want to return a scalar reference | ||
| 269 | Arg [3] : boolean; $binary | ||
| 270 | Arg [4] : HashRef arguments to pass into IO compression layers | ||
| 271 | Description : Forces the contents of a file into a scalar. This is the | ||
| 272 | fastest way to get a file into memory in Perl. You can also | ||
| 273 | get a scalar reference back to avoid copying the file contents | ||
| 274 | in Scalar references. If the input file is binary then specify | ||
| 275 | with the binary flag | ||
| 276 | Returntype : Scalar or reference of the file contents depending on arg 2 | ||
| 277 | Example : my $contents = slurp('/tmp/file.txt.bz2'); | ||
| 278 | Exceptions : If the file did not exist or was not readable | ||
| 279 | Status : Stable | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | sub bz_slurp { | ||
| 284 | my ($file, $want_ref, $binary, $args) = @_; | ||
| 285 | my $contents; | ||
| 286 | bz_work_with_file($file, 'r', sub { | ||
| 287 | my ($fh) = @_; | ||
| 288 | local $/ = undef; | ||
| 289 | binmode($fh) if $binary; | ||
| 290 | $contents = <$fh>; | ||
| 291 | return; | ||
| 292 | }, $args); | ||
| 293 | return ($want_ref) ? \$contents : $contents; | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 zip_slurp | ||
| 297 | |||
| 298 | Arg [1] : string $file | ||
| 299 | Arg [2] : boolean; $want_ref Indicates if we want to return a scalar reference | ||
| 300 | Arg [3] : boolean; $binary | ||
| 301 | Arg [4] : HashRef arguments to pass into IO compression layers | ||
| 302 | Description : Forces the contents of a file into a scalar. This is the | ||
| 303 | fastest way to get a file into memory in Perl. You can also | ||
| 304 | get a scalar reference back to avoid copying the file contents | ||
| 305 | in Scalar references. If the input file is binary then specify | ||
| 306 | with the binary flag | ||
| 307 | Returntype : Scalar or reference of the file contents depending on arg 2 | ||
| 308 | Example : my $contents = slurp('/tmp/file.txt.zip'); | ||
| 309 | Exceptions : If the file did not exist or was not readable | ||
| 310 | Status : Stable | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub zip_slurp { | ||
| 315 | my ($file, $want_ref, $binary, $args) = @_; | ||
| 316 | my $contents; | ||
| 317 | zip_work_with_file($file, 'r', sub { | ||
| 318 | my ($fh) = @_; | ||
| 319 | local $/ = undef; | ||
| 320 | binmode($fh) if $binary; | ||
| 321 | $contents = <$fh>; | ||
| 322 | return; | ||
| 323 | }, $args); | ||
| 324 | return ($want_ref) ? \$contents : $contents; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | =head2 slurp_to_array | ||
| 329 | |||
| 330 | Arg [1] : string $file | ||
| 331 | Arg [2] : boolean $chomp | ||
| 332 | Description : Sends the contents of the given file into an ArrayRef | ||
| 333 | Returntype : ArrayRef | ||
| 334 | Example : my $contents_array = slurp_to_array('/tmp/file.txt'); | ||
| 335 | Exceptions : If the file did not exist or was not readable | ||
| 336 | Status : Stable | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub slurp_to_array { | ||
| 341 | my ($file, $chomp) = @_; | ||
| 342 | my $contents; | ||
| 343 | work_with_file($file, 'r', sub { | ||
| 344 | my ($fh) = @_; | ||
| 345 | $contents = fh_to_array($fh, $chomp); | ||
| 346 | return; | ||
| 347 | }); | ||
| 348 | return $contents; | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 gz_slurp_to_array | ||
| 352 | |||
| 353 | Arg [1] : string $file | ||
| 354 | Arg [2] : boolean $chomp | ||
| 355 | Arg [3] : HashRef arguments to pass into IO compression layers | ||
| 356 | Description : Sends the contents of the given gzipped file into an ArrayRef | ||
| 357 | Returntype : ArrayRef | ||
| 358 | Example : my $contents_array = gz_slurp_to_array('/tmp/file.txt.gz'); | ||
| 359 | Exceptions : If the file did not exist or was not readable | ||
| 360 | Status : Stable | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub gz_slurp_to_array { | ||
| 365 | my ($file, $chomp, $args) = @_; | ||
| 366 | my $contents; | ||
| 367 | gz_work_with_file($file, 'r', sub { | ||
| 368 | my ($fh) = @_; | ||
| 369 | $contents = fh_to_array($fh, $chomp); | ||
| 370 | return; | ||
| 371 | }, $args); | ||
| 372 | return $contents; | ||
| 373 | } | ||
| 374 | |||
| 375 | =head2 bz_slurp_to_array | ||
| 376 | |||
| 377 | Arg [1] : string $file | ||
| 378 | Arg [2] : boolean $chomp | ||
| 379 | Arg [3] : HashRef arguments to pass into IO compression layers | ||
| 380 | Description : Sends the contents of the given bzipped file into an ArrayRef | ||
| 381 | Returntype : ArrayRef | ||
| 382 | Example : my $contents_array = bz_slurp_to_array('/tmp/file.txt.bz2'); | ||
| 383 | Exceptions : If the file did not exist or was not readable | ||
| 384 | Status : Stable | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | sub bz_slurp_to_array { | ||
| 389 | my ($file, $chomp, $args) = @_; | ||
| 390 | my $contents; | ||
| 391 | bz_work_with_file($file, 'r', sub { | ||
| 392 | my ($fh) = @_; | ||
| 393 | $contents = fh_to_array($fh, $chomp); | ||
| 394 | return; | ||
| 395 | }, $args); | ||
| 396 | return $contents; | ||
| 397 | } | ||
| 398 | |||
| 399 | =head2 zip_slurp_to_array | ||
| 400 | |||
| 401 | Arg [1] : string $file | ||
| 402 | Arg [2] : boolean $chomp | ||
| 403 | Arg [3] : HashRef arguments to pass into IO compression layers | ||
| 404 | Description : Sends the contents of the given zipped file into an ArrayRef | ||
| 405 | Returntype : ArrayRef | ||
| 406 | Example : my $contents_array = zip_slurp_to_array('/tmp/file.txt.zip'); | ||
| 407 | Exceptions : If the file did not exist or was not readable | ||
| 408 | Status : Stable | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub zip_slurp_to_array { | ||
| 413 | my ($file, $chomp, $args) = @_; | ||
| 414 | my $contents; | ||
| 415 | zip_work_with_file($file, 'r', sub { | ||
| 416 | my ($fh) = @_; | ||
| 417 | $contents = fh_to_array($fh, $chomp); | ||
| 418 | return; | ||
| 419 | }, $args); | ||
| 420 | return $contents; | ||
| 421 | } | ||
| 422 | |||
| 423 | =head2 fh_to_array | ||
| 424 | |||
| 425 | Arg [1] : Glob/IO::Handle $fh | ||
| 426 | Arg [2] : boolean $chomp | ||
| 427 | Description : Sends the contents of the given filehandle into an ArrayRef. | ||
| 428 | Will perform chomp on each line if specified. If you require | ||
| 429 | any more advanced line based processing then see | ||
| 430 | L<process_to_array>. | ||
| 431 | Returntype : ArrayRef | ||
| 432 | Example : my $contents_array = fh_to_array($fh); | ||
| 433 | Exceptions : None | ||
| 434 | Status : Stable | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | sub fh_to_array { | ||
| 439 | my ($fh, $chomp) = @_; | ||
| 440 | if($chomp) { | ||
| 441 | return process_to_array($fh, sub { | ||
| 442 | my ($line) = @_; | ||
| 443 | chomp($line); | ||
| 444 | return $line; | ||
| 445 | }); | ||
| 446 | } | ||
| 447 | my @contents = <$fh>; | ||
| 448 | return \@contents; | ||
| 449 | } | ||
| 450 | |||
| 451 | =head2 process_to_array | ||
| 452 | |||
| 453 | Arg [1] : Glob/IO::Handle $fh | ||
| 454 | Arg [2] : CodeRef $callback | ||
| 455 | Description : Sends the contents of the given file handle into an ArrayRef | ||
| 456 | via the processing callback. Assumes line based input. | ||
| 457 | Returntype : ArrayRef | ||
| 458 | Example : my $array = process_to_array($fh, sub { return "INPUT: $_"; }); | ||
| 459 | Exceptions : If the fh did not exist or if a callback was not given. | ||
| 460 | Status : Stable | ||
| 461 | |||
| 462 | =cut | ||
| 463 | |||
| 464 | sub process_to_array { | ||
| 465 | my ($fh, $callback) = @_; | ||
| 466 | assert_file_handle($fh, 'FileHandle'); | ||
| 467 | assert_ref($callback, 'CODE', 'callback'); | ||
| 468 | my @contents; | ||
| 469 | iterate_lines($fh, sub { | ||
| 470 | my ($line) = @_; | ||
| 471 | push(@contents, $callback->($line)); | ||
| 472 | return; | ||
| 473 | }); | ||
| 474 | return \@contents; | ||
| 475 | } | ||
| 476 | |||
| 477 | =head2 iterate_lines | ||
| 478 | |||
| 479 | Arg [1] : Glob/IO::Handle $fh | ||
| 480 | Arg [2] : CodeRef $callback | ||
| 481 | Description : Iterates through each line from the given file handle and | ||
| 482 | hands them to the callback one by one | ||
| 483 | Returntype : None | ||
| 484 | Example : iterate_lines($fh, sub { print "INPUT: $_"; }); | ||
| 485 | Exceptions : If the fh did not exist or if a callback was not given. | ||
| 486 | Status : Stable | ||
| 487 | |||
| 488 | =cut | ||
| 489 | |||
| 490 | sub iterate_lines { | ||
| 491 | my ($fh, $callback) = @_; | ||
| 492 | assert_file_handle($fh, 'FileHandle'); | ||
| 493 | assert_ref($callback, 'CODE', 'callback'); | ||
| 494 | while( my $line = <$fh> ) { | ||
| 495 | $callback->($line); | ||
| 496 | } | ||
| 497 | return; | ||
| 498 | } | ||
| 499 | |||
| 500 | =head2 iterate_file | ||
| 501 | |||
| 502 | Arg [1] : string $file | ||
| 503 | Arg [3] : CodeRef the callback which is used to iterate the lines in | ||
| 504 | the file | ||
| 505 | Description : Iterates through each line from the given file and | ||
| 506 | hands them to the callback one by one | ||
| 507 | Returntype : None | ||
| 508 | Example : iterate_file('/my/file', sub { print "INPUT: $_"; }); | ||
| 509 | Exceptions : If the file did not exist or if a callback was not given. | ||
| 510 | Status : Stable | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | |||
| 515 | sub iterate_file { | ||
| 516 | my ($file, $callback) = @_; | ||
| 517 | work_with_file($file, 'r', sub { | ||
| 518 | my ($fh) = @_; | ||
| 519 | iterate_lines($fh, $callback); | ||
| 520 | return; | ||
| 521 | }); | ||
| 522 | return; | ||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | =head2 work_with_file | ||
| 528 | |||
| 529 | Arg [1] : string $file | ||
| 530 | Arg [2] : string; $mode | ||
| 531 | Supports all modes specified by the C<open()> function as well as those | ||
| 532 | supported by IO::File | ||
| 533 | Arg [3] : CodeRef the callback which is given the open file handle as | ||
| 534 | its only argument | ||
| 535 | Description : Performs the nitty gritty of checking if a file handle is open | ||
| 536 | and closing the resulting filehandle down. | ||
| 537 | Returntype : None | ||
| 538 | Example : work_with_file('/tmp/out.txt', 'w', sub { | ||
| 539 | my ($fh) = @_; | ||
| 540 | print $fh 'hello'; | ||
| 541 | return; | ||
| 542 | }); | ||
| 543 | Exceptions : If we could not work with the file due to permissions | ||
| 544 | Status : Stable | ||
| 545 | |||
| 546 | =cut | ||
| 547 | |||
| 548 | sub work_with_file { | ||
| 549 | my ($file, $mode, $callback) = @_; | ||
| 550 | throw "We need a file name to open" if ! $file; | ||
| 551 | throw "We need a mode to open the requested file with" if ! $mode; | ||
| 552 | assert_ref($callback, 'CODE', 'callback'); | ||
| 553 | my $fh = IO::File->new($file, $mode) or | ||
| 554 | throw "Cannot open '${file}' in mode '${mode}': $!"; | ||
| 555 | $callback->($fh); | ||
| 556 | close($fh) or throw "Cannot close FH from ${file}: $!"; | ||
| 557 | return; | ||
| 558 | } | ||
| 559 | |||
| 560 | =head2 gz_work_with_file | ||
| 561 | |||
| 562 | Arg [1] : string $file | ||
| 563 | Arg [2] : string; $mode | ||
| 564 | Supports modes like C<r>, C<w>, C<\>> and C<\<> | ||
| 565 | Arg [3] : CodeRef the callback which is given the open file handle as | ||
| 566 | its only argument | ||
| 567 | Arg [4] : HashRef used to pass options into the IO | ||
| 568 | compression/uncompression modules | ||
| 569 | Description : Performs the nitty gritty of checking if a file handle is open | ||
| 570 | and closing the resulting filehandle down. | ||
| 571 | Returntype : None | ||
| 572 | Example : gz_work_with_file('/tmp/out.txt.gz', 'w', sub { | ||
| 573 | my ($fh) = @_; | ||
| 574 | print $fh 'hello'; | ||
| 575 | return; | ||
| 576 | }); | ||
| 577 | Exceptions : If we could not work with the file due to permissions | ||
| 578 | Status : Stable | ||
| 579 | |||
| 580 | =cut | ||
| 581 | |||
| 582 | sub gz_work_with_file { | ||
| 583 | my ($file, $mode, $callback, $args) = @_; | ||
| 584 | throw "IO::Compress was not available" if ! $GZIP_OK; | ||
| 585 | throw "We need a file name to open" if ! $file; | ||
| 586 | throw "We need a mode to open the requested file with" if ! $mode; | ||
| 587 | assert_ref($callback, 'CODE', 'callback'); | ||
| 588 | $args ||= {}; | ||
| 589 | |||
| 590 | my $fh; | ||
| 591 | { | ||
| 592 | no warnings qw/once/; | ||
| 593 | if($mode =~ '>$' || $mode eq 'w') { | ||
| 594 | $args->{Append} = 1 if $mode =~ />>$/; | ||
| 595 | $fh = IO::Compress::Gzip->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Compress::Gzip::GzipError"; | ||
| 596 | } | ||
| 597 | elsif($mode eq '<' || $mode eq 'r') { | ||
| 598 | $fh = IO::Uncompress::Gunzip->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Uncompress::Gunzip::GunzipError"; | ||
| 599 | } | ||
| 600 | else { | ||
| 601 | throw "Could not decipher a mode from '$mode'"; | ||
| 602 | } | ||
| 603 | }; | ||
| 604 | $callback->($fh); | ||
| 605 | close($fh) or throw "Cannot close FH from ${file}: $!"; | ||
| 606 | return; | ||
| 607 | } | ||
| 608 | |||
| 609 | =head2 bz_work_with_file | ||
| 610 | |||
| 611 | Arg [1] : string $file | ||
| 612 | Arg [2] : string; $mode | ||
| 613 | Supports modes like C<r>, C<w>, C<\>> and C<\<> | ||
| 614 | Arg [3] : CodeRef the callback which is given the open file handle as | ||
| 615 | its only argument | ||
| 616 | Arg [4] : HashRef used to pass options into the IO | ||
| 617 | compression/uncompression modules | ||
| 618 | Description : Performs the nitty gritty of checking if a file handle is open | ||
| 619 | and closing the resulting filehandle down. | ||
| 620 | Returntype : None | ||
| 621 | Example : bz_work_with_file('/tmp/out.txt.bz2', 'w', sub { | ||
| 622 | my ($fh) = @_; | ||
| 623 | print $fh 'hello'; | ||
| 624 | return; | ||
| 625 | }); | ||
| 626 | Exceptions : If we could not work with the file due to permissions | ||
| 627 | Status : Stable | ||
| 628 | |||
| 629 | =cut | ||
| 630 | |||
| 631 | sub bz_work_with_file { | ||
| 632 | my ($file, $mode, $callback, $args) = @_; | ||
| 633 | throw "IO::Compress was not available" if ! $BZIP2_OK; | ||
| 634 | throw "We need a file name to open" if ! $file; | ||
| 635 | throw "We need a mode to open the requested file with" if ! $mode; | ||
| 636 | assert_ref($callback, 'CODE', 'callback'); | ||
| 637 | $args ||= {}; | ||
| 638 | |||
| 639 | my $fh; | ||
| 640 | { | ||
| 641 | no warnings qw/once/; | ||
| 642 | if($mode =~ '>$' || $mode eq 'w') { | ||
| 643 | $args->{Append} = 1 if $mode =~ />>$/; | ||
| 644 | $fh = IO::Compress::Bzip2->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Compress::Bzip2::Bzip2Error"; | ||
| 645 | } | ||
| 646 | elsif($mode eq '<' || $mode eq 'r') { | ||
| 647 | $fh = IO::Uncompress::Bunzip2->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Uncompress::Bunzip2::Bunzip2Error"; | ||
| 648 | } | ||
| 649 | else { | ||
| 650 | throw "Could not decipher a mode from '$mode'"; | ||
| 651 | } | ||
| 652 | }; | ||
| 653 | $callback->($fh); | ||
| 654 | close($fh) or throw "Cannot close FH from ${file}: $!"; | ||
| 655 | return; | ||
| 656 | } | ||
| 657 | |||
| 658 | =head2 zip_work_with_file | ||
| 659 | |||
| 660 | Arg [1] : string $file | ||
| 661 | Arg [2] : string; $mode | ||
| 662 | Supports modes like C<r>, C<w>, C<\>> and C<\<> | ||
| 663 | Arg [3] : CodeRef the callback which is given the open file handle as | ||
| 664 | its only argument | ||
| 665 | Arg [4] : HashRef used to pass options into the IO | ||
| 666 | compression/uncompression modules | ||
| 667 | Description : Performs the nitty gritty of checking if a file handle is open | ||
| 668 | and closing the resulting filehandle down. | ||
| 669 | Returntype : None | ||
| 670 | Example : zip_work_with_file('/tmp/out.txt.zip', 'w', sub { | ||
| 671 | my ($fh) = @_; | ||
| 672 | print $fh 'hello'; | ||
| 673 | return; | ||
| 674 | }); | ||
| 675 | Exceptions : If we could not work with the file due to permissions | ||
| 676 | Status : Stable | ||
| 677 | |||
| 678 | =cut | ||
| 679 | |||
| 680 | sub zip_work_with_file { | ||
| 681 | my ($file, $mode, $callback, $args) = @_; | ||
| 682 | throw "IO::Compress was not available" if ! $ZIP_OK; | ||
| 683 | throw "We need a file name to open" if ! $file; | ||
| 684 | throw "We need a mode to open the requested file with" if ! $mode; | ||
| 685 | assert_ref($callback, 'CODE', 'callback'); | ||
| 686 | $args ||= {}; | ||
| 687 | |||
| 688 | my $fh; | ||
| 689 | { | ||
| 690 | no warnings qw/once/; | ||
| 691 | if($mode =~ '>$' || $mode eq 'w') { | ||
| 692 | $args->{Append} = 1 if $mode =~ />>$/; | ||
| 693 | $fh = IO::Compress::Zip->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Compress::Zip::ZipError"; | ||
| 694 | } | ||
| 695 | elsif($mode eq '<' || $mode eq 'r') { | ||
| 696 | $fh = IO::Uncompress::Unzip->new($file, %$args) or throw "Cannot open '$file' for writing: $IO::Uncompress::Unzip::UnzipError"; | ||
| 697 | } | ||
| 698 | else { | ||
| 699 | throw "Could not decipher a mode from '$mode'"; | ||
| 700 | } | ||
| 701 | }; | ||
| 702 | $callback->($fh); | ||
| 703 | close($fh) or throw "Cannot close FH from ${file}: $!"; | ||
| 704 | return; | ||
| 705 | } | ||
| 706 | |||
| 707 | =head2 filter_dir | ||
| 708 | |||
| 709 | Arg [1] : String; directory | ||
| 710 | Arg [2] : CodeRef; the callback which is given a file in the | ||
| 711 | directory as its only argument | ||
| 712 | Description : Return the lexicographically sorted content of a directory. | ||
| 713 | The callback allows to specify the criteria an entry in | ||
| 714 | the directory must satisfy in order to appear in the content. | ||
| 715 | Returntype : Arrayref; list with the filtered files/directory | ||
| 716 | Example : filter_dir('/tmp', sub { | ||
| 717 | my $file = shift; | ||
| 718 | |||
| 719 | # select perl scripts in the directory | ||
| 720 | return $file if $file =~ /\.pl$/; | ||
| 721 | }); | ||
| 722 | Exceptions : If the directory cannot be opened or its handle | ||
| 723 | cannot be closed | ||
| 724 | Status : Stable | ||
| 725 | |||
| 726 | =cut | ||
| 727 | |||
| 728 | sub filter_dir { | ||
| 729 | my ($dir, $callback) = @_; | ||
| 730 | |||
| 731 | assert_ref($callback, 'CODE', 'callback'); | ||
| 732 | |||
| 733 | opendir(my $dh, $dir) or throw "Cannot open directory $dir"; | ||
| 734 | my @files = sort grep { $callback->($_) } readdir($dh); | ||
| 735 | closedir($dh) or throw "Cannot close directory $dir"; | ||
| 736 | |||
| 737 | return \@files; | ||
| 738 | } | ||
| 739 | |||
| 740 | |||
| 741 | =head2 move_data | ||
| 742 | |||
| 743 | Arg [1] : FileHandle $src_fh | ||
| 744 | Arg [2] : FileHandle $trg_fh | ||
| 745 | Arg [3] : int $buffer. Defaults to 8KB | ||
| 746 | Description : Moves data from the given source filehandle to the target one | ||
| 747 | using a 8KB buffer or user specified buffer | ||
| 748 | Returntype : None | ||
| 749 | Example : move_data($src_fh, $trg_fh, 16*1024); # copy in 16KB chunks | ||
| 750 | Exceptions : If inputs were not as expected | ||
| 751 | |||
| 752 | =cut | ||
| 753 | |||
| 754 | sub move_data { | ||
| 755 | my ($src_fh, $trg_fh, $buffer_size) = @_; | ||
| 756 | assert_file_handle($src_fh, 'SourceFileHandle'); | ||
| 757 | assert_file_handle($trg_fh, 'TargetFileHandle'); | ||
| 758 | |||
| 759 | $buffer_size ||= 8192; #Default 8KB | ||
| 760 | my $buffer; | ||
| 761 | while(1) { | ||
| 762 | my $read = sysread($src_fh, $buffer, $buffer_size); | ||
| 763 | if(! defined $read) { | ||
| 764 | throw "Error whilst reading from filehandle: $!"; | ||
| 765 | } | ||
| 766 | if($read == 0) { | ||
| 767 | last; | ||
| 768 | } | ||
| 769 | my $written = syswrite($trg_fh, $buffer); | ||
| 770 | if(!defined $written) { | ||
| 771 | throw "Error whilst writing to filehandle: $!"; | ||
| 772 | } | ||
| 773 | } | ||
| 774 | return; | ||
| 775 | } | ||
| 776 | |||
| 777 | 1; |