File-BOM

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    If an undefined value is passed as the handle, a symbol will be
    generated for it like open() does:

        # create filehandle on the fly
        $enc = open_bom(my $fh, $filename, ':utf8');
        $line = <$fh>;

    The filehandle will be cued up to read after the BOM. Unseekable files
    (e.g. fifos) will cause croaking, unless called in list context to catch
    spillage from the handle. Any spillage will be automatically decoded
    from the encoding, if found.

        e.g.

        # croak if my_socket is unseekable
        open_bom(FH, 'my_socket');

        # keep spillage if my_socket is unseekable
        ($encoding, $spillage) = open_bom(FH, 'my_socket');

lib/File/BOM.pm  view on Meta::CPAN


If an undefined value is passed as the handle, a symbol will be generated for it
like open() does:

    # create filehandle on the fly
    $enc = open_bom(my $fh, $filename, ':utf8');
    $line = <$fh>;

The filehandle will be cued up to read after the BOM. Unseekable files (e.g.
fifos) will cause croaking, unless called in list context to catch spillage
from the handle. Any spillage will be automatically decoded from the encoding,
if found.

    e.g.

    # croak if my_socket is unseekable
    open_bom(FH, 'my_socket');

    # keep spillage if my_socket is unseekable
    ($encoding, $spillage) = open_bom(FH, 'my_socket');

t/01..bom.t  view on Meta::CPAN

    }

    seek(FH, 0, SEEK_SET);

    is(get_encoding_from_filehandle(FH), $file_enc, "$file: get_encoding_from_filehandle returned correct encoding");

    my($enc, $offset) = get_encoding_from_bom($first_line);
    is($enc, $file_enc, "$file: get_encoding_from_bom also worked");

    {
	my $decoded = $enc ? decode($enc, substr($first_line, $offset))
			   : $first_line;

	is($decoded, $expect, "$file: .. and offset worked with substr()");
    }

    #
    # decode_from_bom()
    #
    my $result = decode_from_bom($first_line, 'UTF-8', FB_CROAK);
    is($result, $expect, "$file: decode_from_bom() scalar context");
    {
	# with default
	my $default = 'UTF-8';
	my $expect_enc = $file_enc || $default;

	my($decoded, $got_enc) = decode_from_bom($first_line, $default, FB_CROAK);

	is($decoded, $expect,      "$file: decode_from_bom() list context");
	is($got_enc, $expect_enc,  "$file: decode_from_bom() list context encoding");
    }
    {
	# without default
	my $expect_enc = $file_enc;
	my($decoded, $got_enc) = decode_from_bom($first_line, undef, FB_CROAK);

	is($decoded, $expect,      "$file: decode_from_bom() list context, no default");
	is($got_enc, $expect_enc,  "$file: decode_from_bom() list context encoding, no default");
    }

    seek(FH, 0, SEEK_SET);

    ($enc, my $spill) = get_encoding_from_stream(FH);

    $line = <FH>; chomp $line;

    is($enc, $file_enc, "$file: get_encoding_from_stream()");



( run in 0.480 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )