Decode::Source - Run scripts written in encodings other than utf-8


Decode-Source documentation Contained in the Decode-Source distribution.

Index


Code Index:

NAME

Top

Decode::Source - Run scripts written in encodings other than utf-8

SYNOPSIS

Top

    use Decode::Source "iso-8859-1";
       ... code written in ISO-8859-1 ...
    use Decode::Source "cp-850";
       ... code written in DOS codepage 850 ...
    no Decode::Source;
        ... code written in US-ASCII ...

ABSTRACT

Top

Use alternative encodings/charsets for your program code. Perl 5.8 or higher is required for use of this module.

DESCRIPTION

Top

This code is so far only tested on Win32 platforms!

Decode::Source makes it possible to write perl programs in any script or encoding supported by the Encode module. Variable names can contain non-ASCII characters, just as when you use the use utf8 pragma. All theese characters, both in identifiers and string literals, will be decoded to perl's internal utf-8 form, before execution.

The syntax are similar to use utf8 and no utf8, but Decode::Source also takes an optional argument with source encoding. This argument can be any argument that Encode's decode function accept as a valid encoding. See also Encode.

EXAMPLE

Top

  use Decode::Source "windows-1252";

  $åke   = ["Åke Braun", "08-555 55 55"];
  $örjan = ["Örjan Älg", "08-555 55 54"];

  binmode STDOUT, ":encodings(cp850)";

  printf "Name: %-20s   Phone: %12s\n", @$_ for $åke, $örjan;

SEE ALSO

Top

Encode, Encode::Supported, utf8

AUTHOR

Top

Magnus Håkansson, <mailto:magnus@mbox604.swipnet.se>

COPYRIGHT AND LICENSE

Top


Decode-Source documentation Contained in the Decode-Source distribution.

package Decode::Source;

use 5.008;
use strict;
use Filter::Util::Call;
use Encode;

our $VERSION = '1.01';

our $filter_is_on = 0;

sub import {
	my $pkg = shift;
	my $enc = "utf8";
	$enc = shift if @_;
	filter_del() if $filter_is_on++;
	filter_add({_encoding => $enc});
	return 1;
}

sub unimport {
	filter_del(); 
	$filter_is_on = 0;
}

sub filter {
	my $obj = shift;
	my $ok = filter_read();
	$_ = decode $obj->{_encoding}, $_ if $ok > 0;
	$_ = "use utf8;$_" unless $obj->{_lines}++;
	$obj->{_lines} = 0 if s/(no\s+Decode::Source)/no utf8;$1/go;
	return $ok;
}

1;