This file is indexed.

/usr/share/perl5/IO/Callback.pm is in libio-callback-perl 1.12-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package IO::Callback;

use warnings;
use strict;

=head1 NAME

IO::Callback - Emulate file interface for a code reference

=head1 VERSION

Version 1.12

=cut

our $VERSION = '1.12';

=head1 SYNOPSIS

C<IO::Callback> provides an easy way to produce a phoney read-only filehandle that calls back to your own code when it needs data to satisfy a read. This is useful if you want to use a library module that expects to read data from a filehandle, but you want the data to come from some other source and you don't want to read it all into memory and use L<IO::String>.

    use IO::Callback;

    my $fh = IO::Callback->new('<', sub { ... ; return $data });
    my $object = Some::Class->new_from_file($fh);

Similarly, IO::Callback allows you to wrap up a coderef as a write-only filehandle, which you can pass to a library module that expects to write its output to a filehandle.

    my $fh = IO::Callback->new('>', sub { my $data = shift ; ... });
    $object->dump_to_file($fh);


=head1 CONSTRUCTOR

=head2 C<new ( MODE, CODEREF [,ARG ...] )>

Returns a filehandle object encapsulating the coderef.

MODE must be either C<E<lt>> for a read-only filehandle or C<E<gt>> for a write-only filehandle.

For a read-only filehandle, the callback coderef will be invoked in a scalar context each time more data is required to satisfy a read. It must return some more input data (at least one byte) as a string. If there is no more data to be read, then the callback should return either C<undef> or the empty string. If ARG values were supplied to the constructor, then they will be passed to the callback each time it is invoked.

For a write-only filehandle, the callback will be invoked each time there is data to be written. The first argument will be the data as a string, which will always be at least one byte long. If ARG values were supplied to the constructor, then they will be passed as additional arguments to the callback. When the filehandle is closed, the callback will be invoked once with the empty string as its first argument.  

To simulate a non-fatal error on the file, the callback should set C<$!> and return the special value C<IO::Callback::Error>. See examples 6 and 7 below.

=head1 EXAMPLES

=over 4

=item Example 1

To generate a filehandle from which an infinite number of C<x> characters can be read:

=for test "ex1" begin

  my $fh = IO::Callback->new('<', sub {"xxxxxxxxxxxxxxxxxxxxxxxxxxx"});

  my $x = $fh->getc;  # $x now contains "x"
  read $fh, $x, 5;    # $x now contains "xxxxx"

=for test "ex1" end

=item Example 2

A filehandle from which 1000 C<foo> lines can be read before EOF:

=for test "ex2" begin

  my $count = 0;
  my $fh = IO::Callback->new('<', sub {
      return if ++$count > 1000; # EOF
      return "foo\n";
  });

  my $x = <$fh>;    # $x now contains "foo\n"
  read $fh, $x, 2;  # $x now contains "fo"
  read $fh, $x, 2;  # $x now contains "o\n"
  read $fh, $x, 20; # $x now contains "foo\nfoo\nfoo\nfoo\nfoo\n"
  my @foos = <$fh>; # @foos now contains ("foo\n") x 993

=for test "ex2" end

