Sha256: aba12acf9196bf5e1a1df329e3b9acdcd8b6edaba8cb447f20c479d501dc5787

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

use strict;
use warnings;
package YAML::PP::Writer::File;

our $VERSION = 'v0.38.0'; # VERSION

use Scalar::Util qw/ openhandle /;

use base qw/ YAML::PP::Writer /;

use Carp qw/ croak /;

sub _open_handle {
    my ($self) = @_;
    if (openhandle($self->{output})) {
        $self->{filehandle} = $self->{output};
        return $self->{output};
    }
    open my $fh, '>:encoding(UTF-8)', $self->{output}
        or croak "Could not open '$self->{output}' for writing: $!";
    $self->{filehandle} = $fh;
    return $fh;
}

sub write {
    my ($self, $line) = @_;
    my $fh = $self->{filehandle};
    print $fh $line;
}

sub init {
    my ($self) = @_;
    my $fh = $self->_open_handle;
}

sub finish {
    my ($self) = @_;
    if (openhandle($self->{output})) {
        # Original argument was a file handle, so the caller needs
        # to close it
        return;
    }
    close $self->{filehandle};
}

1;

__END__

=pod

=encoding utf-8

=head1 NAME

YAML::PP::Writer::File - Write YAML output to file or file handle

=head1 SYNOPSIS

    my $writer = YAML::PP::Writer::File->new(output => $file);

=head1 DESCRIPTION

The L<YAML::PP::Emitter> sends its output to the writer.

You can use your own writer. if you want to send the YAML output to
somewhere else. See t/44.writer.t for an example.

=head1 METHODS

=over

=item new

    my $writer = YAML::PP::Writer::File->new(output => $file);
    my $writer = YAML::PP::Writer::File->new(output => $filehandle);

Constructor.

=item write

    $writer->write('- ');

=item init

    $writer->init;

Initialize

=item finish

    $writer->finish;

Gets called when the output ends. If The argument was a filename, the
filehandle will be closed. If the argument was a filehandle, the caller needs to
close it.

=item output, set_output

Getter/setter for the YAML output

=back

=cut

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
javonet-ruby-sdk-2.5.10 lib/javonet-ruby-sdk/Binaries/Perl/Linux/X64/deps/lib/perl5/YAML/PP/Writer/File.pm
javonet-ruby-sdk-2.5.10 lib/javonet-ruby-sdk/Binaries/Perl/Windows/X64/deps/lib/perl5/YAML/PP/Writer/File.pm