File: perlio_open.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (51 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download
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
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
    skip_all_without_perlio();
    skip_all_without_dynamic_extension('Fcntl'); # how did you get this far?
}

use strict;
use warnings;

plan tests => 11;

use Fcntl qw(:seek);

{
    ok((open my $fh, "+>", undef), "open my \$fh, '+>', undef");
    print $fh "the right write stuff";
    ok(seek($fh, 0, SEEK_SET), "seek to zero");
    my $data = <$fh>;
    is($data, "the right write stuff", "found the right stuff");
}

{
    ok((open my $fh, "+<", undef), "open my \$fh, '+<', undef");
    print $fh "the right read stuff";
    ok(seek($fh, 0, SEEK_SET), "seek to zero");
    my $data = <$fh>;
    is($data, "the right read stuff", "found the right stuff");
}

SKIP:
{
    ok((open my $fh, "+>>", undef), "open my \$fh, '+>>', undef")
      or skip "can't open temp for append: $!", 3;
    print $fh "abc";
    ok(seek($fh, 0, SEEK_SET), "seek to zero");
    print $fh "xyz";
    ok(seek($fh, 0, SEEK_SET), "seek to zero again");
    my $data = <$fh>;
    is($data, "abcxyz", "check the second write appended");
}

{
    # minimal-reproduction moral equivalent of the autodie wrapper for open()
    # because APIs that wrap open() should be able to expose this behaviour
    sub wrapped_open (*;$@) { open $_[0], $_[1], $_[2] }
    ok((wrapped_open my $fh, "+>", undef), "wrapped_open my \$fh, '+>', undef");
}