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!
Depends on the layer.
There are two options for the base layer of ordinary handles: :unix
and :stdio
.
The :unix
layer is is defined in perlio.c
as PerlIO_unix
.
For opening, :unix
uses PerlIOUnix_open
, which calls PerlLIO_open3_cloexec
, which calls PerlLIO_open3
, which is defined as open
(except maybe on MVS).
For writing, :unix
uses PerlIOUnix_write
, which calls PerlLIO_write
, which is defined as write
.
The :stdio
layer is is defined in perlio.c
as PerlIO_stdio
.
For opening, :stdio
uses PerlIOStdio_open
. Follow from there.
For writing, :stdio
uses PerlIOStdio_write
, which calls PerlSIO_fwrite
, which is defined as fwrite
.
The selection of the base layer appears to be:
open
, use that one.PERLIO
env var), use that one.: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.]