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
|
use strict;
use FileHandle;
use FileHandle::Unget;
use File::Spec::Functions qw(:ALL);
use Test::More tests => 5;
use File::Temp;
my $tmp = File::Temp->new();
{
print $tmp "first line\n";
print $tmp "second line\n";
close $tmp;
}
# Test ungetc'ing and reading a line of data
{
my $fh = new FileHandle::Unget($tmp->filename);
my $line = <$fh>;
$fh->ungetc(ord("\n"));
$fh->ungetc(ord("d"));
$fh->ungetc(ord("e"));
$fh->ungetc(ord("t"));
$fh->ungetc(ord("r"));
$fh->ungetc(ord("e"));
$fh->ungetc(ord("s"));
$fh->ungetc(ord("n"));
$fh->ungetc(ord("i"));
$line = <$fh>;
# 1
is($line, "inserted\n",'Ungetc');
$line = <$fh>;
# 2
is($line, "second line\n",'getline()');
$fh->close;
}
# Test ungetc'ing and read'ing some bytes of data
{
my $fh = new FileHandle::Unget($tmp->filename);
my $line = <$fh>;
$fh->ungetc(ord("\n"));
$fh->ungetc(ord("d"));
$fh->ungetc(ord("e"));
$fh->ungetc(ord("t"));
$fh->ungetc(ord("r"));
$fh->ungetc(ord("e"));
$fh->ungetc(ord("s"));
$fh->ungetc(ord("n"));
$fh->ungetc(ord("i"));
read($fh, $line, 6);
# 3
is($line, "insert", 'read() after insert');
$line = <$fh>;
# 4
is($line, "ed\n", 'getline() 1');
$line = <$fh>;
# 5
is($line, "second line\n", 'getline() 2');
$fh->close;
}
|