| 1 | #!/usr/bin/perl -c | ||
| 2 | |||
| 3 | package IO::Moose::Handle; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IO::Moose::Handle - Reimplementation of IO::Handle with improvements | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use IO::Moose::Handle; | ||
| 12 | |||
| 13 | my $fh = IO::Moose::Handle->new; | ||
| 14 | $fh->fdopen( fileno(STDIN) ); | ||
| 15 | print $fh->getline; | ||
| 16 | my $content = $fh->slurp; | ||
| 17 | $fh->close; | ||
| 18 | |||
| 19 | my $fh = IO::Moose::Handle->fdopen( \*STDERR, '>' ); | ||
| 20 | $fh->autoflush(1); | ||
| 21 | $fh->say('Some text'); | ||
| 22 | undef $fh; # calls close at DESTROY | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This class extends L<IO::Handle> with following differences: | ||
| 27 | |||
| 28 | =over | ||
| 29 | |||
| 30 | =item * | ||
| 31 | |||
| 32 | It is based on L<Moose> object framework. | ||
| 33 | |||
| 34 | =item * | ||
| 35 | |||
| 36 | The C<stat> method returns L<File::Stat::Moose> object. | ||
| 37 | |||
| 38 | =item * | ||
| 39 | |||
| 40 | It uses L<Exception::Base> for signaling errors. Most of methods are throwing | ||
| 41 | exception on failure. | ||
| 42 | |||
| 43 | =item * | ||
| 44 | |||
| 45 | The modifiers like C<input_record_separator> are supported on per file handle | ||
| 46 | basis. | ||
| 47 | |||
| 48 | =item * | ||
| 49 | |||
| 50 | It also implements additional methods like C<say>, C<slurp>. | ||
| 51 | |||
| 52 | =back | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | |||
| 57 | use 5.008; | ||
| 58 | |||
| 59 | use strict; | ||
| 60 | use warnings FATAL => 'all'; | ||
| 61 | |||
| 62 | our $VERSION = '0.1004'; | ||
| 63 | |||
| 64 | use Moose; | ||
| 65 | |||
| 66 | |||
| 67 | =head1 INHERITANCE | ||
| 68 | |||
| 69 | =over 2 | ||
| 70 | |||
| 71 | =item * | ||
| 72 | |||
| 73 | extends L<MooseX::GlobRef::Object> | ||
| 74 | |||
| 75 | =over 2 | ||
| 76 | |||
| 77 | =item * | ||
| 78 | |||
| 79 | extends L<Moose::Object> | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =item * | ||
| 84 | |||
| 85 | extends L<IO::Handle> | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | extends 'MooseX::GlobRef::Object', 'IO::Handle'; | ||
| 92 | |||
| 93 | |||
| 94 | =head1 EXCEPTIONS | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | use Exception::Base ( | ||
| 101 | '+ignore_package' => [ __PACKAGE__, qr/^MooseX?::/, qr/^Class::MOP::/ ], | ||
| 102 | ); | ||
| 103 | |||
| 104 | =item L<Exception::Argument> | ||
| 105 | |||
| 106 | Thrown whether method is called with wrong argument. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | use Exception::Argument; | ||
| 111 | |||
| 112 | =item L<Exception::Fatal> | ||
| 113 | |||
| 114 | Thrown whether fatal error is occurred by core function. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | use Exception::Fatal; | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | |||
| 125 | # TRUE and FALSE | ||
| 126 | use constant::boolean; | ||
| 127 | use English '-no_match_vars'; | ||
| 128 | |||
| 129 | use Scalar::Util 'blessed', 'reftype', 'looks_like_number'; | ||
| 130 | use Symbol 'qualify', 'qualify_to_ref'; | ||
| 131 | |||
| 132 | # stat method | ||
| 133 | use File::Stat::Moose; | ||
| 134 | |||
| 135 | |||
| 136 | # EBADF error code. | ||
| 137 | use Errno; | ||
| 138 | |||
| 139 | |||
| 140 | # Assertions | ||
| 141 | use Test::Assert ':assert'; | ||
| 142 | |||
| 143 | # Debugging flag | ||
| 144 | use if $ENV{PERL_DEBUG_IO_MOOSE_HANDLE}, 'Smart::Comments'; | ||
| 145 | |||
| 146 | |||
| 147 | # Additional types | ||
| 148 | use MooseX::Types::OpenModeStr; | ||
| 149 | use MooseX::Types::CanonOpenModeStr; | ||
| 150 | |||
| 151 | |||
| 152 | =head1 ATTRIBUTES | ||
| 153 | |||
| 154 | =over | ||
| 155 | |||
| 156 | =item file : Num|FileHandle|OpenHandle {ro} | ||
| 157 | |||
| 158 | File (file descriptor number, file handle or IO object) as a parameter for new | ||
| 159 | object or argument for C<fdopen> method. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | # File to open (descriptor number or existing file handle) | ||
| 164 | has 'file' => ( | ||
| 165 | is => 'ro', | ||
| 166 | isa => 'Num | FileHandle | OpenHandle', | ||
| 167 | reader => 'file', | ||
| 168 | writer => '_set_file', | ||
| 169 | clearer => '_clear_file', | ||
| 170 | predicate => 'has_file', | ||
| 171 | ); | ||
| 172 | |||
| 173 | =item mode : CanonOpenModeStr {ro} = "<" | ||
| 174 | |||
| 175 | File mode as a parameter for new object or argument for C<fdopen> method. Can | ||
| 176 | be Perl-style (C<E<lt>>, C<E<gt>>, C<E<gt>E<gt>>, etc.) or C-style (C<r>, | ||
| 177 | C<w>, C<a>, etc.) | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | has 'mode' => ( | ||
| 182 | is => 'ro', | ||
| 183 | isa => 'CanonOpenModeStr', | ||
| 184 | lazy => TRUE, | ||
| 185 | default => '<', | ||
| 186 | coerce => TRUE, | ||
| 187 | reader => 'mode', | ||
| 188 | writer => '_set_mode', | ||
| 189 | clearer => '_clear_mode', | ||
| 190 | predicate => 'has_mode', | ||
| 191 | ); | ||
| 192 | |||
| 193 | =item fh : GlobRef {ro} | ||
| 194 | |||
| 195 | File handle used for internal IO operations. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | has 'fh' => ( | ||
| 200 | is => 'ro', | ||
| 201 | isa => 'GlobRef | FileHandle | OpenHandle', | ||
| 202 | reader => 'fh', | ||
| 203 | writer => '_set_fh', | ||
| 204 | ); | ||
| 205 | |||
| 206 | =item autochomp : Bool = false {rw} | ||
| 207 | |||
| 208 | If is true value the input will be auto chomped. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | has 'autochomp' => ( | ||
| 213 | is => 'rw', | ||
| 214 | isa => 'Bool', | ||
| 215 | default => FALSE, | ||
| 216 | ); | ||
| 217 | |||
| 218 | =item tainted : Bool = ${^TAINT} {rw} | ||
| 219 | |||
| 220 | If is false value and tainted mode is enabled the C<untaint> method will be | ||
| 221 | called after C<fdopen>. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | has 'tainted' => ( | ||
| 226 | is => 'ro', | ||
| 227 | isa => 'Bool', | ||
| 228 | default => !! ${^TAINT}, | ||
| 229 | reader => 'tainted', | ||
| 230 | writer => '_set_tainted', | ||
| 231 | ); | ||
| 232 | |||
| 233 | =item blocking : Bool = true {rw} | ||
| 234 | |||
| 235 | If is false value the non-blocking IO will be turned on. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | has 'blocking' => ( | ||
| 240 | is => 'rw', | ||
| 241 | isa => 'Bool', | ||
| 242 | default => TRUE, | ||
| 243 | reader => '_get_blocking', | ||
| 244 | writer => '_set_blocking', | ||
| 245 | ); | ||
| 246 | |||
| 247 | =item copyfh : Bool = false {ro} | ||
| 248 | |||
| 249 | If is true value the file handle will be copy of I<file> argument. If | ||
| 250 | I<file> argument is not a file handle, the L<Exception::Argument> is | ||
| 251 | thrown. | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | has 'copyfh' => ( | ||
| 256 | is => 'ro', | ||
| 257 | isa => 'Bool', | ||
| 258 | default => FALSE, | ||
| 259 | ); | ||
| 260 | |||
| 261 | =item tied : Bool = true {ro} | ||
| 262 | |||
| 263 | By default the object's file handle is tied variable, so it can be used with | ||
| 264 | standard, non-OO interface (C<open>, C<print>, C<getc> functions and | ||
| 265 | C<< <> >> operator). This interface is slow and can be disabled if the OO | ||
| 266 | interface only is used. | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | has 'tied' => ( | ||
| 271 | is => 'ro', | ||
| 272 | isa => 'Bool', | ||
| 273 | default => TRUE, | ||
| 274 | ); | ||
| 275 | |||
| 276 | =item strict_accessors : Bool = false {rw} | ||
| 277 | |||
| 278 | By default the accessors might be avoided for performance reason. This | ||
| 279 | optimization can be disabled if the attribute is set to true value. | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | has 'strict_accessors' => ( | ||
| 284 | is => 'rw', | ||
| 285 | isa => 'Bool', | ||
| 286 | default => FALSE, | ||
| 287 | ); | ||
| 288 | |||
| 289 | =item format_formfeed : Str {rw, var="$^L"} | ||
| 290 | |||
| 291 | =item format_line_break_characters : Str {rw, var="$:"} | ||
| 292 | |||
| 293 | =item input_record_separator : Str {rw, var="$/"} | ||
| 294 | |||
| 295 | =item output_field_separator : Str {rw, var="$,"} | ||
| 296 | |||
| 297 | =item output_record_separator : Str {rw, var="$\"} | ||
| 298 | |||
| 299 | These are attributes assigned with Perl's built-in variables. See L<perlvar> | ||
| 300 | for complete descriptions. The fields have accessors available as per file | ||
| 301 | handle basis if called as C<$io-E<gt>accessor> or as global setting if called | ||
| 302 | as C<IO::Moose::Handle-E<gt>accessor>. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | # IO modifiers per file handle with special accessor | ||
| 307 | { | ||
| 308 | foreach my $attr ( qw{ | ||
| 309 | format_formfeed | ||
| 310 | format_line_break_characters | ||
| 311 | input_record_separator | ||
| 312 | output_field_separator | ||
| 313 | output_record_separator | ||
| 314 | } ) { | ||
| 315 | |||
| 316 | has "$attr" => ( | ||
| 317 | is => 'rw', | ||
| 318 | isa => 'Maybe[Str]', | ||
| 319 | reader => "_get_$attr", | ||
| 320 | writer => "_set_$attr", | ||
| 321 | clearer => "clear_$attr", | ||
| 322 | predicate => "has_$attr", | ||
| 323 | ); | ||
| 324 | |||
| 325 | }; | ||
| 326 | }; | ||
| 327 | |||
| 328 | # Flag if error was occured in IO operation | ||
| 329 | has '_error' => ( | ||
| 330 | isa => 'Bool', | ||
| 331 | default => FALSE, | ||
| 332 | reader => '_get_error', | ||
| 333 | writer => '_set_error', | ||
| 334 | ); | ||
| 335 | |||
| 336 | =back | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | |||
| 341 | use namespace::clean -except => 'meta'; | ||
| 342 | |||
| 343 | |||
| 344 | ## no critic qw(ProhibitOneArgSelect) | ||
| 345 | ## no critic qw(ProhibitBuiltinHomonyms) | ||
| 346 | ## no critic qw(ProhibitCaptureWithoutTest) | ||
| 347 | ## no critic qw(RequireArgUnpacking) | ||
| 348 | ## no critic qw(RequireCheckingReturnValueOfEval) | ||
| 349 | ## no critic qw(RequireLocalizedPunctuationVars) | ||
| 350 | |||
| 351 | =head1 IMPORTS | ||
| 352 | |||
| 353 | =over | ||
| 354 | |||
| 355 | =item use IO::Moose::Handle '$STDIN', '$STDOUT', '$STDERR'; | ||
| 356 | |||
| 357 | =item use IO::Moose::Handle ':std'; | ||
| 358 | |||
| 359 | =item use IO::Moose::Handle ':all'; | ||
| 360 | |||
| 361 | Creates handle as a copy of standard handle and imports it into caller's | ||
| 362 | namespace. This handles won't be created until explicit import. | ||
| 363 | |||
| 364 | use IO::Moose::Handle ':std'; | ||
| 365 | print $STDOUT->autoflush(1); | ||
| 366 | print $STDIN->slurp; | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | # Import standard handles | ||
| 371 | sub import { | ||
| 372 | ### IO::Moose::Handle::import: @_ | ||
| 373 | |||
| 374 | my ($pkg, @args) = @_; | ||
| 375 | |||
| 376 | my %setup = ref $args[0] eq 'HASH' ? %{ shift @args } : (); | ||
| 377 | |||
| 378 | my %vars; | ||
| 379 | foreach my $arg (@args) { | ||
| 380 | if (defined $arg and $arg =~ /^:(all|std)$/) { | ||
| 381 | %vars = map { $_ => 1 } qw{ STDIN STDOUT STDERR }; | ||
| 382 | } | ||
| 383 | elsif (defined $arg and $arg =~ /^\$(STDIN|STDOUT|STDERR)$/) { | ||
| 384 | $vars{$1} = 1; | ||
| 385 | } | ||
| 386 | else { | ||
| 387 | Exception::Argument->throw( | ||
| 388 | message => ['Unknown argument for import: %s', (defined $arg ? $arg : 'undef')], | ||
| 389 | ); | ||
| 390 | }; | ||
| 391 | }; | ||
| 392 | |||
| 393 | my $caller = $setup{into} || caller($setup{into_level} || 0); | ||
| 394 | |||
| 395 | # Standard handles | ||
| 396 | our ($STDIN, $STDOUT, $STDERR); | ||
| 397 | |||
| 398 | foreach my $var (keys %vars) { | ||
| 399 | if ($var eq 'STDIN') { | ||
| 400 | $STDIN = __PACKAGE__->new( file => \*STDIN, mode => '<', copyfh => 1 ) if not defined $STDIN; | ||
| 401 | *{qualify_to_ref("${caller}::STDIN")} = \$STDIN; | ||
| 402 | } | ||
| 403 | elsif ($var eq 'STDOUT') { | ||
| 404 | $STDOUT = __PACKAGE__->new( file => \*STDOUT, mode => '>', copyfh => 1 ) if not defined $STDOUT; | ||
| 405 | *{qualify_to_ref("${caller}::STDOUT")} = \$STDOUT; | ||
| 406 | } | ||
| 407 | elsif ($var eq 'STDERR') { | ||
| 408 | $STDERR = __PACKAGE__->new( file => \*STDERR, mode => '>', copyfh => 1 ) if not defined $STDERR; | ||
| 409 | *{qualify_to_ref("${caller}::STDERR")} = \$STDERR; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | assert_false("Unknown variable \$$var") if ASSERT; | ||
| 413 | }; | ||
| 414 | }; | ||
| 415 | |||
| 416 | return TRUE; | ||
| 417 | }; | ||
| 418 | |||
| 419 | |||
| 420 | =back | ||
| 421 | |||
| 422 | =head1 CONSTRUCTORS | ||
| 423 | |||
| 424 | =over | ||
| 425 | |||
| 426 | =item new( I<args> : Hash ) : Self | ||
| 427 | |||
| 428 | Creates the C<IO::Moose::Handle> object and calls C<fdopen> method if the | ||
| 429 | I<mode> parameter is defined. | ||
| 430 | |||
| 431 | $io = IO::Moose::Handle->new( file => \*STDIN, mode => "r" ); | ||
| 432 | |||
| 433 | The object can be created with unopened file handle which can be opened later. | ||
| 434 | |||
| 435 | $in = IO::Moose::Handle->new( file => \*STDIN ); | ||
| 436 | $in->fdopen("r"); | ||
| 437 | |||
| 438 | If I<copyfh> is true value and I<file> contains a file handle, this file | ||
| 439 | handle is copied rather than new file handle created. | ||
| 440 | |||
| 441 | $tmp = File::Temp->new; | ||
| 442 | $io = IO::Moose::Handle->new( file => $tmp, copyfh => 1, mode => "w" ); | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | # Object initialization | ||
| 447 | sub BUILD { | ||
| 448 | ### IO::Moose::Handle::BUILD: @_ | ||
| 449 | |||
| 450 | my ($self, $params) = @_; | ||
| 451 | |||
| 452 | $self->_init_fh; | ||
| 453 | |||
| 454 | return $self; | ||
| 455 | }; | ||
| 456 | |||
| 457 | |||
| 458 | # Initialize file handle | ||
| 459 | sub _init_fh { | ||
| 460 | ### IO::Moose::Handle::BUILD: @_ | ||
| 461 | |||
| 462 | my ($self) = @_; | ||
| 463 | |||
| 464 | assert_equals('GLOB', reftype $self) if ASSERT; | ||
| 465 | |||
| 466 | my $strict_accessors = $self->strict_accessors; | ||
| 467 | my $tied = $strict_accessors ? $self->tied : *$self->{tied}; | ||
| 468 | my $copyfh = $strict_accessors ? $self->copyfh : *$self->{copyfh}; | ||
| 469 | |||
| 470 | my $fd = $strict_accessors ? $self->file : *$self->{file}; | ||
| 471 | |||
| 472 | # initialize anonymous handle | ||
| 473 | if ($copyfh) { | ||
| 474 | # Copy file handle | ||
| 475 | if (blessed $fd and $fd->isa(__PACKAGE__)) { | ||
| 476 | if ($strict_accessors) { | ||
| 477 | $self->_set_fh( $fd->fh ); | ||
| 478 | } | ||
| 479 | else { | ||
| 480 | *$self->{fh} = $fd->fh; | ||
| 481 | }; | ||
| 482 | if (not $tied) { | ||
| 483 | my $fh = $fd->fh; | ||
| 484 | *$self = *$fh{IO}; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | elsif ((ref $fd || '') eq 'GLOB' or (reftype $fd || '') eq 'GLOB') { | ||
| 488 | if ($self->strict_accessors) { | ||
| 489 | $self->_set_fh( $fd ); | ||
| 490 | } | ||
| 491 | else { | ||
| 492 | *$self->{fh} = $fd; | ||
| 493 | }; | ||
| 494 | if (not $tied) { | ||
| 495 | *$self = *$fd{IO}; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | else { | ||
| 499 | Exception::Argument->throw( | ||
| 500 | message => 'Cannot copy file handle from bad file argument' | ||
| 501 | ); | ||
| 502 | }; | ||
| 503 | } | ||
| 504 | else { | ||
| 505 | # Create the new handle | ||
| 506 | select select my $fh; | ||
| 507 | if ($self->strict_accessors) { | ||
| 508 | $self->_set_fh( $fh ); | ||
| 509 | } | ||
| 510 | else { | ||
| 511 | *$self->{fh} = $fh; | ||
| 512 | }; | ||
| 513 | if (not $tied) { | ||
| 514 | *$self = *$fh{IO}; | ||
| 515 | }; | ||
| 516 | }; | ||
| 517 | |||
| 518 | my $is_opened; | ||
| 519 | |||
| 520 | if (not $copyfh) { | ||
| 521 | $is_opened = eval { $self->_open_file }; | ||
| 522 | if ($EVAL_ERROR) { | ||
| 523 | my $e = Exception::Fatal->catch; | ||
| 524 | $e->throw( message => 'Cannot new' ); | ||
| 525 | }; | ||
| 526 | assert_not_null($is_opened) if ASSERT; | ||
| 527 | }; | ||
| 528 | |||
| 529 | $self->_tie if $tied and not $is_opened; | ||
| 530 | |||
| 531 | return $self; | ||
| 532 | }; | ||
| 533 | |||
| 534 | |||
| 535 | # Open file if is defined | ||
| 536 | sub _open_file { | ||
| 537 | #### IO::Moose::Handle::_open_file: @_ | ||
| 538 | |||
| 539 | my ($self) = @_; | ||
| 540 | |||
| 541 | if ($self->has_file) { | ||
| 542 | # call fdopen if file is defined; it also ties handle | ||
| 543 | $self->fdopen( $self->file, $self->mode ); | ||
| 544 | return TRUE; | ||
| 545 | }; | ||
| 546 | |||
| 547 | return FALSE; | ||
| 548 | }; | ||
| 549 | |||
| 550 | |||
| 551 | # Tie self object | ||
| 552 | sub _tie { | ||
| 553 | ### IO::Moose::Handle::_tie: @_ | ||
| 554 | |||
| 555 | my ($self) = @_; | ||
| 556 | |||
| 557 | assert_equals('GLOB', reftype $self) if ASSERT; | ||
| 558 | assert_true($self->tied) if ASSERT; | ||
| 559 | |||
| 560 | tie *$self, blessed $self, $self; | ||
| 561 | |||
| 562 | assert_not_null(tied *$self) if ASSERT; | ||
| 563 | |||
| 564 | return $self; | ||
| 565 | }; | ||
| 566 | |||
| 567 | |||
| 568 | # Untie self object | ||
| 569 | sub _untie { | ||
| 570 | ### IO::Moose::Handle::_untie: @_ | ||
| 571 | |||
| 572 | my ($self) = @_; | ||
| 573 | |||
| 574 | assert_equals('GLOB', reftype $self) if ASSERT; | ||
| 575 | assert_true($self->tied) if ASSERT; | ||
| 576 | |||
| 577 | untie *$self; | ||
| 578 | |||
| 579 | return $self; | ||
| 580 | }; | ||
| 581 | |||
| 582 | |||
| 583 | # Clone standard handler for tied handle | ||
| 584 | sub _open_tied { | ||
| 585 | ### IO::Moose::Handle::_open_tied: @_ | ||
| 586 | |||
| 587 | my ($self) = @_; | ||
| 588 | |||
| 589 | assert_equals('GLOB', reftype $self) if ASSERT; | ||
| 590 | assert_true($self->tied) if ASSERT; | ||
| 591 | assert_not_null($self->mode) if ASSERT; | ||
| 592 | |||
| 593 | my $mode = $self->mode; | ||
| 594 | |||
| 595 | # clone standard handler for tied handler | ||
| 596 | $self->_untie; | ||
| 597 | eval { | ||
| 598 | CORE::open *$self, "$mode&", $self->fh; | ||
| 599 | }; | ||
| 600 | if ($EVAL_ERROR) { | ||
| 601 | Exception::Fatal->throw( message => 'Cannot fdopen' ); | ||
| 602 | }; | ||
| 603 | $self->_tie; | ||
| 604 | |||
| 605 | return $self; | ||
| 606 | }; | ||
| 607 | |||
| 608 | |||
| 609 | # Close tied handle | ||
| 610 | sub _close_tied { | ||
| 611 | ### IO::Moose::Handle::_close_tied: @_ | ||
| 612 | |||
| 613 | my ($self) = @_; | ||
| 614 | |||
| 615 | assert_equals('GLOB', reftype $self) if ASSERT; | ||
| 616 | assert_true($self->tied) if ASSERT; | ||
| 617 | |||
| 618 | $self->_untie; | ||
| 619 | |||
| 620 | CORE::close *$self; | ||
| 621 | |||
| 622 | $self->_tie; | ||
| 623 | |||
| 624 | return $self; | ||
| 625 | }; | ||
| 626 | |||
| 627 | |||
| 628 | =item new_from_fd( I<fd> : Num|FileHandle|OpenHandle, I<mode> : CanonOpenModeStr = "<") : Self | ||
| 629 | |||
| 630 | Creates the C<IO::Moose::Handle> object and immediately opens the file handle | ||
| 631 | based on arguments. | ||
| 632 | |||
| 633 | $out = IO::Moose::Handle->new_from_fd( \*STDOUT, "w" ); | ||
| 634 | |||
| 635 | =cut | ||
| 636 | |||
| 637 | sub new_from_fd { | ||
| 638 | ### IO::Moose::Handle::new_from_fd: @_ | ||
| 639 | |||
| 640 | my $class = shift; | ||
| 641 | Exception::Argument->throw( | ||
| 642 | message => ['Usage: %s->new_from_fd(FD, [MODE])', __PACKAGE__], | ||
| 643 | ) if @_ < 1 or @_ > 2; | ||
| 644 | |||
| 645 | my ($fd, $mode) = @_; | ||
| 646 | |||
| 647 | my $io = eval { | ||
| 648 | $class->new( | ||
| 649 | file => $fd, | ||
| 650 | defined $mode ? (mode => $mode) : () | ||
| 651 | ) | ||
| 652 | }; | ||
| 653 | if ($EVAL_ERROR) { | ||
| 654 | my $e = Exception::Fatal->catch; | ||
| 655 | $e->throw( message => 'Cannot new_from_fd' ); | ||
| 656 | }; | ||
| 657 | assert_isa(__PACKAGE__, $io) if ASSERT; | ||
| 658 | |||
| 659 | return $io; | ||
| 660 | }; | ||
| 661 | |||
| 662 | |||
| 663 | =back | ||
| 664 | |||
| 665 | =head1 METHODS | ||
| 666 | |||
| 667 | =over | ||
| 668 | |||
| 669 | =item fdopen( I<fd> : Num|FileHandle|OpenHandle, I<mode> : CanonOpenModeStr = "<" ) : Self | ||
| 670 | |||
| 671 | Opens the previously created file handle. If the file was already opened, it | ||
| 672 | is closed automatically and reopened without resetting its line counter. The | ||
| 673 | method also sets the C<file> and C<mode> attributes. | ||
| 674 | |||
| 675 | $out = IO::Moose::Handle->new; | ||
| 676 | $out->fdopen( \*STDOUT, "w" ); | ||
| 677 | |||
| 678 | $dup = IO::Moose::Handle->new; | ||
| 679 | $dup->fdopen( $dup, "a" ); | ||
| 680 | |||
| 681 | $stdin = IO::Moose::Handle->new; | ||
| 682 | $stdin->fdopen( 0, "r"); | ||
| 683 | |||
| 684 | =cut | ||
| 685 | |||
| 686 | sub fdopen { | ||
| 687 | ### IO::Moose::Handle::fdopen: @_ | ||
| 688 | |||
| 689 | my $self = shift; | ||
| 690 | Exception::Argument->throw( | ||
| 691 | message => 'Usage: $io->fdopen(FD, [MODE])', | ||
| 692 | ) if not blessed $self or @_ < 1 or @_ > 2 or not defined $_[0]; | ||
| 693 | |||
| 694 | my ($fd, $mode) = @_; | ||
| 695 | |||
| 696 | my $status; | ||
| 697 | eval { | ||
| 698 | # check constraints and fill attributes | ||
| 699 | $fd = $self->_set_file($fd); | ||
| 700 | $mode = defined $mode ? $self->_set_mode($mode) : do { $self->_clear_mode; $self->mode }; | ||
| 701 | |||
| 702 | assert_not_null($fd) if ASSERT; | ||
| 703 | assert_not_null($mode) if ASSERT; | ||
| 704 | |||
| 705 | if (blessed $fd and $fd->isa(__PACKAGE__)) { | ||
| 706 | #### fdopen: "open(fh, $mode&, \$fd->fh)" | ||
| 707 | $status = CORE::open $self->fh, "$mode&", $fd->fh; | ||
| 708 | } | ||
| 709 | elsif ((ref $fd || '') eq 'GLOB') { | ||
| 710 | #### fdopen: "open(fh, $mode&, \\$$fd)" | ||
| 711 | $status = CORE::open $self->fh, "$mode&", $fd; | ||
| 712 | } | ||
| 713 | elsif ((reftype $fd || '') eq 'GLOB') { | ||
| 714 | #### fdopen: "open(fh, $mode&, *$fd)" | ||
| 715 | $status = CORE::open $self->fh, "$mode&", *$fd; | ||
| 716 | } | ||
| 717 | elsif ($fd =~ /^\d+$/) { | ||
| 718 | #### fdopen: "open(fh, $mode&=$fd)" | ||
| 719 | $status = CORE::open $self->fh, "$mode&=$fd"; | ||
| 720 | } | ||
| 721 | else { | ||
| 722 | # should be caught by constraint | ||
| 723 | assert_false("Bad file descriptor"); | ||
| 724 | }; | ||
| 725 | }; | ||
| 726 | if (not $status) { | ||
| 727 | $self->_set_error(TRUE); | ||
| 728 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 729 | $e->throw( message => 'Cannot fdopen' ); | ||
| 730 | }; | ||
| 731 | assert_true($status) if ASSERT; | ||
| 732 | |||
| 733 | $self->_set_error(FALSE); | ||
| 734 | |||
| 735 | $self->_open_tied if $self->tied; | ||
| 736 | |||
| 737 | if (${^TAINT} and not $self->tainted) { | ||
| 738 | $self->untaint; | ||
| 739 | }; | ||
| 740 | |||
| 741 | if (${^TAINT} and not $self->_get_blocking) { | ||
| 742 | $self->blocking(FALSE); | ||
| 743 | }; | ||
| 744 | |||
| 745 | return $self; | ||
| 746 | }; | ||
| 747 | |||
| 748 | |||
| 749 | =item close(I<>) : Self | ||
| 750 | |||
| 751 | Closes the opened file handle. The C<file> and C<mode> attributes are cleared | ||
| 752 | after closing. | ||
| 753 | |||
| 754 | =cut | ||
| 755 | |||
| 756 | sub close { | ||
| 757 | ### IO::Moose::Handle::close: @_ | ||
| 758 | |||
| 759 | my $self = shift; | ||
| 760 | |||
| 761 | Exception::Argument->throw( | ||
| 762 | message => 'Usage: $io->close()' | ||
| 763 | ) if not blessed $self or @_ > 0; | ||
| 764 | |||
| 765 | if (not CORE::close $self->fh) { | ||
| 766 | $self->_set_error(TRUE); | ||
| 767 | Exception::IO->throw( message => 'Cannot close' ); | ||
| 768 | }; | ||
| 769 | |||
| 770 | $self->_set_error(FALSE); | ||
| 771 | |||
| 772 | # clear file and mode attributes | ||
| 773 | $self->_clear_file; | ||
| 774 | $self->_clear_mode; | ||
| 775 | |||
| 776 | $self->_close_tied if $self->tied; | ||
| 777 | |||
| 778 | return $self; | ||
| 779 | }; | ||
| 780 | |||
| 781 | |||
| 782 | =item eof(I<>) : Bool | ||
| 783 | |||
| 784 | =cut | ||
| 785 | |||
| 786 | sub eof { | ||
| 787 | ### IO::Moose::Handle::eof: @_ | ||
| 788 | |||
| 789 | my $self = shift; | ||
| 790 | |||
| 791 | Exception::Argument->throw( | ||
| 792 | message => 'Usage: $io->eof()' | ||
| 793 | ) if not blessed $self or @_ > 0; | ||
| 794 | |||
| 795 | my $status; | ||
| 796 | eval { | ||
| 797 | $status = CORE::eof $self->fh; | ||
| 798 | }; | ||
| 799 | if ($EVAL_ERROR) { | ||
| 800 | my $e = Exception::Fatal->catch; | ||
| 801 | $e->throw( message => 'Cannot eof' ); | ||
| 802 | }; | ||
| 803 | return $status; | ||
| 804 | }; | ||
| 805 | |||
| 806 | |||
| 807 | =item fileno(I<>) : Int | ||
| 808 | |||
| 809 | =cut | ||
| 810 | |||
| 811 | sub fileno { | ||
| 812 | ### IO::Moose::Handle::fileno: @_ | ||
| 813 | |||
| 814 | my $self = shift; | ||
| 815 | |||
| 816 | Exception::Argument->throw( | ||
| 817 | message => 'Usage: $io->fileno()' | ||
| 818 | ) if not blessed $self or @_ > 0; | ||
| 819 | |||
| 820 | my $fileno = CORE::fileno $self->fh; | ||
| 821 | if (not defined $fileno) { | ||
| 822 | local $! = Errno::EBADF; | ||
| 823 | Exception::IO->throw( message => 'Cannot fileno' ); | ||
| 824 | }; | ||
| 825 | |||
| 826 | return $fileno; | ||
| 827 | }; | ||
| 828 | |||
| 829 | |||
| 830 | =item opened(I<>) : Bool | ||
| 831 | |||
| 832 | =cut | ||
| 833 | |||
| 834 | sub opened { | ||
| 835 | ### IO::Moose::Handle::opened: @_ | ||
| 836 | |||
| 837 | my $self = shift; | ||
| 838 | |||
| 839 | Exception::Argument->throw( | ||
| 840 | message => 'Usage: $io->opened()' | ||
| 841 | ) if not blessed $self or @_ > 0; | ||
| 842 | |||
| 843 | my $fileno; | ||
| 844 | eval { | ||
| 845 | $fileno = CORE::fileno $self->fh; | ||
| 846 | }; | ||
| 847 | |||
| 848 | return defined $fileno; | ||
| 849 | }; | ||
| 850 | |||
| 851 | |||
| 852 | =item print( I<args> : Array ) : Self | ||
| 853 | |||
| 854 | =cut | ||
| 855 | |||
| 856 | sub print { | ||
| 857 | ### IO::Moose::Handle::print: @_ | ||
| 858 | |||
| 859 | my $self = shift; | ||
| 860 | |||
| 861 | Exception::Argument->throw( | ||
| 862 | message => 'Usage: $io->print(ARGS)' | ||
| 863 | ) if not ref $self; | ||
| 864 | |||
| 865 | my $status; | ||
| 866 | eval { | ||
| 867 | # IO modifiers based on object's attributes | ||
| 868 | local $OUTPUT_FIELD_SEPARATOR | ||
| 869 | = $self->has_output_field_separator | ||
| 870 | ? $self->_get_output_field_separator | ||
| 871 | : $OUTPUT_FIELD_SEPARATOR; | ||
| 872 | local $OUTPUT_RECORD_SEPARATOR | ||
| 873 | = $self->has_output_record_separator | ||
| 874 | ? $self->_get_output_record_separator | ||
| 875 | : $OUTPUT_RECORD_SEPARATOR; | ||
| 876 | |||
| 877 | { | ||
| 878 | # IO modifiers based on tied fh modifiers | ||
| 879 | my $oldfh = select *$self; | ||
| 880 | my $var = $|; | ||
| 881 | select $self->fh; | ||
| 882 | $| = $var; | ||
| 883 | select $oldfh; | ||
| 884 | }; | ||
| 885 | |||
| 886 | $status = CORE::print { $self->fh } @_; | ||
| 887 | }; | ||
| 888 | if (not $status) { | ||
| 889 | $self->_set_error(TRUE); | ||
| 890 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 891 | $e->throw( message => 'Cannot print' ); | ||
| 892 | }; | ||
| 893 | assert_true($status) if ASSERT; | ||
| 894 | |||
| 895 | return $self; | ||
| 896 | }; | ||
| 897 | |||
| 898 | |||
| 899 | =item printf( I<fmt> : Str = "", I<args> : Array = (I<>) ) : Self | ||
| 900 | |||
| 901 | =cut | ||
| 902 | |||
| 903 | sub printf { | ||
| 904 | ### IO::Moose::Handle::printf: @_ | ||
| 905 | |||
| 906 | my $self = shift; | ||
| 907 | |||
| 908 | Exception::Argument->throw( | ||
| 909 | message => 'Usage: $io->printf(FMT, [ARGS])' | ||
| 910 | ) if not ref $self; | ||
| 911 | |||
| 912 | { | ||
| 913 | # IO modifiers based on tied fh modifiers | ||
| 914 | my $oldfh = select *$self; | ||
| 915 | my $var = $|; | ||
| 916 | select $self->fh; | ||
| 917 | $| = $var; | ||
| 918 | select $oldfh; | ||
| 919 | }; | ||
| 920 | |||
| 921 | my $status; | ||
| 922 | eval { | ||
| 923 | $status = CORE::printf { $self->fh } @_; | ||
| 924 | }; | ||
| 925 | if (not $status) { | ||
| 926 | $self->_set_error(TRUE); | ||
| 927 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 928 | $e->throw( message => 'Cannot printf' ); | ||
| 929 | }; | ||
| 930 | assert_true($status) if ASSERT; | ||
| 931 | |||
| 932 | return $self; | ||
| 933 | }; | ||
| 934 | |||
| 935 | |||
| 936 | =item sysread( out I<buf>, I<len> : Int, I<offset> : Int = 0 ) : Int | ||
| 937 | |||
| 938 | =cut | ||
| 939 | |||
| 940 | sub sysread { | ||
| 941 | ### IO::Moose::Handle::sysread: @_ | ||
| 942 | |||
| 943 | my $self = shift; | ||
| 944 | |||
| 945 | Exception::Argument->throw( | ||
| 946 | message => 'Usage: $io->sysread(BUF, LEN [, OFFSET])' | ||
| 947 | ) if not ref $self or @_ < 2 or @_ > 3; | ||
| 948 | |||
| 949 | my $bytes; | ||
| 950 | eval { | ||
| 951 | $bytes = CORE::sysread($self->fh, $_[0], $_[1], $_[2] || 0); | ||
| 952 | }; | ||
| 953 | if (not defined $bytes) { | ||
| 954 | $self->_set_error(TRUE); | ||
| 955 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 956 | $e->throw( message => 'Cannot sysread' ); | ||
| 957 | }; | ||
| 958 | assert_not_null($bytes) if ASSERT; | ||
| 959 | return $bytes; | ||
| 960 | }; | ||
| 961 | |||
| 962 | |||
| 963 | =item syswrite( I<buf> : Str, I<len> : Int, I<offset> : Int = 0 ) : Int | ||
| 964 | |||
| 965 | =cut | ||
| 966 | |||
| 967 | sub syswrite { | ||
| 968 | ### IO::Moose::Handle::syswrite: @_ | ||
| 969 | |||
| 970 | my $self = shift; | ||
| 971 | |||
| 972 | Exception::Argument->throw( | ||
| 973 | message => 'Usage: $io->syswrite(BUF [, LEN [, OFFSET]])' | ||
| 974 | ) if not ref $self or @_ < 1 or @_ > 3; | ||
| 975 | |||
| 976 | my $bytes; | ||
| 977 | eval { | ||
| 978 | if (defined($_[1])) { | ||
| 979 | $bytes = CORE::syswrite($self->fh, $_[0], $_[1], $_[2] || 0); | ||
| 980 | } | ||
| 981 | else { | ||
| 982 | $bytes = CORE::syswrite($self->fh, $_[0]); | ||
| 983 | }; | ||
| 984 | }; | ||
| 985 | if (not defined $bytes) { | ||
| 986 | $self->_set_error(TRUE); | ||
| 987 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 988 | $e->throw( message => 'Cannot syswrite' ); | ||
| 989 | }; | ||
| 990 | assert_not_null($bytes) if ASSERT; | ||
| 991 | return $bytes; | ||
| 992 | }; | ||
| 993 | |||
| 994 | |||
| 995 | =item getc(I<>) : Char | ||
| 996 | |||
| 997 | =cut | ||
| 998 | |||
| 999 | sub getc { | ||
| 1000 | ### IO::Moose::Handle::getc: @_ | ||
| 1001 | |||
| 1002 | my $self = shift; | ||
| 1003 | |||
| 1004 | Exception::Argument->throw( | ||
| 1005 | message => 'Usage: $io->getc()' | ||
| 1006 | ) if not blessed $self or @_ > 0; | ||
| 1007 | |||
| 1008 | my $strict_accessors = $self->strict_accessors; | ||
| 1009 | |||
| 1010 | my $hashref; | ||
| 1011 | $hashref = \%{*$self} if not $strict_accessors; | ||
| 1012 | |||
| 1013 | undef $!; | ||
| 1014 | my $char; | ||
| 1015 | eval { | ||
| 1016 | $char = CORE::getc $self->fh; | ||
| 1017 | }; | ||
| 1018 | if ($EVAL_ERROR or (not defined $char and $! and $! != Errno::EBADF)) { | ||
| 1019 | $self->_set_error(TRUE); | ||
| 1020 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1021 | $e->throw( message => 'Cannot getc' ); | ||
| 1022 | assert_false("Should throw an exception ealier") if ASSERT; | ||
| 1023 | }; | ||
| 1024 | |||
| 1025 | if (${^TAINT} and defined $char and not ($strict_accessors ? $self->tainted : $hashref->{tainted})) { | ||
| 1026 | # Bug on Ubuntu intrepid: does not work: $char =~ /(.*)/; $char = $1 | ||
| 1027 | ord($char) =~ /(\d+)/; | ||
| 1028 | my $c = chr($1); | ||
| 1029 | $char = $c; | ||
| 1030 | }; | ||
| 1031 | |||
| 1032 | return $char; | ||
| 1033 | }; | ||
| 1034 | |||
| 1035 | |||
| 1036 | =item read( out I<buf>, I<len> : Int, I<offset> : Int = 0 ) : Int | ||
| 1037 | |||
| 1038 | =cut | ||
| 1039 | |||
| 1040 | sub read { | ||
| 1041 | ### IO::Moose::Handle::read: @_ | ||
| 1042 | |||
| 1043 | my $self = shift; | ||
| 1044 | |||
| 1045 | Exception::Argument->throw( | ||
| 1046 | message => 'Usage: $io->read(BUF, LEN [, OFFSET])' | ||
| 1047 | ) if not ref $self or @_ < 2 or @_ > 3; | ||
| 1048 | |||
| 1049 | my $bytes; | ||
| 1050 | eval { | ||
| 1051 | $bytes = CORE::read($self->fh, $_[0], $_[1], $_[2] || 0); | ||
| 1052 | }; | ||
| 1053 | if (not defined $bytes) { | ||
| 1054 | $self->_set_error(TRUE); | ||
| 1055 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1056 | $e->throw( message => 'Cannot read' ); | ||
| 1057 | }; | ||
| 1058 | assert_not_null($bytes) if ASSERT; | ||
| 1059 | |||
| 1060 | return $bytes; | ||
| 1061 | }; | ||
| 1062 | |||
| 1063 | |||
| 1064 | =item truncate( I<len> : Int ) : Self | ||
| 1065 | |||
| 1066 | These are front ends for corresponding built-in functions. Most of them | ||
| 1067 | throws exception on failure which can be caught with try/catch: | ||
| 1068 | |||
| 1069 | use Exception::Base; | ||
| 1070 | eval { | ||
| 1071 | open $f, "/etc/hostname"; | ||
| 1072 | $io = IO::Moose::Handle->new( file => $f, mode => "r" ); | ||
| 1073 | $c = $io->getc; | ||
| 1074 | }; | ||
| 1075 | if ($@) { | ||
| 1076 | my $e = Exception::Base->catch) { | ||
| 1077 | warn "problem with /etc/hostname file: $e"; | ||
| 1078 | }; | ||
| 1079 | |||
| 1080 | The C<fdopen>, C<close>, C<print>, C<printf> and C<truncate> methods returns | ||
| 1081 | this object. | ||
| 1082 | |||
| 1083 | =cut | ||
| 1084 | |||
| 1085 | sub truncate { | ||
| 1086 | ### IO::Moose::Handle::truncate: @_ | ||
| 1087 | |||
| 1088 | my $self = shift; | ||
| 1089 | |||
| 1090 | Exception::Argument->throw( | ||
| 1091 | message => 'Usage: $io->truncate(LEN)' | ||
| 1092 | ) if not ref $self or @_ != 1 or not looks_like_number $_[0]; | ||
| 1093 | |||
| 1094 | my $status; | ||
| 1095 | eval { | ||
| 1096 | $status = CORE::truncate($self->fh, $_[0]); | ||
| 1097 | }; | ||
| 1098 | if ($EVAL_ERROR or not $status) { | ||
| 1099 | $self->_set_error(TRUE); | ||
| 1100 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1101 | $e->throw( message => 'Cannot truncate' ); | ||
| 1102 | }; | ||
| 1103 | assert_true($status) if ASSERT; | ||
| 1104 | |||
| 1105 | return $self; | ||
| 1106 | }; | ||
| 1107 | |||
| 1108 | |||
| 1109 | =item write( I<buf> : Str, I<len> : Int, I<offset> : Int = 0 ) : Int | ||
| 1110 | |||
| 1111 | The opposite of B<read>. The wrapper for the perl L<perlfunc/write> function is called | ||
| 1112 | C<format_write>. | ||
| 1113 | |||
| 1114 | =cut | ||
| 1115 | |||
| 1116 | sub write { | ||
| 1117 | ### IO::Moose::Handle::write: @_ | ||
| 1118 | |||
| 1119 | my $self = shift; | ||
| 1120 | |||
| 1121 | Exception::Argument->throw( | ||
| 1122 | message => 'Usage: $io->write(BUF [, LEN [, OFFSET]])' | ||
| 1123 | ) if not blessed $self or @_ > 3 or @_ < 1; | ||
| 1124 | |||
| 1125 | my ($buf, $len, $offset) = @_; | ||
| 1126 | |||
| 1127 | my $bytes; | ||
| 1128 | my $status; | ||
| 1129 | eval { | ||
| 1130 | # clean IO modifiers | ||
| 1131 | local $OUTPUT_RECORD_SEPARATOR = ''; | ||
| 1132 | |||
| 1133 | { | ||
| 1134 | # IO modifiers based on tied fh modifiers | ||
| 1135 | my $oldfh = select *$self; | ||
| 1136 | my $var = $OUTPUT_AUTOFLUSH; | ||
| 1137 | select $self->fh; | ||
| 1138 | $OUTPUT_AUTOFLUSH = $var; | ||
| 1139 | select $oldfh; | ||
| 1140 | }; | ||
| 1141 | |||
| 1142 | my $output = substr($buf, $offset || 0, defined $len ? $len : length($buf)); | ||
| 1143 | $bytes = length($output); | ||
| 1144 | $status = CORE::print { $self->fh } $output; | ||
| 1145 | }; | ||
| 1146 | if (not $status) { | ||
| 1147 | $self->_set_error(TRUE); | ||
| 1148 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1149 | $e->throw( message => 'Cannot write' ); | ||
| 1150 | }; | ||
| 1151 | assert_true($status) if ASSERT; | ||
| 1152 | assert_not_null($bytes) if ASSERT; | ||
| 1153 | |||
| 1154 | return $bytes; | ||
| 1155 | }; | ||
| 1156 | |||
| 1157 | |||
| 1158 | =item format_write( I<format_name> : Str ) : Self | ||
| 1159 | |||
| 1160 | The wrapper for perl L<perlfunc/format> function. | ||
| 1161 | |||
| 1162 | =cut | ||
| 1163 | |||
| 1164 | sub format_write { | ||
| 1165 | ### IO::Moose::Handle::format_write: @_ | ||
| 1166 | |||
| 1167 | my $self = shift; | ||
| 1168 | |||
| 1169 | Exception::Argument->throw( | ||
| 1170 | message => 'Usage: $io->format_write([FORMAT_NAME])' | ||
| 1171 | ) if not blessed $self or @_ > 1; | ||
| 1172 | |||
| 1173 | my ($fmt) = @_; | ||
| 1174 | |||
| 1175 | my $e; | ||
| 1176 | my $status; | ||
| 1177 | { | ||
| 1178 | my ($oldfmt, $oldtopfmt); | ||
| 1179 | |||
| 1180 | # New format in argument | ||
| 1181 | if (defined $fmt) { | ||
| 1182 | $oldfmt = $self->format_name(qualify($fmt, caller)); | ||
| 1183 | $oldtopfmt = $self->format_top_name(qualify($fmt . '_TOP', caller)); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | # IO modifiers based on object's attributes | ||
| 1187 | my @vars_obj = ($FORMAT_LINE_BREAK_CHARACTERS, $FORMAT_FORMFEED); | ||
| 1188 | |||
| 1189 | # Global variables without local scope | ||
| 1190 | $FORMAT_LINE_BREAK_CHARACTERS | ||
| 1191 | = $self->has_format_line_break_characters | ||
| 1192 | ? $self->_get_format_line_break_characters | ||
| 1193 | : $FORMAT_LINE_BREAK_CHARACTERS; | ||
| 1194 | $FORMAT_FORMFEED | ||
| 1195 | = $self->has_format_formfeed | ||
| 1196 | ? $self->_get_format_formfeed | ||
| 1197 | : $FORMAT_FORMFEED; | ||
| 1198 | |||
| 1199 | # IO modifiers based on tied fh modifiers | ||
| 1200 | { | ||
| 1201 | my $oldfh = select *$self; | ||
| 1202 | my @vars_tied = ( | ||
| 1203 | $OUTPUT_AUTOFLUSH, $FORMAT_PAGE_NUMBER, | ||
| 1204 | $FORMAT_LINES_PER_PAGE, $FORMAT_LINES_LEFT, $FORMAT_NAME, | ||
| 1205 | $FORMAT_TOP_NAME, $INPUT_LINE_NUMBER, | ||
| 1206 | ); | ||
| 1207 | select $self->fh; | ||
| 1208 | ( | ||
| 1209 | $OUTPUT_AUTOFLUSH, $FORMAT_PAGE_NUMBER, | ||
| 1210 | $FORMAT_LINES_PER_PAGE, $FORMAT_LINES_LEFT, $FORMAT_NAME, | ||
| 1211 | $FORMAT_TOP_NAME, $INPUT_LINE_NUMBER, | ||
| 1212 | ) = @vars_tied; | ||
| 1213 | select $oldfh; | ||
| 1214 | }; | ||
| 1215 | |||
| 1216 | eval { | ||
| 1217 | $status = CORE::write $self->fh; | ||
| 1218 | }; | ||
| 1219 | $e = Exception::Fatal->catch; | ||
| 1220 | |||
| 1221 | # Restore previous settings | ||
| 1222 | ($FORMAT_LINE_BREAK_CHARACTERS, $FORMAT_FORMFEED) = @vars_obj; | ||
| 1223 | if (defined $fmt) { | ||
| 1224 | $self->format_name($oldfmt); | ||
| 1225 | $self->format_top_name($oldtopfmt); | ||
| 1226 | }; | ||
| 1227 | }; | ||
| 1228 | if (not $status) { | ||
| 1229 | $self->_set_error(TRUE); | ||
| 1230 | $e = Exception::IO->new unless $e; | ||
| 1231 | $e->throw( message => 'Cannot format_write' ); | ||
| 1232 | }; | ||
| 1233 | assert_true($status) if ASSERT; | ||
| 1234 | |||
| 1235 | return $self; | ||
| 1236 | }; | ||
| 1237 | |||
| 1238 | |||
| 1239 | =item readline(I<>) : Maybe[Str|Array] | ||
| 1240 | |||
| 1241 | =cut | ||
| 1242 | |||
| 1243 | # TODO POD | ||
| 1244 | sub readline { | ||
| 1245 | ### IO::Moose::Handle::readline: @_ | ||
| 1246 | |||
| 1247 | my ($self) = @_; | ||
| 1248 | |||
| 1249 | Exception::Argument->throw( | ||
| 1250 | message => 'Usage: $io->readline()' | ||
| 1251 | ) if not ref $self or @_ > 1; | ||
| 1252 | |||
| 1253 | my ($status, @lines, $line); | ||
| 1254 | my $strict_accessors = $self->strict_accessors; | ||
| 1255 | |||
| 1256 | my $hashref; | ||
| 1257 | $hashref = \%{*$self} if not $strict_accessors; | ||
| 1258 | |||
| 1259 | my $wantarray = wantarray; | ||
| 1260 | |||
| 1261 | undef $!; | ||
| 1262 | eval { | ||
| 1263 | # IO modifiers based on object's attributes | ||
| 1264 | local $INPUT_RECORD_SEPARATOR | ||
| 1265 | = ($strict_accessors ? $self->has_input_record_separator : exists $hashref->{input_record_separator}) | ||
| 1266 | ? ($strict_accessors ? $self->_get_input_record_separator : $hashref->{input_record_separator}) | ||
| 1267 | : $INPUT_RECORD_SEPARATOR; | ||
| 1268 | |||
| 1269 | # scalar or array context | ||
| 1270 | if ($wantarray) { | ||
| 1271 | $status = scalar(@lines = CORE::readline ($strict_accessors ? $self->fh : $hashref->{fh})); | ||
| 1272 | chomp @lines if ($strict_accessors ? $self->autochomp : $hashref->{autochomp}); | ||
| 1273 | } | ||
| 1274 | else { | ||
| 1275 | $status = defined($line = CORE::readline ($strict_accessors ? $self->fh : $hashref->{fh})); | ||
| 1276 | chomp $line if $strict_accessors ? $self->autochomp : $hashref->{autochomp}; | ||
| 1277 | }; | ||
| 1278 | }; | ||
| 1279 | if ($EVAL_ERROR or (not $status and $!)) { | ||
| 1280 | $self->_set_error(TRUE); | ||
| 1281 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1282 | $e->throw( message => 'Cannot readline' ); | ||
| 1283 | }; | ||
| 1284 | |||
| 1285 | return $wantarray ? @lines : $line; | ||
| 1286 | }; | ||
| 1287 | |||
| 1288 | |||
| 1289 | =item getline(I<>) : Str | ||
| 1290 | |||
| 1291 | The C<readline> method which is called always in scalar context. | ||
| 1292 | |||
| 1293 | $io = IO::Moose::Handle->new( file=>\*STDIN, mode=>"r" ); | ||
| 1294 | push @a, $io->getline; # reads only one line | ||
| 1295 | |||
| 1296 | =cut | ||
| 1297 | |||
| 1298 | sub getline { | ||
| 1299 | ### IO::Moose::Handle::getline: @_ | ||
| 1300 | |||
| 1301 | my $self = shift; | ||
| 1302 | |||
| 1303 | my $line; | ||
| 1304 | eval { | ||
| 1305 | $line = $self->readline(@_); | ||
| 1306 | }; | ||
| 1307 | if ($EVAL_ERROR) { | ||
| 1308 | my $e = Exception::Fatal->catch; | ||
| 1309 | if ($e->isa('Exception::Argument')) { | ||
| 1310 | $e->throw( message => 'Usage: $io->getline()' ); | ||
| 1311 | } | ||
| 1312 | else { | ||
| 1313 | $e->throw( message => 'Cannot getline' ); | ||
| 1314 | }; | ||
| 1315 | assert_false("Should throw an exception ealier") if ASSERT; | ||
| 1316 | }; | ||
| 1317 | |||
| 1318 | return $line; | ||
| 1319 | }; | ||
| 1320 | |||
| 1321 | |||
| 1322 | =item getlines(I<>) : Array | ||
| 1323 | |||
| 1324 | The C<readline> method which is called always in array context. | ||
| 1325 | |||
| 1326 | $io = IO::Moose::Handle->new( file => \*STDIN, mode => "r" ); | ||
| 1327 | print scalar $io->getlines; # error: can't call in scalar context. | ||
| 1328 | |||
| 1329 | =cut | ||
| 1330 | |||
| 1331 | sub getlines { | ||
| 1332 | ### IO::Moose::Handle::getlines: @_ | ||
| 1333 | |||
| 1334 | my $self = shift; | ||
| 1335 | |||
| 1336 | Exception::Argument->throw( | ||
| 1337 | message => 'Cannot call $io->getlines in a scalar context, use $io->getline' | ||
| 1338 | ) if not wantarray; | ||
| 1339 | |||
| 1340 | my @lines; | ||
| 1341 | eval { | ||
| 1342 | @lines = $self->readline(@_); | ||
| 1343 | }; | ||
| 1344 | if ($EVAL_ERROR) { | ||
| 1345 | my $e = Exception::Fatal->catch; | ||
| 1346 | if ($e->isa('Exception::Argument')) { | ||
| 1347 | $e->throw( message => 'Usage: $io->getlines()' ); | ||
| 1348 | } | ||
| 1349 | else { | ||
| 1350 | $e->throw( message => 'Cannot getlines' ); | ||
| 1351 | }; | ||
| 1352 | assert_false("Should throw an exception ealier") if ASSERT; | ||
| 1353 | }; | ||
| 1354 | |||
| 1355 | return @lines; | ||
| 1356 | }; | ||
| 1357 | |||
| 1358 | |||
| 1359 | =item ungetc( I<ord> : Int ) : Self | ||
| 1360 | |||
| 1361 | Pushes a character with the given ordinal value back onto the given handle's | ||
| 1362 | input stream. | ||
| 1363 | |||
| 1364 | $io = IO::Moose::Handle->new( file => \*STDIN, mode => "r" ); | ||
| 1365 | $io->ungetc(ord('A')); | ||
| 1366 | print $io->getc; # prints A | ||
| 1367 | |||
| 1368 | =cut | ||
| 1369 | |||
| 1370 | sub ungetc { | ||
| 1371 | ### IO::Moose::Handle::ungetc: @_ | ||
| 1372 | |||
| 1373 | my $self = shift; | ||
| 1374 | |||
| 1375 | Exception::Argument->throw( | ||
| 1376 | message => 'Usage: $io->ungetc(ORD)' | ||
| 1377 | ) if not blessed $self or @_ != 1 or not looks_like_number $_[0]; | ||
| 1378 | |||
| 1379 | my ($ord) = @_; | ||
| 1380 | |||
| 1381 | eval { | ||
| 1382 | IO::Handle::ungetc( $self->fh, $ord ); | ||
| 1383 | }; | ||
| 1384 | if ($EVAL_ERROR) { | ||
| 1385 | my $e = Exception::Fatal->catch; | ||
| 1386 | $e->throw( message => 'Cannot ungetc' ); | ||
| 1387 | }; | ||
| 1388 | |||
| 1389 | return $self; | ||
| 1390 | }; | ||
| 1391 | |||
| 1392 | |||
| 1393 | =item say( I<args> : Array ) : Self | ||
| 1394 | |||
| 1395 | The C<print> method with EOL character at the end. | ||
| 1396 | |||
| 1397 | $io = IO::Moose::Handle->new( file => \*STDOUT, mode => "w" ); | ||
| 1398 | $io->say("Hello!"); | ||
| 1399 | |||
| 1400 | =cut | ||
| 1401 | |||
| 1402 | sub say { | ||
| 1403 | ### IO::Moose::Handle::say: @_ | ||
| 1404 | |||
| 1405 | my $self = shift; | ||
| 1406 | |||
| 1407 | eval { | ||
| 1408 | $self->print(@_, "\n"); | ||
| 1409 | }; | ||
| 1410 | if ($EVAL_ERROR) { | ||
| 1411 | my $e = Exception::Fatal->catch; | ||
| 1412 | if ($e->isa('Exception::Argument')) { | ||
| 1413 | $e->throw( message => 'Usage: $io->say(ARGS)' ); | ||
| 1414 | } | ||
| 1415 | else { | ||
| 1416 | $e->throw( message => 'Cannot say' ); | ||
| 1417 | }; | ||
| 1418 | }; | ||
| 1419 | |||
| 1420 | return $self; | ||
| 1421 | }; | ||
| 1422 | |||
| 1423 | |||
| 1424 | =item IO::Moose::Handle->slurp( I<file> : Num|FileHandle|OpenHandle, I<args> : Hash ) : Str|Array | ||
| 1425 | |||
| 1426 | Creates the C<IO::Moose::Handle> object and returns its content as a scalar in | ||
| 1427 | scalar context or as an array in array context. | ||
| 1428 | |||
| 1429 | open $f, "/etc/passwd"; | ||
| 1430 | $passwd_file = IO::Moose::Handle->slurp($f); | ||
| 1431 | |||
| 1432 | Additional I<args> are passed to C<IO::Moose::Handle> constructor. | ||
| 1433 | |||
| 1434 | =item slurp(I<>) : Str|Array | ||
| 1435 | |||
| 1436 | Reads whole file and returns its content as a scalar in scalar context or as | ||
| 1437 | an array in array context (like C<getlines> method). | ||
| 1438 | |||
| 1439 | open $f, "/etc/passwd"; | ||
| 1440 | |||
| 1441 | $io1 = IO::Moose::Handle->new( file => $f, mode => "r" ); | ||
| 1442 | $passwd_file = $io1->slurp; | ||
| 1443 | |||
| 1444 | $io2 = IO::Moose::Handle->new( file => $f, mode => "r" ); | ||
| 1445 | $io2->autochomp(1); | ||
| 1446 | @passwd_lines = $io2->slurp; | ||
| 1447 | |||
| 1448 | =cut | ||
| 1449 | |||
| 1450 | sub slurp { | ||
| 1451 | ### IO::Moose::Handle::slurp: @_ | ||
| 1452 | |||
| 1453 | my $self = shift; | ||
| 1454 | my $class = ref $self || $self || __PACKAGE__; | ||
| 1455 | my %args = @_; | ||
| 1456 | |||
| 1457 | Exception::Argument->throw( | ||
| 1458 | message => ['Usage: $io->slurp() or %s->slurp(file=>FILE)', __PACKAGE__], | ||
| 1459 | ) if not blessed $self and not defined $args{file} or blessed $self and @_ > 0; | ||
| 1460 | |||
| 1461 | if (not blessed $self) { | ||
| 1462 | $self = eval { $self->new( %args ) }; | ||
| 1463 | if ($EVAL_ERROR) { | ||
| 1464 | my $e = Exception::Fatal->catch; | ||
| 1465 | $e->throw( message => 'Cannot slurp' ); | ||
| 1466 | }; | ||
| 1467 | assert_isa(__PACKAGE__, $self) if ASSERT; | ||
| 1468 | }; | ||
| 1469 | |||
| 1470 | my (@lines, $string); | ||
| 1471 | my $wantarray = wantarray; | ||
| 1472 | |||
| 1473 | my $old_separator = $self->_get_input_record_separator; | ||
| 1474 | my $old_autochomp = $self->autochomp; | ||
| 1475 | |||
| 1476 | undef $!; | ||
| 1477 | eval { | ||
| 1478 | # scalar or array context | ||
| 1479 | if ($wantarray) { | ||
| 1480 | $self->_set_input_record_separator("\n"); | ||
| 1481 | @lines = $self->readline; | ||
| 1482 | } | ||
| 1483 | else { | ||
| 1484 | $self->_set_input_record_separator(undef); | ||
| 1485 | $self->autochomp(FALSE); | ||
| 1486 | $string = $self->readline; | ||
| 1487 | }; | ||
| 1488 | }; | ||
| 1489 | my $e = Exception::Fatal->catch; | ||
| 1490 | |||
| 1491 | $self->_set_input_record_separator($old_separator); | ||
| 1492 | $self->autochomp($old_autochomp); | ||
| 1493 | |||
| 1494 | if ($e) { | ||
| 1495 | $e->throw( message => 'Cannot slurp' ); | ||
| 1496 | }; | ||
| 1497 | |||
| 1498 | return $wantarray ? @lines : $string; | ||
| 1499 | }; | ||
| 1500 | |||
| 1501 | |||
| 1502 | =item stat(I<>) : File::Stat::Moose | ||
| 1503 | |||
| 1504 | Returns C<File::Stat::Moose> object which represents status of file pointed by | ||
| 1505 | current file handle. | ||
| 1506 | |||
| 1507 | open $f, "/etc/passwd"; | ||
| 1508 | $io = IO::Moose::Handle->new( file => $f, mode => "r" ); | ||
| 1509 | $st = $io->stat; | ||
| 1510 | print $st->size; # size of /etc/passwd file | ||
| 1511 | |||
| 1512 | =cut | ||
| 1513 | |||
| 1514 | sub stat { | ||
| 1515 | ### IO::Moose::Handle::stat: @_ | ||
| 1516 | |||
| 1517 | my $self = shift; | ||
| 1518 | |||
| 1519 | Exception::Argument->throw( | ||
| 1520 | message => 'Usage: $io->stat()' | ||
| 1521 | ) if not ref $self or @_ > 0; | ||
| 1522 | |||
| 1523 | my $stat; | ||
| 1524 | eval { | ||
| 1525 | $stat = File::Stat::Moose->new( file => $self->fh ); | ||
| 1526 | }; | ||
| 1527 | if ($EVAL_ERROR) { | ||
| 1528 | my $e = Exception::Fatal->catch; | ||
| 1529 | $self->_set_error(TRUE); | ||
| 1530 | $e->throw( message => 'Cannot stat' ); | ||
| 1531 | }; | ||
| 1532 | assert_isa('File::Stat::Moose', $stat) if ASSERT; | ||
| 1533 | |||
| 1534 | return $stat; | ||
| 1535 | }; | ||
| 1536 | |||
| 1537 | |||
| 1538 | =item error(I<>) : Bool | ||
| 1539 | |||
| 1540 | Returns true value if the file handle has experienced any errors since it was | ||
| 1541 | opened or since the last call to C<clearerr>, or if the handle is invalid. | ||
| 1542 | |||
| 1543 | It is recommended to use exceptions mechanism to handle errors. | ||
| 1544 | |||
| 1545 | =cut | ||
| 1546 | |||
| 1547 | sub error { | ||
| 1548 | ### IO::Moose::Handle::error: @_ | ||
| 1549 | |||
| 1550 | my $self = shift; | ||
| 1551 | |||
| 1552 | Exception::Argument->throw( | ||
| 1553 | message => 'Usage: $io->error()' | ||
| 1554 | ) if not blessed $self or @_ > 0; | ||
| 1555 | |||
| 1556 | return $self->_get_error || ! defined CORE::fileno $self->fh; | ||
| 1557 | }; | ||
| 1558 | |||
| 1559 | |||
| 1560 | =item clearerr(I<>) : Bool | ||
| 1561 | |||
| 1562 | Clear the given handle's error indicator. Returns true value if the file | ||
| 1563 | handle is valid or false value otherwise. | ||
| 1564 | |||
| 1565 | =cut | ||
| 1566 | |||
| 1567 | sub clearerr { | ||
| 1568 | ### IO::Moose::Handle::clearerr: @_ | ||
| 1569 | |||
| 1570 | my $self = shift; | ||
| 1571 | |||
| 1572 | Exception::Argument->throw( | ||
| 1573 | message => 'Usage: $io->clearerr()' | ||
| 1574 | ) if not blessed $self or @_ > 0; | ||
| 1575 | |||
| 1576 | $self->_set_error(FALSE); | ||
| 1577 | return defined CORE::fileno $self->fh; | ||
| 1578 | }; | ||
| 1579 | |||
| 1580 | |||
| 1581 | =item sync(I<>) : Self | ||
| 1582 | |||
| 1583 | Synchronizes a file's in-memory state with that on the physical medium. It | ||
| 1584 | operates on file descriptor and it is low-level operation. Returns this | ||
| 1585 | object on success or throws an exception. | ||
| 1586 | |||
| 1587 | =cut | ||
| 1588 | |||
| 1589 | sub sync { | ||
| 1590 | ### IO::Moose::Handle::sync: @_ | ||
| 1591 | |||
| 1592 | my $self = shift; | ||
| 1593 | |||
| 1594 | Exception::Argument->throw( | ||
| 1595 | message => 'Usage: $io->sync()' | ||
| 1596 | ) if not blessed $self or @_ > 0; | ||
| 1597 | |||
| 1598 | my $status; | ||
| 1599 | eval { | ||
| 1600 | $status = IO::Handle::sync($self->fh); | ||
| 1601 | }; | ||
| 1602 | if ($EVAL_ERROR or not defined $status) { | ||
| 1603 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1604 | $self->_set_error(TRUE); | ||
| 1605 | $e->throw( message => 'Cannot sync' ); | ||
| 1606 | }; | ||
| 1607 | assert_not_null($status) if ASSERT; | ||
| 1608 | |||
| 1609 | return $self; | ||
| 1610 | }; | ||
| 1611 | |||
| 1612 | |||
| 1613 | =item flush(I<>) : Self | ||
| 1614 | |||
| 1615 | Flushes any buffered data at the perlio API level. Returns self object on | ||
| 1616 | success or throws an exception. | ||
| 1617 | |||
| 1618 | =cut | ||
| 1619 | |||
| 1620 | sub flush { | ||
| 1621 | ### IO::Moose::Handle::flush: @_ | ||
| 1622 | |||
| 1623 | my $self = shift; | ||
| 1624 | |||
| 1625 | Exception::Argument->throw( | ||
| 1626 | message => 'Usage: $io->flush()' | ||
| 1627 | ) if not blessed $self or @_ > 0; | ||
| 1628 | |||
| 1629 | my $oldfh = select $self->fh; | ||
| 1630 | my @var = ($OUTPUT_AUTOFLUSH, $OUTPUT_RECORD_SEPARATOR); | ||
| 1631 | $OUTPUT_AUTOFLUSH = 1; | ||
| 1632 | $OUTPUT_RECORD_SEPARATOR = undef; | ||
| 1633 | |||
| 1634 | my $e; | ||
| 1635 | my $status; | ||
| 1636 | eval { | ||
| 1637 | $status = CORE::print { $self->fh } ''; | ||
| 1638 | }; | ||
| 1639 | if ($EVAL_ERROR) { | ||
| 1640 | $e = Exception::Fatal->catch; | ||
| 1641 | }; | ||
| 1642 | |||
| 1643 | ($OUTPUT_AUTOFLUSH, $OUTPUT_RECORD_SEPARATOR) = @var; | ||
| 1644 | select $oldfh; | ||
| 1645 | |||
| 1646 | if ($e) { | ||
| 1647 | $self->_set_error(TRUE); | ||
| 1648 | $e->throw( message => 'Cannot flush' ); | ||
| 1649 | }; | ||
| 1650 | assert_null($e) if ASSERT; | ||
| 1651 | |||
| 1652 | return $self; | ||
| 1653 | }; | ||
| 1654 | |||
| 1655 | |||
| 1656 | =item printflush( I<args> : Array ) : Self | ||
| 1657 | |||
| 1658 | Turns on autoflush, print I<args> and then restores the autoflush status. | ||
| 1659 | Returns self object on success or throws an exception. | ||
| 1660 | |||
| 1661 | =cut | ||
| 1662 | |||
| 1663 | sub printflush { | ||
| 1664 | ### IO::Moose::Handle::printflush: @_ | ||
| 1665 | |||
| 1666 | my $self = shift; | ||
| 1667 | |||
| 1668 | if (blessed $self) { | ||
| 1669 | my $oldfh = select *$self; | ||
| 1670 | my $var = $OUTPUT_AUTOFLUSH; | ||
| 1671 | $OUTPUT_AUTOFLUSH = 1; | ||
| 1672 | |||
| 1673 | my $e; | ||
| 1674 | my $status; | ||
| 1675 | eval { | ||
| 1676 | $status = $self->print(@_); | ||
| 1677 | }; | ||
| 1678 | if ($EVAL_ERROR) { | ||
| 1679 | $e = Exception::Fatal->catch; | ||
| 1680 | }; | ||
| 1681 | |||
| 1682 | $OUTPUT_AUTOFLUSH = $var; | ||
| 1683 | select $oldfh; | ||
| 1684 | |||
| 1685 | if ($e) { | ||
| 1686 | $e->throw( message => 'Cannot printflush' ); | ||
| 1687 | }; | ||
| 1688 | |||
| 1689 | return $status; | ||
| 1690 | } | ||
| 1691 | else { | ||
| 1692 | local $OUTPUT_AUTOFLUSH = 1; | ||
| 1693 | return CORE::print @_; | ||
| 1694 | }; | ||
| 1695 | }; | ||
| 1696 | |||
| 1697 | |||
| 1698 | =item blocking(I<>) : Bool | ||
| 1699 | |||
| 1700 | =item blocking( I<bool> : Bool ) : Bool | ||
| 1701 | |||
| 1702 | If called with an argument blocking will turn on non-blocking IO if I<bool> is | ||
| 1703 | false, and turn it off if I<bool> is true. C<blocking> will return the value | ||
| 1704 | of the previous setting, or the current setting if I<bool> is not given. | ||
| 1705 | |||
| 1706 | =cut | ||
| 1707 | |||
| 1708 | sub blocking { | ||
| 1709 | ### IO::Moose::Handle::blocking: @_ | ||
| 1710 | |||
| 1711 | my $self = shift; | ||
| 1712 | |||
| 1713 | Exception::Argument->throw( | ||
| 1714 | message => 'Usage: $io->blocking([BOOL])' | ||
| 1715 | ) if not blessed $self or @_ > 1; | ||
| 1716 | |||
| 1717 | # constraint checking | ||
| 1718 | my $old_blocking = $self->_get_blocking; | ||
| 1719 | eval { | ||
| 1720 | $self->_set_blocking($_[0]); | ||
| 1721 | }; | ||
| 1722 | Exception::Fatal->catch->throw( | ||
| 1723 | message => 'Cannot blocking' | ||
| 1724 | ) if $EVAL_ERROR; | ||
| 1725 | |||
| 1726 | my $status; | ||
| 1727 | eval { | ||
| 1728 | if (defined $_[0]) { | ||
| 1729 | $status = IO::Handle::blocking($self->fh, $_[0]); | ||
| 1730 | } | ||
| 1731 | else { | ||
| 1732 | $status = IO::Handle::blocking($self->fh); | ||
| 1733 | }; | ||
| 1734 | }; | ||
| 1735 | if ($EVAL_ERROR or not defined $status) { | ||
| 1736 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1737 | $self->_set_error(TRUE); | ||
| 1738 | $self->_set_blocking($old_blocking); | ||
| 1739 | $e->throw( message => 'Cannot blocking' ); | ||
| 1740 | }; | ||
| 1741 | assert_not_null($status) if ASSERT; | ||
| 1742 | |||
| 1743 | return $status; | ||
| 1744 | }; | ||
| 1745 | |||
| 1746 | |||
| 1747 | =item untaint(I<>) : Self {rw} | ||
| 1748 | |||
| 1749 | Marks the object as taint-clean, and as such data read from it will also be | ||
| 1750 | considered taint-clean. It has meaning only if Perl is running in tainted | ||
| 1751 | mode (C<-T>). | ||
| 1752 | |||
| 1753 | =cut | ||
| 1754 | |||
| 1755 | sub untaint { | ||
| 1756 | ### IO::Moose::Handle::untaint: @_ | ||
| 1757 | |||
| 1758 | my $self = shift; | ||
| 1759 | |||
| 1760 | Exception::Argument->throw( | ||
| 1761 | message => 'Usage: $io->untaint()' | ||
| 1762 | ) if not blessed $self or @_ > 0; | ||
| 1763 | |||
| 1764 | my $status; | ||
| 1765 | eval { | ||
| 1766 | $status = IO::Handle::untaint($self->fh); | ||
| 1767 | }; | ||
| 1768 | if ($EVAL_ERROR or not defined $status or $status != 0) { | ||
| 1769 | my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new; | ||
| 1770 | $self->_set_error(TRUE); | ||
| 1771 | $e->throw( message => 'Cannot untaint' ); | ||
| 1772 | }; | ||
| 1773 | assert_equals(0, $status) if ASSERT; | ||
| 1774 | |||
| 1775 | $self->_set_tainted(FALSE); | ||
| 1776 | |||
| 1777 | return $self; | ||
| 1778 | }; | ||
| 1779 | |||
| 1780 | |||
| 1781 | # Clean up on destroy | ||
| 1782 | sub DEMOLISH { | ||
| 1783 | ### IO::Moose::Handle::DESTROY: @_ | ||
| 1784 | |||
| 1785 | my ($self) = @_; | ||
| 1786 | |||
| 1787 | local $@ = ''; | ||
| 1788 | eval { | ||
| 1789 | $self->_untie; | ||
| 1790 | }; | ||
| 1791 | |||
| 1792 | return $self; | ||
| 1793 | }; | ||
| 1794 | |||
| 1795 | |||
| 1796 | # Tie hook by proxy class | ||
| 1797 | sub TIEHANDLE { | ||
| 1798 | ### IO::Moose::Handle::TIEHANDLE: @_ | ||
| 1799 | |||
| 1800 | my ($class, $self) = @_; | ||
| 1801 | |||
| 1802 | return $self; | ||
| 1803 | }; | ||
| 1804 | |||
| 1805 | |||
| 1806 | # Called on untie. | ||
| 1807 | sub UNTIE { | ||
| 1808 | ### IO::Moose::Handle::UNTIE: @_ | ||
| 1809 | }; | ||
| 1810 | |||
| 1811 | |||
| 1812 | =item format_lines_left(I<>) : Str {var="$-"} | ||
| 1813 | |||
| 1814 | =item format_lines_left( I<value> : Str ) : Str {var="$-"} | ||
| 1815 | |||
| 1816 | =item format_lines_per_page(I<>) : Str {var="$="} | ||
| 1817 | |||
| 1818 | =item format_lines_per_page( I<value> : Str ) : Str {var="$="} | ||
| 1819 | |||
| 1820 | =item format_page_number(I<>) : Str {var="$%"} | ||
| 1821 | |||
| 1822 | =item format_page_number( I<value> : Str ) : Str {var="$%"} | ||
| 1823 | |||
| 1824 | =item input_line_number(I<>) : Str {var="$."} | ||
| 1825 | |||
| 1826 | =item input_line_number( I<value> : Str ) : Str {var="$."} | ||
| 1827 | |||
| 1828 | =item output_autoflush(I<>) : Str {var="$|"} | ||
| 1829 | |||
| 1830 | =item output_autoflush( I<value> : Str ) : Str {var="$|"} | ||
| 1831 | |||
| 1832 | =item autoflush(I<>) : Str {var="$|"} | ||
| 1833 | |||
| 1834 | =item autoflush( I<value> : Str ) : Str {var="$|"} | ||
| 1835 | |||
| 1836 | =item format_name(I<>) : Str {var="$~"} | ||
| 1837 | |||
| 1838 | =item format_name( I<value> : Str ) : Str {var="$~"} | ||
| 1839 | |||
| 1840 | =item format_top_name(I<>) : Str {var="$^"} | ||
| 1841 | |||
| 1842 | =item format_top_name( I<value> : Str ) : Str {var="$^"} | ||
| 1843 | |||
| 1844 | These are accessors assigned with Perl's built-in variables. See L<perlvar> | ||
| 1845 | for complete descriptions. | ||
| 1846 | |||
| 1847 | =cut | ||
| 1848 | |||
| 1849 | { | ||
| 1850 | # Generate accessors for IO modifiers (global and local) | ||
| 1851 | my @standard_accessors = ( | ||
| 1852 | 'format_formfeed', # $^L | ||
| 1853 | 'format_line_break_characters', # $: | ||
| 1854 | 'input_record_separator', # $/ | ||
| 1855 | 'output_field_separator', # $, | ||
| 1856 | 'output_record_separator', # $\ | ||
| 1857 | ); | ||
| 1858 | foreach my $func (@standard_accessors) { | ||
| 1859 | my $var = qualify_to_ref(uc($func)); | ||
| 1860 | __PACKAGE__->meta->add_method( $func => sub { | ||
| 1861 | ### IO::Moose::Handle::$func: $func, @_ | ||
| 1862 | my $self = shift; | ||
| 1863 | Exception::Argument->throw( | ||
| 1864 | message => ['Usage: $io->%2$s([EXPR]) or %1$s->%2$s([EXPR])', __PACKAGE__, $func], | ||
| 1865 | ) if @_ > 1; | ||
| 1866 | if (ref $self) { | ||
| 1867 | my $prev = do { \%{*$self} }->{$func}; | ||
| 1868 | if (@_ > 0) { | ||
| 1869 | do { \%{*$self} }->{$func} = shift; | ||
| 1870 | }; | ||
| 1871 | return $prev; | ||
| 1872 | } | ||
| 1873 | else { | ||
| 1874 | my $prev = ${*$var}; | ||
| 1875 | if (@_ > 0) { | ||
| 1876 | ${*$var} = shift; | ||
| 1877 | }; | ||
| 1878 | return $prev; | ||
| 1879 | }; | ||
| 1880 | } ); | ||
| 1881 | }; | ||
| 1882 | }; | ||
| 1883 | |||
| 1884 | { | ||
| 1885 | # Generate accessors for IO modifiers (output modifiers which require select) | ||
| 1886 | my @output_accessors = ( | ||
| 1887 | 'format_lines_left', # $- | ||
| 1888 | 'format_lines_per_page', # $= | ||
| 1889 | 'format_page_number', # $% | ||
| 1890 | 'input_line_number', # $. | ||
| 1891 | 'output_autoflush', # $| | ||
| 1892 | ); | ||
| 1893 | foreach my $func (@output_accessors) { | ||
| 1894 | my $var = qualify_to_ref(uc($func)); | ||
| 1895 | __PACKAGE__->meta->add_method( $func => sub { | ||
| 1896 | ### IO::Moose::Handle::$func: $func, @_ | ||
| 1897 | my $self = shift; | ||
| 1898 | Exception::Argument->throw( | ||
| 1899 | message => ['Usage: $io->%2$s([EXPR]) or %1$s->%2$s([EXPR])', __PACKAGE__, $func], | ||
| 1900 | ) if @_ > 1; | ||
| 1901 | if (ref $self) { | ||
| 1902 | my $oldfh = select *$self; | ||
| 1903 | my $prev = ${*$var}; | ||
| 1904 | if (@_ > 0) { | ||
| 1905 | ${*$var} = shift; | ||
| 1906 | }; | ||
| 1907 | select $oldfh; | ||
| 1908 | return $prev; | ||
| 1909 | } | ||
| 1910 | else { | ||
| 1911 | my $prev = ${*$var}; | ||
| 1912 | if (@_ > 0) { | ||
| 1913 | ${*$var} = shift; | ||
| 1914 | }; | ||
| 1915 | return $prev; | ||
| 1916 | }; | ||
| 1917 | } ); | ||
| 1918 | }; | ||
| 1919 | }; | ||
| 1920 | |||
| 1921 | { | ||
| 1922 | # Generate accessors for IO modifiers (qualified format name) | ||
| 1923 | my @format_name_accessors = ( | ||
| 1924 | 'format_name', # $~ | ||
| 1925 | 'format_top_name', # $^ | ||
| 1926 | ); | ||
| 1927 | foreach my $func (@format_name_accessors) { | ||
| 1928 | my $var = qualify_to_ref(uc($func)); | ||
| 1929 | __PACKAGE__->meta->add_method( $func => sub { | ||
| 1930 | ### IO::Moose::Handle::$func: $func, @_ | ||
| 1931 | my $self = shift; | ||
| 1932 | Exception::Argument->throw( | ||
| 1933 | message => ['Usage: $io->%2$s([EXPR]) or %1$s->%2$s([EXPR])', __PACKAGE__, $func], | ||
| 1934 | ) if @_ > 1; | ||
| 1935 | if (ref $self) { | ||
| 1936 | my $oldfh = select *$self; | ||
| 1937 | my $prev = ${*$var}; | ||
| 1938 | if (@_ > 0) { | ||
| 1939 | my $value = shift; | ||
| 1940 | ${*$var} = defined $value ? qualify($value, caller) : undef; | ||
| 1941 | }; | ||
| 1942 | select $oldfh; | ||
| 1943 | return $prev; | ||
| 1944 | } | ||
| 1945 | else { | ||
| 1946 | my $prev = ${*$var}; | ||
| 1947 | my $value = shift; | ||
| 1948 | ${*$var} = defined $value ? qualify($value, caller) : undef; | ||
| 1949 | return $prev; | ||
| 1950 | }; | ||
| 1951 | } ); | ||
| 1952 | }; | ||
| 1953 | }; | ||
| 1954 | |||
| 1955 | # Aliasing accessor | ||
| 1956 | __PACKAGE__->meta->add_method( | ||
| 1957 | 'autoflush' => __PACKAGE__->meta->get_method('output_autoflush') | ||
| 1958 | ); | ||
| 1959 | |||
| 1960 | |||
| 1961 | # Aliasing tie hooks to real functions | ||
| 1962 | foreach my $hook (qw{ CLOSE FILENO PRINT PRINTF READLINE GETC }) { | ||
| 1963 | my $method = lc($hook); | ||
| 1964 | __PACKAGE__->meta->add_method( | ||
| 1965 | $hook => sub { | ||
| 1966 | ### IO::Moose::Handle::$hook: $hook, @_ | ||
| 1967 | shift()->$method(@_) | ||
| 1968 | } | ||
| 1969 | ); | ||
| 1970 | }; | ||
| 1971 | |||
| 1972 | __PACKAGE__->meta->add_method( 'EOF' => sub { | ||
| 1973 | ### IO::Moose::Handle::EOF: @_ | ||
| 1974 | shift()->eof; | ||
| 1975 | } ); | ||
| 1976 | |||
| 1977 | foreach my $hook (qw{ READ WRITE }) { | ||
| 1978 | my $method = 'sys' . lc($hook); | ||
| 1979 | __PACKAGE__->meta->add_method( | ||
| 1980 | $hook => sub { | ||
| 1981 | ### IO::Moose::Handle::$hook: $hook, @_ | ||
| 1982 | shift()->$method(@_) | ||
| 1983 | } | ||
| 1984 | ); | ||
| 1985 | }; | ||
| 1986 | |||
| 1987 | |||
| 1988 | # Make immutable finally | ||
| 1989 | __PACKAGE__->meta->make_immutable; | ||
| 1990 | |||
| 1991 | |||
| 1992 | 1; | ||
| 1993 | |||
| 1994 | |||
| 1995 | =back | ||
| 1996 | |||
| 1997 | =begin umlwiki | ||
| 1998 | |||
| 1999 | = Class Diagram = | ||
| 2000 | |||
| 2001 | [ IO::Moose::Handle | ||
| 2002 | -------------------------------------------------------------------------------------- | ||
| 2003 | +file : Num|FileHandle|OpenHandle {ro} | ||
| 2004 | +mode : CanonOpenModeStr = "<" {ro} | ||
| 2005 | +fh : GlobRef {ro} | ||
| 2006 | +autochomp : Bool = false {rw} | ||
| 2007 | +untaint : Bool = ${^TAINT} {ro} | ||
| 2008 | +blocking : Bool = true {ro} | ||
| 2009 | +copyfh : Bool = false {ro} | ||
| 2010 | +strict_accessors : Bool = false {rw} | ||
| 2011 | +format_formfeed : Str {rw} | ||
| 2012 | +format_line_break_characters : Str {rw} | ||
| 2013 | +input_record_separator : Str {rw} | ||
| 2014 | +output_field_separator : Str {rw} | ||
| 2015 | +output_record_separator : Str {rw} | ||
| 2016 | #_error : Bool | ||
| 2017 | -------------------------------------------------------------------------------------- | ||
| 2018 | <<create>> +new( args : Hash ) : Self | ||
| 2019 | <<create>> +new_from_fd( fd : Num|FileHandle|OpenHandle, mode : CanonOpenModeStr ) : Self | ||
| 2020 | <<create>> +slurp( file : Num|FileHandle|OpenHandle, args : Hash ) : Str|Array | ||
| 2021 | +fdopen( file : Num|FileHandle|OpenHandle, mode : CanonOpenModeStr = '<' ) : Self | ||
| 2022 | +close() : Self | ||
| 2023 | +eof() : Bool | ||
| 2024 | +opened() : Bool | ||
| 2025 | +fileno() : Int | ||
| 2026 | +print( args : Array ) : Self | ||
| 2027 | +printf( fmt : Str = "", args : Array = () ) : Self | ||
| 2028 | +readline() : Maybe[Str|Array] | ||
| 2029 | +getline() : Str | ||
| 2030 | +getlines() : Array | ||
| 2031 | +ungetc( ord : Int ) : Self | ||
| 2032 | +sysread( out buf, len : Int, offset : Int = 0 ) : Int | ||
| 2033 | +syswrite( buf : Str, len : Int, offset : Int = 0 ) : Int | ||
| 2034 | +getc() : Char | ||
| 2035 | +read( out buf, len : Int, offset : Int = 0 ) : Int | ||
| 2036 | +write( buf : Str, len : Int, offset : Int = 0 ) : Int | ||
| 2037 | +format_write( format_name : Str ) : Self | ||
| 2038 | +say( args : Array ) : Self | ||
| 2039 | +slurp() : Str|Array | ||
| 2040 | +truncate( len : Int ) : Self | ||
| 2041 | +stat() : File::Stat::Moose | ||
| 2042 | +error() : Bool | ||
| 2043 | +clearerr() : Bool | ||
| 2044 | +sync() : Self | ||
| 2045 | +flush() : Self | ||
| 2046 | +printflush( args : Array ) : Self | ||
| 2047 | +blocking() : Bool | ||
| 2048 | +blocking( bool : Bool ) : Bool | ||
| 2049 | +untaint() : Self {rw} | ||
| 2050 | +clear_input_record_separator() | ||
| 2051 | +clear_output_field_separator() | ||
| 2052 | +clear_output_record_separator() | ||
| 2053 | +clear_format_formfeed() | ||
| 2054 | +clear_format_line_break_characters() | ||
| 2055 | +format_lines_left() : Str | ||
| 2056 | +format_lines_left( value : Str ) : Str | ||
| 2057 | +format_lines_per_page() : Str | ||
| 2058 | +format_lines_per_page( value : Str ) : Str | ||
| 2059 | +format_page_number() : Str | ||
| 2060 | +format_page_number( value : Str ) : Str | ||
| 2061 | +input_line_number() : Str | ||
| 2062 | +input_line_number( value : Str ) : Str | ||
| 2063 | +autoflush() : Str | ||
| 2064 | +autoflush( value : Str ) : Str | ||
| 2065 | +output_autoflush() : Str | ||
| 2066 | +output_autoflush( value : Str ) : Str | ||
| 2067 | +format_name() : Str | ||
| 2068 | +format_name( value : Str ) : Str | ||
| 2069 | +format_top_name() : Str | ||
| 2070 | +format_top_name( value : Str ) : Str | ||
| 2071 | #_open_file() : Bool | ||
| 2072 | ] | ||
| 2073 | |||
| 2074 | [IO::Moose::Handle] ---|> [MooseX::GlobRef::Object] [IO::Handle] | ||
| 2075 | |||
| 2076 | [IO::Moose::Handle] ---> <<use>> [File::Stat::Moose] | ||
| 2077 | |||
| 2078 | [IO::Moose::Handle] ---> <<exception>> [Exception::Fatal] [Exception::IO] [Exception::Argument] | ||
| 2079 | |||
| 2080 | =end umlwiki | ||
| 2081 | |||
| 2082 | =head1 DEBUGGING | ||
| 2083 | |||
| 2084 | The debugging mode can be enabled if C<PERL_DEBUG_IO_MOOSE_HANDLE> environment | ||
| 2085 | variable is set to true value. The debugging mode requires L<Smart::Comments> | ||
| 2086 | module. | ||
| 2087 | |||
| 2088 | The run-time assertions can be enabled with L<Test::Assert> module. | ||
| 2089 | |||
| 2090 | =head1 INTERNALS | ||
| 2091 | |||
| 2092 | This module uses L<MooseX::GlobRef::Object> and stores the object's attributes | ||
| 2093 | in glob reference. They can be accessed with C<< *$self->{attr} >> | ||
| 2094 | expression or with standard accessors C<< $self->attr >>. | ||
| 2095 | |||
| 2096 | There are two handles used for IO operations: the original handle used for | ||
| 2097 | real IO operations and tied handle which hooks IO functions interface. | ||
| 2098 | |||
| 2099 | The OO-style uses original handle stored in I<fh> field. | ||
| 2100 | |||
| 2101 | # Usage: | ||
| 2102 | $io->print("OO style"); | ||
| 2103 | |||
| 2104 | # Implementation: | ||
| 2105 | package IO::Moose::Handle; | ||
| 2106 | sub print { | ||
| 2107 | my $self = shift; | ||
| 2108 | CORE::print { $self->fh } @_ | ||
| 2109 | } | ||
| 2110 | |||
| 2111 | The IO functions-style uses object reference which is dereferenced as a | ||
| 2112 | handle tied to proxy object which operates on original handle. | ||
| 2113 | |||
| 2114 | # Usage: | ||
| 2115 | print $io "IO functions style"; | ||
| 2116 | |||
| 2117 | # Implementation: | ||
| 2118 | package IO::Moose::Handle; | ||
| 2119 | sub PRINT { shift()->print(@_) }; | ||
| 2120 | sub print { | ||
| 2121 | my $self = shift; | ||
| 2122 | CORE::print { $self->fh } @_ | ||
| 2123 | } | ||
| 2124 | |||
| 2125 | =head1 SEE ALSO | ||
| 2126 | |||
| 2127 | L<IO::Handle>, L<MooseX::GlobRef::Object>, L<Moose>. | ||
| 2128 | |||
| 2129 | =head1 BUGS | ||
| 2130 | |||
| 2131 | The API is not stable yet and can be changed in future. | ||
| 2132 | |||
| 2133 | =head1 AUTHOR | ||
| 2134 | |||
| 2135 | Piotr Roszatycki <dexter@cpan.org> | ||
| 2136 | |||
| 2137 | =head1 LICENSE | ||
| 2138 | |||
| 2139 | Copyright 2007, 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>. | ||
| 2140 | |||
| 2141 | This program is free software; you can redistribute it and/or modify it | ||
| 2142 | under the same terms as Perl itself. | ||
| 2143 | |||
| 2144 | See L<http://www.perl.com/perl/misc/Artistic.html> |