The example above uses a C<closure> (a special kind of anonymous sub, see L<http://perldoc.perl.org/perlfaq7.html#What's-a-closure?>) to allow the callback to keep track of how many lines it has returned. You don't have to use a closure if you don't want to, since C<IO::Callback> will forward extra constructor arguments to the callback. This example could be re-written as:

=for test "ex2a" begin

  my $count = 0;
  my $fh = IO::Callback->new('<', \&my_callback, \$count); 

  my $x = <$fh>;    # $x now contains "foo\n"
  read $fh, $x, 2;  # $x now contains "fo"
  read $fh, $x, 2;  # $x now contains "o\n"
  read $fh, $x, 20; # $x now contains "foo\nfoo\nfoo\nfoo\nfoo\n"
  my @foos = <$fh>; # @foos now contains ("foo\n") x 993

  sub my_callback {
      my $count_ref = shift;

      return if ++$$count_ref > 1000; # EOF
      return "foo\n";
  };

=for test "ex2a" end

=item Example 3

To generate a filehandle interface to data drawn from an SQL table:

=for test "ex3" begin

  my $sth = $dbh->prepare("SELECT ...");
  $sth->execute;
  my $fh = IO::Callback->new('<', sub {
      my @row = $sth->fetchrow_array;
      return unless @row; # EOF
      return join(',', @row) . "\n";
  });

  # ...

=for test "ex3" end

=item Example 4

You want a filehandle to which data can be written, where the data is discarded but an exception is raised if the data includes the string C<foo>.

=for test "ex4" begin

  my $buf = '';
  my $fh = IO::Callback->new('>', sub {
      $buf .= shift;
      die "foo written" if $buf =~ /foo/;

      if ($buf =~ /(fo?)\z/) {
          # Part way through a "foo", carry over to the next block.
          $buf = $1;
      } else {
          $buf = '';
      }
  });

=for test "ex4" end

=item Example 5

You have been given an object with a copy_data_out() method that takes a destination filehandle as an argument.  You don't want the data written to a file though, you want it split into 1024-byte blocks and inserted into an SQL database.

=for test "ex5" begin

  my $blocksize = 1024;
  my $sth = $dbh->prepare('INSERT ...');

  my $buf = '';
  my $fh = IO::Callback->new('>', sub {
      $buf .= shift;
      while (length $buf >= $blocksize) {
          $sth->execute(substr $buf, 0, $blocksize, '');
      }
  });

  $thing->copy_data_out($fh);

  if (length $buf) {
      # There is a remainder of < $blocksize
      $sth->execute($buf);
  }

=for test "ex5" end

=item Example 6

You're testing some code that reads data from a file, you want to check that it behaves as expected if it gets an IO error part way through the file.

=for test "ex6" begin

  use IO::Callback;
  use Errno qw/EIO/;

  my $block1 = "x" x 10240;
  my $block2 = "y" x 10240;
  my @blocks = ($block1, $block2);

  my $fh = IO::Callback->new('<', sub {
      return shift @blocks if @blocks;
      $! = EIO;
      return IO::Callback::Error;
  });

  # ...

=for test "ex6" end

=item Example 7

You're testing some code that writes data to a file handle, you want to check that it behaves as expected if it gets a C<file system full> error after it has written the first 100k of data.

=for test "ex7" begin

  use IO::Callback;
  use Errno qw/ENOSPC/;

  my $wrote = 0;
  my $fh = IO::Callback->new('>', sub {
      $wrote += length $_[0];
      if ($wrote > 100_000) {
          $! = ENOSPC;
          return IO::Callback::Error;
      }
  });

  # ...

=for test "ex7" end

=back

=cut

use Carp;
use Errno qw/EBADF/;
use IO::String;
use base qw/IO::String/;

sub open
{
    my $self = shift;
    return $self->new(@_) unless ref($self);

    my $mode = shift or croak "mode missing in IO::Callback::new";
    if ($mode eq '<') {
        *$self->{R} = 1;
    } elsif ($mode eq '>') {
        *$self->{W} = 1;
    } else {
        croak qq{invalid mode "$mode" in IO::Callback::new};
    }

    my $code = shift or croak "coderef missing in IO::Callback::new";
    ref $code eq "CODE" or croak "non-coderef second argument in IO::Callback::new";

    my $buf = '';
    *$self->{Buf} = \$buf;
    *$self->{Pos} = 0;
    *$self->{Err} = 0;
    *$self->{lno} = 0;

    if (@_) {
        my @args = @_;
        *$self->{Code} = sub { $code->(@_, @args) };
    } else {
        *$self->{Code} = $code;
    }
}

sub close
{
    my $self = shift;
    return unless defined *$self->{Code};
    return if *$self->{Err};
    if (*$self->{W}) {
        my $ret = *$self->{Code}('');
        if ($ret and ref $ret eq 'IO::Callback::ErrorMarker') {
            *$self->{Err} = 1;
            return;
        }
    }
    foreach my $key (qw/Code Buf Eof R W Pos lno/) {
        delete *$self->{$key};
    }
    *$self->{Err} = -1;
    undef *$self if $] eq "5.008";  # cargo culted from IO::String
    return 1;
}

sub opened
{
    my $self = shift;
    return defined *$self->{R} || defined *$self->{W};
}

sub getc
{
    my $self = shift;
    *$self->{R} or return $self->_ebadf;
    my $buf;
    return $buf if $self->read($buf, 1);
    return undef;
}

sub ungetc
{
    my ($self, $char) = @_;
    *$self->{R} or return $self->_ebadf;
    my $buf = *$self->{Buf};
    $$buf = chr($char) . $$buf;
    --*$self->{Pos};
    delete *$self->{Eof};
    return 1;
}

