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!
3
Depends on the layer.
There are two options for the base layer of ordinary handles: :unix
and :stdio
.
-
The
:unix
layer is is defined inperlio.c
asPerlIO_unix
.-
For opening,
:unix
usesPerlIOUnix_open
, which callsPerlLIO_open3_cloexec
, which callsPerlLIO_open3
, which is defined asopen
(except maybe on MVS). -
For writing,
:unix
usesPerlIOUnix_write
, which callsPerlLIO_write
, which is defined aswrite
.
-
-
The
:stdio
layer is is defined inperlio.c
asPerlIO_stdio
.-
For opening,
:stdio
usesPerlIOStdio_open
. Follow from there. -
For writing,
:stdio
usesPerlIOStdio_write
, which callsPerlSIO_fwrite
, which is defined asfwrite
.
-
The selection of the base layer appears to be:
- If a layer is provided to
open
, use that one. - If a process-wide default is provided (e.g. using the
PERLIO
env var), use that one. - 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.]
4