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