sub eof
{
    my $self = shift;
    return *$self->{Eof};
}

# Use something very distinctive for the error return code, since write callbacks
# may pay no attention to what they are returning, and it would be bad to mistake
# returned noise for an error indication.
sub Error () {
    return bless {}, 'IO::Callback::ErrorMarker';
}

sub _doread {
    my $self = shift;

    return unless *$self->{Code};
    my $newbit = *$self->{Code}();
    if (defined $newbit) {
        if (ref $newbit) {
            if (ref $newbit eq 'IO::Callback::ErrorMarker') {
                *$self->{Err} = 1;
                return;
            } else {
                confess "unexpected reference type ".ref($newbit)." returned by callback";
            }
        }
        if (length $newbit) {
            ${*$self->{Buf}} .= $newbit;
            return 1;
        }
    }

    # fall-through for both undef and ''
    delete *$self->{Code};
    return;
}

sub getline
{
    my $self = shift;

    *$self->{R} or return $self->_ebadf;
    return if *$self->{Eof} || *$self->{Err};
    my $buf = *$self->{Buf};
    $. = *$self->{lno};

    unless (defined $/) {  # slurp
        1 while $self->_doread;
        return if *$self->{Err};
        *$self->{Pos} += length $$buf;
        *$self->{Eof} = 1;
        *$self->{Buf} = \(my $newbuf = '');
        $. = ++ *$self->{lno};
        return $$buf;
    }

    my $rs = length $/ ? $/ : "\n\n";
    for (;;) {
        # In paragraph mode, discard extra newlines.
        if ($/ eq '' and $$buf =~ s/^(\n+)//) {
            *$self->{Pos} += length $1;
        }
        my $pos = index $$buf, $rs;
        if ($pos >= 0) {
            *$self->{Pos} += $pos+length($rs);
            my $ret = substr $$buf, 0, $pos+length($rs), '';
            unless (length $/) {
                # paragraph mode, discard extra trailing newlines
                $$buf =~ s/^(\n+)// and *$self->{Pos} += length $1;
                while (*$self->{Code} and length $$buf == 0) {
                    $self->_doread;
                    return if *$self->{Err};
                    $$buf =~ s/^(\n+)// and *$self->{Pos} += length $1;
                }
            }
            $self->_doread while *$self->{Code} and length $$buf == 0 and not *$self->{Err};
            if (length $$buf == 0 and not *$self->{Code}) {
                *$self->{Eof} = 1;
            }
            $. = ++ *$self->{lno};
            return $ret;
        }
        if (*$self->{Code}) {
            $self->_doread;
            return if *$self->{Err};
        } else {
            # EOL not in buffer and no more data to come - the last line is missing its EOL.
            *$self->{Eof} = 1;
            *$self->{Pos} += length $$buf;
            *$self->{Buf} = \(my $newbuf = '');
            $. = ++ *$self->{lno} if length $$buf;
            return $$buf if length $$buf;
            return;
        }
    }
}

sub getlines
{
    croak "getlines() called in scalar context" unless wantarray;
    my $self = shift;

    *$self->{R} or return $self->_ebadf;
    return if *$self->{Err} || *$self->{Eof};

    # To exactly match Perl's behavior on real files, getlines() should not
    # increment $. if there is no more input, but getline() should. I won't
    # call getline() until I've established that there is more input.
    my $buf = *$self->{Buf};
    unless (length $$buf) {
        $self->_doread;
        return unless length $$buf;
    }

    my($line, @lines);
    push(@lines, $line) while defined($line = $self->getline);
    return @lines;
}

sub READLINE
{
    goto &getlines if wantarray;
    goto &getline;
}

sub read
{
    my $self = shift;

    *$self->{R} or return $self->_ebadf;
    my $len = $_[1]||0;

    croak "Negative length" if $len < 0;
    return if *$self->{Err};
    return 0 if *$self->{Eof};
    my $buf = *$self->{Buf};

    1 while *$self->{Code} and $len > length $$buf and $self->_doread;
    return if *$self->{Err};
    if ($len > length $$buf) {
        $len = length $$buf;
        *$self->{Eof} = 1 unless $len;
    }

    if (@_ > 2) { # read offset
        my $offset = $_[2]||0;
        if ($offset < -1 * length $_[0]) {
            croak "Offset outside string";
        }
        if ($offset > length $_[0]) {
            $_[0] .= "\0" x ($offset - length $_[0]);
        }
        substr($_[0], $offset) = substr($$buf, 0, $len, '');
    }
    else {
        $_[0] = substr($$buf, 0, $len, '');
    }
    *$self->{Pos} += $len;
    return $len;
}

