Batch-Interpreter

 view release on metacpan or  search on metacpan

bin/runbat  view on Meta::CPAN

	dump_parse_tree => $dump_parse_tree,
	verbose_system => $verbose_system,
	verbose_set => $verbose_set,

	echo => !$interactive_mode,
	vars => \%var,
	varcases => \%varcase,
	terminal => -t STDIN ? Term::ReadLine->new('CON') : undef,

	version_string => 'Microsoft Windows [Version 6.1.7601]',
	locale => 'de_DE',

	filenames => \%filename,
	mountpoints => \@mountpoint,
	remote_names => \@remote_name,

	internal_commands => \%internal_command,
	external_commands => \%external_command,
);

# this is global state which is difficult to restore (even with PerlIO::Layers),

lib/Batch/Interpreter.pm  view on Meta::CPAN

our $VERSION = '0.01';


=head1 SYNOPSIS
	use Batch::Interpreter;

	open my $fh, '<:crlf', $ARGV[0]
		or die "$ARGV[0]: $!";

	my $rc = Batch::Interpreter->new(
		locale => 'de_DE',
		# more settings, see below
	)->run({}, [ <$fh> ], @ARGV);

=head1 METHODS

=head2 ->new(%settings)

Create an instance of the class, which can be custimized with the following parameters (values for keys of C<%settings>):

=head3 Customization and Behavior

lib/Batch/Interpreter.pm  view on Meta::CPAN

The list of handlers for file extensions. See chapter 'Extension Handlers' for the call interface.

=item internal_commands => $self->builtin_internal_commands

The list of handlers for internal commands, i.e. commands handled by CMD.EXE. See chapter 'Command Handlers' for the call interface.

=item external_commands => $self->builtin_external_commands

The list of handlers for external commands, i.e. command line tools. See chapter 'Command Handlers' for the call interface.

=item locale

The locale, see chapter LOCALES.

=back


=head3 Interpreter State

=over

=item echo => 1

lib/Batch/Interpreter.pm  view on Meta::CPAN

=item command

Return a new C<$command> to restart the handler search.

=back

=back

=head2 Locales

The locale class has to implement the following methods:

=head3 ->format_date($year, $month, $day)

Format a date for %DATE%, prompt $D, and the date command.

=head3 ->format_time_short($hour, $min)

Format a time for the time command.

=head3 ->format_time($hour, $min, $sec, $sec100)

lib/Batch/Interpreter.pm  view on Meta::CPAN

		my ($path) = @_;
		$path =~ y|\\|/|;
		return $path;
	}, sub {
		my ($path) = @_;
		$path = File::Spec->rel2abs($path);
		$path =~ y|\\|/|;
		return $path;
	});

	defined $self->{locale}
		or die "missing locale";
	if ('' eq ref(my $locale = $self->{locale})) {
		my $first_err;
		my $locale_class = "Batch::Interpreter::Locale::$locale";
		eval "require $locale_class";
		if ($@) {
			$first_err = $@;
			$locale_class = $locale;
			eval "require $locale_class";
		}
		$@ and die "couldn't load locale $locale:\n$first_err\n$@";
		$self->{locale} = $locale_class;
	}
	if (my @missing = grep !$self->{locale}->can($_), qw(
		format_date format_time_short format_time
		format_file_timedate format_file_timedate_for
		get_string
	)) {
		die "invalid locale, no handler for: ", join ', ', @missing;
	}

	return $self;
}

sub deep_clone {
	my ($self, @modifier) = @_;
	return bless clone({ %$self, @modifier }), ref $self;
}

sub get_message {
	my ($self, $message) = @_;
	return $self->{locale}->get_string('message', $message) // $message;
}

sub internal_error {
	my ($self, $message, @arg) = @_;
	return 'error: '.sprintf $message, @arg;
}

sub syn_error {
	my ($self, $message, @arg) = @_;
	return 'error: '.sprintf $self->get_message($message), @arg;

lib/Batch/Interpreter.pm  view on Meta::CPAN

	if ($uc_var eq 'PATHEXT') {
		%{$self->{var_path_cache}} = ();
	} else {
		delete $self->{var_path_cache}{$uc_var};
	}
}

sub get_date {
	my ($self) = @_;
	my @localtime = localtime;
	return $self->{locale}->format_date(
		$localtime[5]+1900, $localtime[4]+1, $localtime[3]
	);
}

sub get_time {
	my ($self, $short) = @_;

	my @time = gettimeofday;
	my @localtime = localtime $time[0];
	if ($short) {
		return $self->{locale}->format_time_short(
			$localtime[2], $localtime[1]
		);
	} else {
		return $self->{locale}->format_time(
			$localtime[2], $localtime[1],
			$localtime[0], $time[1]/10000
		);
	}
}


sub normalize_map {
	my ($map, $callback_unc, $callback_sys) = @_;
	for (0..$#$map) {

lib/Batch/Interpreter.pm  view on Meta::CPAN

}

sub get_file_timedate {
	my ($self, $syspath, $mode) = @_;
	my @localtime = localtime((stat $syspath)[9] // 0);
	my @timedate = (
		$localtime[5]+1900, $localtime[4]+1, $localtime[3],
		$localtime[2], $localtime[1],
	);
	return ($mode//'') eq 'for'
		? $self->{locale}->format_file_timedate_for(@timedate)
		: $self->{locale}->format_file_timedate(@timedate)
	;
}

sub format_size {
	my ($self, $size) = @_;
	1 while $size =~ s/(?<=\d)(\d\d\d)(?!\d)/.$1/;
	return $size;
}

sub set_subprocess_env {

lib/Batch/Interpreter/Locale/de_DE.pm  view on Meta::CPAN

package Batch::Interpreter::Locale::de_DE;

use v5.10;
use warnings;
use strict;

=head1 NAME

Batch::Interpreter::Locale::de_DE - German locale for Batch::Interpreter

=head1 SYNOPSIS

See Batch::Interpreter.

=cut

our $VERSION = 0.01;

sub format_date {

t/01-example.t  view on Meta::CPAN

	pass;
	exit;
}

	use Batch::Interpreter;

	open my $fh, '<:crlf', $ARGV[0]
		or die "$ARGV[0]: $!";

	my $rc = Batch::Interpreter->new(
		locale => 'de_DE',
		# more settings, see below
	)->run({}, [ <$fh> ], @ARGV);

is $rc, '0';



( run in 0.962 second using v1.01-cache-2.11-cpan-ceb78f64989 )