fileperliolibcstdio

Which libc/stdio IO functions Perl uses to open a file?


Which libc/stdio functions does Perl use to open and read a file (e.g. for perl -e 'open(F,"<","test.txt");print(<F>);')?

I tried running this command under strace and it gave me a sequence open -> fcntl -> ioctl -> lseek -> fstat -> mmap -> read. strace intercepts syscalls, so I'm wondering what are the higher-level libc/stdio functions are actually being called in https://github.com/Perl/perl5/blob/blead/perlio.c and https://github.com/Perl/perl5/blob/blead/doio.c? The Perl I/O codebase has many layers of indirection, so I'm struggling to understand it.

I'm not a Perl pro, so not understanding https://perldoc.perl.org/PerlIO and the codebase sufficiently enough :(

Thanks!


Solution

  • Depends on the layer.

    There are two options for the base layer of ordinary handles: :unix and :stdio.

    The selection of the base layer appears to be:

    1. If a layer is provided to open, use that one.
    2. If a process-wide default is provided (e.g. using the PERLIO env var), use that one.
    3. Otherwise, use the default selected when Perl was built. This can apparently be :stdio in some circumstances, but it's usually :perlio, and :perlio adds an underlying :unix layer.

    Note that :unix is used on Windows as well. It refers to the libc functions rather than anything specific to Unix.

    [All links for v5.40.0.]