There are some open
methods which only support the open() do io ... end
conventions. This module provides a trick to enable keeping io
for later usage. This is convenient for interactive programming.
using WebSockets as an example.
using OpenTrick
using WebSockets
io = opentrick(WebSockets.open, "ws://echo.websocket.org");
write(io, "Hello");
println(String(read(io)));
close(io) # you can close io manually
io = nothing; # or leave it to GC
unsafe_clear() # or you can clear all ios opened by opentrick manually
- read, read!, readbytes!, unsafe_read, readavailable, readline, readlines, eachline, readchomp, readuntil, bytesavailable
- write, unsafe_write, truncate, flush, print, println, printstyled, showerror
- seek, seekstart, seekend, skip, skipchars, position
- mark, unmark, reset, ismarked
- isreadonly, iswritable, isreadable, isopen, eof
- countlines, displaysize
#
OpenTrick.opentrick
— Function.
opentrick(openfn[, args... [; <keyword arguments>]])
Call openfn
with (handlefn, args... ,kwargs ...)
as arguments, return an IOWrapper
instance. (NB:handlefn
is provided by opentrick
.)
Arguments
openfn::Function
function actually called to obtain aIO
instance.openfn
must take aFunction(::IO)
instance as its first argumentargs
optional arguments that will be passed toopenfn
kwargs
optional keyword arguments that will be passed toopenfn
Examples
julia> using OpenTrick
julia> filename = tempname();
julia> io = opentrick(open, filename, "w+");
julia> write(io, "hello world!")
12
julia> seek(io, 0);
julia> readline(io)
"hello world!"
#
OpenTrick.rawio
— Function.
rawio(io)
Return the actual io
instance
#
OpenTrick.blockingtask
— Function.
blockingtask(io)
Return the task blocking which prevents the handlefn
passed to openfn
from returning
#
OpenTrick.unsafe_clear
— Function.
unsafe_clear()
Unblock all blocking tasks. All io
s returned by opentrick
will be closed as a consequence.