#! perl
use B::Assembler qw(assemble_fh);
use FileHandle;

=pod

=head1 NAME

assemble - Assemble Perl bytecode

=head1 SYNOPSIS

  assemble [-d] [filename] [outfilename]

=head1 DESCRIPTION

Compiles a perl script to binary bytecode assembler.

bytecode is a binary file wih either the magic 4 bytes 'PLBC'
at the start, or something like "#! /usr/bin/perl\n
use ByteLoader '0.07'"

If filename is -, the input is read from STDIN and
you can still provide an outfilename.

=head1 OPTION -d

Prints some debugging information.

=cut


my ($filename, $fh, $out);

if ($ARGV[0] eq "-d") {
    B::Assembler::debug(1);
    shift;
}

$out = \*STDOUT;

if (@ARGV == 0) {
    $fh = \*STDIN;
    $filename = "-";
} elsif (@ARGV == 1) {
    $filename = $ARGV[0];
    $fh = new FileHandle "<$filename";
} elsif (@ARGV == 2) {
    $filename = $ARGV[0];
    $fh = new FileHandle "<$filename";
    $out = new FileHandle ">$ARGV[1]";
} else {
    die "Usage: assemble [-d] [filename | -] [outfilename]\n";
}

binmode $out;
$SIG{__WARN__} = sub { warn "$filename:@_" };
$SIG{__DIE__} = sub { die "$filename: @_" };
assemble_fh($fh, sub { print $out @_ });
