#!/usr/bin/env perl
#PODNAME: transerialize

package File::Serialize::transerialize;
our $AUTHORITY = 'cpan:YANICK';
#ABSTRACT: transerialize files from a format to a different one
$File::Serialize::transerialize::VERSION = '1.1.1';

use strict;
use warnings;

my %args;

BEGIN {
    while( $ARGV[0] =~ /=/ ) {
        my ($k,$v) = split '=', shift @ARGV;
        $args{$k} = $v;
    }

    if ( $ARGV[0] =~ s/(?<=^-)\.(?<format>\w+)$// ) {
        $args{format} = $+{format};
    }
}

use File::Serialize \%args;

my $codish = qr/
    ^(?:
         [ \[\{ ]    # [ .. ] or { ... }
       | sub \s* \{  # or a sub
    )
/x;

for ( grep { $_ =~ $codish }  @ARGV ) {
    $_ = eval $_ or die $@;
}

for( grep { /^-\.\w+$/ } @ARGV ) {
    $_ = { split '\.', $_, 2 };
}

transerialize_file @ARGV;

__END__

=pod

=encoding UTF-8

=head1 NAME

transerialize - transerialize files from a format to a different one

=head1 VERSION

version 1.1.1

=head1 SYNOPSIS

    # simple
    $ transerialize foo.yaml bar.json

    # with options
    $ transerialize pretty=1 foo.yaml bar.json

    # reading from STDIN
    $ cat foo.yaml | transerialize -.yaml bar.json

    # printing to STDOUT
    $ transerialize foo.yaml -.json

=head1 DESCRIPTION

C<transerialize> is a command-line interface to the 
L<File::Serialize> function of the same name.

The command behaves pretty much like its underlying function, except
for the details below.

=head2 Default options

All leading arguments containing an '=' 
will be considered default options. In other words, 

    $ transerialize pretty=1 format=json foo bar

is equivalent to the script

    use File::Serialize { pretty => 1, format => 'json' };

    transerialize_file 'foo' => 'bar';

=head2 Code arguments

Any argument that begin with a '{', '[' or 'sub {' will be
eval'ed (as opposed as being considered filenames).

=head2 STDIN and STDOUT

An input filename which main part is a dash will be
taken to be STDIN. For example

    $ transerialize_file -.yaml foo.json

will read STDIN, consider it to be YAML, and then
convert it to JSON.

Likewise, an output file which main part is a dash
will be printed on STDOUT. For example

    $ transerialize_file foo.yaml -.json

will print out the data of F<foo.yaml> as JSON.

=head1 AUTHOR

Yanick Champoux <yanick@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
