ETL-Yertl
view release on metacpan or search on metacpan
lib/ETL/Yertl.pm view on Meta::CPAN
if ( !ref $xform ) {
my $module = $xform;
$obj = use_module( $module )->new( @args );
}
elsif ( ref $xform eq 'CODE' ) {
$obj = ETL::Yertl::Transform->new(
@args,
transform_doc => $xform,
);
}
loop->add( $obj );
return $obj;
}
#pod =sub file
#pod
#pod my $stream = file( $mode, $path, %args );
#pod
#pod Create a L<ETL::Yertl::FormatStream> object for the given C<$path>.
#pod C<$mode> should be one of C<< < >> for reading or C<< > >> for writing.
#pod C<%args> are additional arguments to pass to the
#pod L<ETL::Yertl::FormatStream> constructor. Useful keys are:
#pod
#pod =over
#pod
#pod =item format
#pod
#pod Specify the format that standard input is. Defaults to C<yaml> or the value
#pod of C<YERTL_FORMAT> (see L<ETL::Yertl::Format/get_default>.
#pod
#pod =back
#pod
#pod =cut
sub file( $$;% ) {
my ( $mode, $name, %args ) = @_;
# Detect whether to read_handle/write_handle via '<', '>'
open my $fh, $mode, $name
or croak sprintf q{Can't open file "%s": %s}, $name, $!;
if ( $mode =~ /^</ ) {
$args{read_handle} = $fh;
}
elsif ( $mode =~ /^>/ ) {
$args{write_handle} = $fh;
}
else {
croak sprintf q{Can't determine if mode "%s" is read or write}, $mode;
}
return stream( %args );
}
#pod =sub yq
#pod
#pod my $xform = yq( $filter );
#pod
#pod Create a L<ETL::Yertl::Transform::Yq> object with the given filter. See
#pod L<yq/SYNTAX> for full filter syntax.
#pod
#pod =cut
sub yq( $ ) {
my ( $filter ) = @_;
return transform(
'ETL::Yertl::Transform::Yq',
filter => $filter,
);
}
#pod =sub loop
#pod
#pod my $loop = loop();
#pod
#pod Get the L<IO::Async::Loop> singleton. Use this to add other L<IO::Async> objects
#pod to a larger program. This is not needed for simple Yertl streams, and is mostly
#pod used internally.
#pod
#pod This is not exported by default. You can import it using C<< use ETL::Yertl 'loop' >>.
#pod
#pod =cut
sub loop() {
state $loop = IO::Async::Loop->new;
return $loop;
}
1;
__END__
=pod
=head1 NAME
ETL::Yertl - ETL with a Shell
=head1 VERSION
version 0.044
=head1 SYNOPSIS
### On a shell...
# Convert file to Yertl's format
$ yfrom csv file.csv >work.yml
$ yfrom json file.json >work.yml
# Convert file to output format
$ yto csv work.yml
$ yto json work.yml
# Parse HTTP logs into documents
$ ygrok '%{LOG.HTTP_COMMON}' httpd.log
# Read data from a database
$ ysql db_name 'SELECT * FROM employee'
# Write data to a database
$ ysql db_name 'INSERT INTO employee ( id, name ) VALUES ( $.id, $.name )'
### In Perl...
use ETL::Yertl;
( run in 3.368 seconds using v1.01-cache-2.11-cpan-b301d465b3d )