*sysread = \&read;
*syswrite = \&write;

sub stat {
    my $self = shift;
    return unless $self->opened;
    return 1 unless wantarray;

    my @stat = $self->SUPER::stat();

    # size unknown, report 0
    $stat[7] = 0;
    $stat[12] = 1;

    return @stat;
}

sub print
{
    my $self = shift;

    my $result;
    if (defined $\) {
        if (defined $,) {
            $result = $self->write(join($,, @_).$\);
        }
        else {
            $result = $self->write(join("",@_).$\);
        }
    }
    else {
        if (defined $,) {
            $result = $self->write(join($,, @_));
        }
        else {
            $result = $self->write(join("",@_));
        }
    }

    return unless defined $result;
    return 1;
}
*printflush = \*print;

sub printf
{
    my $self = shift;
    my $fmt = shift;
    my $result = $self->write(sprintf($fmt, @_));
    return unless defined $result;
    return 1;
}

sub getpos
{
    my $self = shift;

    $. = *$self->{lno};
    return *$self->{Pos};
}
*tell = \&getpos;
*pos  = \&getpos;

sub setpos
{
    croak "setpos not implemented for IO::Callback";
}

sub truncate
{
    croak "truncate not implemented for IO::Callback";
}

sub seek
{
    croak "Illegal seek";
}
*sysseek = \&seek;

sub write
{
    my $self = shift;

    *$self->{W} or return $self->_ebadf;
    return if *$self->{Err};

    my $slen = length($_[0]);
    my $len = $slen;
    my $off = 0;
    if (@_ > 1) {
        my $xlen = defined $_[1] ? $_[1] : 0;
        $len = $xlen if $xlen < $len;
        croak "Negative length" if $len < 0;
        if (@_ > 2) {
            $off = $_[2] || 0;
            if ( $off >= $slen and $off > 0 and ($] < 5.011 or $off > $slen) ) {
                croak "Offset outside string";
            }
            if ($off < 0) {
                $off += $slen;
                croak "Offset outside string" if $off < 0;
            }
            my $rem = $slen - $off;
            $len = $rem if $rem < $len;
        }
    }
    return $len if $len == 0;
    my $ret = *$self->{Code}(substr $_[0], $off, $len);
    if (defined $ret and ref $ret eq 'IO::Callback::ErrorMarker') {
        *$self->{Err} = 1;
        return;
    }
    *$self->{Pos} += $len;
    return $len;
}

sub error {
    my $self = shift;

    return *$self->{Err};
}

sub clearerr {
    my $self = shift;

    *$self->{Err} = 0;
}

sub _ebadf {
    my $self = shift;

    $! = EBADF;
    *$self->{Err} = -1;
    return;
}

*GETC   = \&getc;
*PRINT  = \&print;
*PRINTF = \&printf;
*READ   = \&read;
*WRITE  = \&write;
*SEEK   = \&seek;
*TELL   = \&getpos;
*EOF    = \&eof;
*CLOSE  = \&close;

=head1 AUTHOR

Dave Taylor, C<< <dave.taylor.cpan at gmail.com> >>

=head1 BUGS AND LIMITATIONS

Fails to inter-operate with some library modules that read or write filehandles from within XS code. I am aware of the following specific cases, please let me know if you run into any others:

=over 4

=item C<Digest::MD5::addfile()>

=back

Please report any other bugs or feature requests to C<bug- at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO::Callback>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc IO::Callback

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO::Callback>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/IO::Callback>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/IO::Callback>

=item * Search CPAN

L<http://search.cpan.org/dist/IO::Callback>

=back

=head1 SEE ALSO

L<IO::String>, L<IO::Stringy>, L<perlfunc/open>

=head1 ACKNOWLEDGEMENTS

Adapted from code in L<IO::String> by Gisle Aas.

=head1 MANITAINER

This module is currently being maintained by Toby Inkster (TOBYINK)
for bug fixes. No substantial changes or new features are planned.

=head1 COPYRIGHT & LICENSE

Copyright 1998-2005 Gisle Aas.

Copyright 2009-2010 Dave Taylor.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of IO::Callback