NAME
Prima::File - asynchronous stream I/O.
SYNOPSIS
use strict;
use Prima qw(Application);
# create pipe and autoflush the writer end
pipe(READ, WRITE) or die "pipe():$!\n";
select WRITE;
$|=1;
select STDOUT;
# create Prima listener on the reader end
my $read = Prima::File-> new(
file => \*READ,
mask => fe::Read,
onRead => sub {
$_ = <READ>;
print "read:$_\n";
},
);
print WRITE "line\n";
run Prima;
DESCRIPTION
Prima::File provides access to the I/O stream events, that are called when a file handle becomes readable, writable or if an exception occurred. Registering file handles to Prima::File objects makes possible the stream operations coexist with the event loop.
USAGE
Prima::File is a descendant of Prima::Component. Objects of Prima::File class must be bounded to a valid file handle object, before the associated events can occur:
my $f = Prima::File-> create();
$f-> file( *STDIN);
When a file handle, bound via the ::file
property becomes readable, writable or when an exception signaled, one of three correspondent events called - Read
, Write
or Exception
. When a handle is always readable, or always writable, or, some of these events are desired to be blocked, the file event mask can be set via the ::mask
property:
$f-> mask( fe::Read | fe::Exception);
When a file handle is not needed anymore, it is expected to be detached from an object explicitly:
$f-> file( undef);
However, if the system detects that a file handle is no longer valid, it is automatically detached. It is possible to check, if a file handle is still valid by calling the is_active()
method.
Prima::File events are basically the same I/O callbacks, provided by a system select()
call. See documentation of your system's select() for the implementation details.
API
Properties
- file HANDLE
-
Selects a file handle, that is to be monitored for stream I/O events. If HANDLE is
undef
, object is returned to a passive state, and the previously bonded file handle is de-selected.If the OS reports an error when attaching the file, f ex because there are too many objects to monitor, the file handle is reverted to
undef
. Use that to check for an error. - fd INTEGER
-
Same as file(), but to be used for file descriptors. When this property is used, consequent get-calls to file() will return undef.
If the OS reports an error when attaching the file, f ex because there are too many objects to monitor, the file handle is reverted to
undef
. Use that to check for an error. - mask EVENT_MASK
-
Selects a event mask, that is a combination of
fe::XXX
integer constants, each representing an event:fe::Read fe::Write fe::Exception
The omitted events are effectively excluded from the system file event multiplexing mechanism.
Methods
- get_handle
-
Returns
sprintf("0x%08x", fileno( file ))
string. If::file
isundef
, -1 is used instead fileno() result. - is_active AUTODETACH = 0
-
Returns a boolean flag, indicating if a file handle is valid. If AUTODETACH is 1, and the file handle is not valid,
file(undef)
is called.
Events
- Read
-
Called when a file handle becomes readable. The callback procedure is expected to call a non-blocking read() on the file handle.
- Write
-
Called when a file handle becomes writable. The callback procedure is expected to call a non-blocking write() on the file handle.
- Exception
-
Called when an exception is signaled on a file handle. The exceptions are specific to handle type and the operating system. For example, a unix socket signals
Exception
when a control status data for a pseudo terminal or an out-of-band data arrives.
OS considerations
Unix
Prima can monitor max FD_SETSIZE file handles (not Prima::File objects, these can refer to same file handles just fine). See also man 2 select
.
Win32
- Files
-
If Prima detects that a handle is neither a socket nor a console, it assumes that it is a "regular" file. Prima doesn't use any win32 api for checking on regular file handle availability for reading and writing, and therefore sends synthetic events without actual correlation whether the file handle is actually readable or writable.
- Pipes
-
Pipe handles are not implemented and won't work.
- Sockets
-
Sockets work natively, however there's a single catch: according to the MSDN, WSAEventSelect() sets sockets in a non-blocking mode, however I couldn't confirm that when I was experimenting. If you want to be 100% covered, remember and restore the blocking flag in your event handlers.
There can be normally max 63 sockets (not Prima::File objects, these can refer to same sockets just fine). Or max 62 sockets if STDIN is monitored too. See also https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-msgwaitformultipleobjectsex .
- STDIN
-
STDIN works fine when it is a console. Use
Prima::sys::win32::ReadConsoleInput
for detailed input parsing. See also https://learn.microsoft.com/en-us/windows/console/readconsoleinputex.
AUTHOR
Dmitry Karasik, <dmitry@karasik.eu.org>.