BATsh

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

    existing behaviour is preserved exactly. Explicit selection:

      BATsh->run($file, encoding => 'cp932');
      BATsh->run_string($src, encoding => 'sjis');
      BATsh->set_encoding('cp932');
      set BATSH_ENCODING=cp932        (environment variable)
      perl lib/BATsh.pm --encoding=cp932 script.batsh

    Supported: cp932/sjis, gbk/cp936, uhc/cp949, big5/cp950 (all
    sharing the same trail-byte hazard), utf8, none, auto. A UTF-8
    BOM on the first line is stripped.

  - Character semantics for substring/length operators under the
    guard: ${#VAR}, ${VAR:N:L}, ${VAR:N} (SH) and %VAR:~n,m% (CMD)
    now count CP932 CHARACTERS, not bytes, via BATsh::MB::mb_length
    / mb_substr. Byte semantics are unchanged while the guard is
    inactive (ASCII/UTF-8 operation identical to 0.06).

  - Modulino command line (perl lib/BATsh.pm ...): new options
    --encoding=ENC and --version; script arguments after the script
    name are now passed through as %1..%9 / %* (previously they

README  view on Meta::CPAN

    unaffected. Explicit selection:

      BATsh->run($file, encoding => 'cp932');   # per run
      BATsh->set_encoding('cp932');             # for the process
      set BATSH_ENCODING=cp932                  # environment variable
      perl lib/BATsh.pm --encoding=cp932 script.batsh

    Supported names: cp932 (sjis), gbk (cp936), uhc (cp949), big5 (cp950),
    utf8, none, auto. Under an active DBCS encoding the substring and length
    operators "${#VAR}", "${VAR:N:L}" and "%VAR:~n,m%" count characters
    rather than bytes. A UTF-8 BOM on the first line is stripped. See
    BATsh::MB for the mechanism.

EXIT STATUS
    "run", "run_string" and "run_lines" return the script's final exit
    status as an integer: the argument of SH "exit N" or CMD "EXIT [/B] N"
    if one was executed, otherwise the status of the last command. "EXIT"
    with no code keeps the current "ERRORLEVEL" (so "false" then "EXIT /B"
    returns 1). The same value is available afterwards as
    "BATsh->last_status".

lib/BATsh.pm  view on Meta::CPAN

        if !defined($enc) && defined($class_or_self)
        && $class_or_self !~ /\ABATsh\b/;
    $_ENV_ENCODING_APPLIED = 1;   # explicit choice beats BATSH_ENCODING
    return BATsh::MB::set_encoding($enc);
}

sub encoding { return BATsh::MB::encoding() }

###############################################################################
# _prepare_source -- per-run encoding setup on the raw script lines
#   1. strip a UTF-8 BOM from the first line
#   2. apply an explicit per-run encoding, or BATSH_ENCODING (once),
#      or leave the current ('auto' by default) setting in place
#   3. under 'auto', detect the source encoding and activate the guard
#   4. guard-transform every line in place (identity when inactive)
###############################################################################
sub _prepare_source {
    my ($lines_ref, $encoding) = @_;
    $lines_ref->[0] = BATsh::MB::strip_bom($lines_ref->[0]) if @{$lines_ref};
    if (defined $encoding && $encoding ne '') {
        BATsh::MB::set_encoding($encoding);

lib/BATsh.pm  view on Meta::CPAN

UTF-8 scripts are unaffected.  Explicit selection:

  BATsh->run($file, encoding => 'cp932');   # per run
  BATsh->set_encoding('cp932');             # for the process
  set BATSH_ENCODING=cp932                  # environment variable
  perl lib/BATsh.pm --encoding=cp932 script.batsh

Supported names: cp932 (sjis), gbk (cp936), uhc (cp949), big5
(cp950), utf8, none, auto.  Under an active DBCS encoding the
substring and length operators C<${#VAR}>, C<${VAR:N:L}> and
C<%VAR:~n,m%> count characters rather than bytes.  A UTF-8 BOM on
the first line is stripped.  See L<BATsh::MB> for the mechanism.

=head1 EXIT STATUS

C<run>, C<run_string> and C<run_lines> return the script's B<final exit
status> as an integer: the argument of SH C<exit N> or CMD C<EXIT [/B] N>
if one was executed, otherwise the status of the last command.  C<EXIT>
with no code keeps the current C<ERRORLEVEL> (so C<false> then C<EXIT /B>
returns 1).  The same value is available afterwards as
C<BATsh-E<gt>last_status>.

lib/BATsh/MB.pm  view on Meta::CPAN

            push @c, $b;
            $i++;
        }
    }
    return [ @c ];
}

# ----------------------------------------------------------------
# strip_bom: remove a UTF-8 byte-order mark from the head of the
# first script line (harmless everywhere, required for editors that
# save UTF-8-with-BOM).
# ----------------------------------------------------------------
sub strip_bom {
    my $s = defined $_[0] && $_[0] !~ /\ABATsh::MB\z/ ? $_[0] : $_[1];
    return $s unless defined $s;
    $s =~ s/\A\xef\xbb\xbf//;
    return $s;
}

1;



( run in 1.175 second using v1.01-cache-2.11-cpan-364913b4093 )