The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Chj::xopen

SYNOPSIS

use Chj::xopen;
{
    my $in = xopen_read "foo.txt";
    my $out = glob_to_fh(*STDOUT,"utf-8");
    local $_;
    while (<$in>) { # default operation. (overload not possible :/)
        $out->xprint($_); # print, throwing an exception on error
    }
    $out->xclose; # close explicitely, throwing an exception on error
}
  # $in and $out are closed automatically in any case
  # (issuing a warning on error)

DESCRIPTION

Constructors around Chj::IO::File.

FUNCTIONS

xopen ( EXPR | MODE,LIST )

Open the given file like the perl builtin "open" or croak on errors. Returns an anonymous symbol blessed into Chj::xopen::file, which can be used both as object or filehandle (more correctly: anonymous glob) (? always? Perl is a bit complicated when handling filehandles in indirect object notation).

(BTW, note that perl won't give an error if you open a directory instead of a file for reading. The returned filehandle will give empty results if either used with read or readdir. That's true for perl 5.005x - 5.6.1 on linux.)

xopen_read EXPR
xopen_write EXPR
xopen_append EXPR
xopen_update EXPR

Those *optionally exported* functions check the one given input parameter for <>+ chars at the beginning, and either croak if they don't match the purpose of the function, or prepend the right chars if missing.

stdin, stdout, stderr

The functions with these names call glob_to_fh(*STD..., "utf-8") by default. If given an argument, it is used as the encoding instead of "utf-8".

BUGS

Stuff like >&1 not yet really supported by the above xopen_* functions.

SEE ALSO

Chj::IO::File, Chj::xsysopen, Chj::xopendir

NOTE

This is alpha software! Read the status section in the package README or on the website.