root/lang/perl/dan/trunk/lib/dan.pm @ 3314

Revision 3314, 3.5 kB (checked in by yappo, 5 years ago)

lang/perl/dan: version number change, pod fix

Line 
1package dan;
2
3use 5.009005;
4use strict;
5use warnings;
6
7use Encode qw(find_encoding);
8
9our $VERSION = 0.551.1;
10
11our $SINGLETON = bless { code => {} }, __PACKAGE__;
12
13sub croak {
14    require Carp;
15    Carp::croak(__PACKAGE__ . ": @_");
16}
17
18my $LATIN1 = find_encoding('iso-8859-1')
19    or croak("Can't load latin-1");
20
21my $DEFAULT_ENCODING;
22my $DEFAULT_UTF8HINTBITS;
23my $utf8_hint_bits = 0x00800000;
24
25sub import {
26    my($class, %opts) = @_;
27
28    if (ref($opts{cat_decode} || '') eq 'CODE' && ! exists $opts{decode}) {
29        $opts{decode} = sub { shift };
30    }
31
32    # set hinthash
33    $^H{$class} = 'dan';
34
35    # set option
36    my $pkg = caller;
37    $SINGLETON->{code}->{$pkg} = \%opts;
38
39    # swapping to utf8 hint bits
40    $DEFAULT_UTF8HINTBITS = 0;
41    if ($opts{force} && $^H & $utf8_hint_bits) {
42        $DEFAULT_UTF8HINTBITS = 1;
43        $^H &= ~$utf8_hint_bits;
44    }
45
46    # swapping to encoding
47    $DEFAULT_ENCODING = ${^ENCODING};
48    ${^ENCODING} = $SINGLETON;
49}
50
51sub unimport {
52    my $class = shift;
53    undef $^H{$class};
54    my $pkg = caller;
55    delete $SINGLETON->{code}->{$pkg};
56
57    if ($DEFAULT_UTF8HINTBITS) {
58        $DEFAULT_UTF8HINTBITS = 0;
59        $^H |= $utf8_hint_bits;
60    }
61    ${^ENCODING} = $DEFAULT_ENCODING || ${^ENCODING};
62}
63
64
65sub is_dan {
66    my $level = $_[1] // 1;
67    my $hinthash = (caller($level))[10];
68    $hinthash->{"" . __PACKAGE__};
69}
70
71sub run {
72    my($self, $mode, $str, %opts) = @_;
73    my $level = $opts{level} // 1;
74    my $pkg = (caller($level))[0];
75    my $code = ($SINGLETON->{code}->{$pkg} || {})->{$mode} || '';
76    return $code if $opts{wantcode};
77
78    return '' unless ref($code) eq 'CODE';
79    return $code->($str);
80}
81
82# for DATA / END section
83sub name { $LATIN1->name }
84
85sub decode {
86    my $self = shift;
87    if ($self->is_dan) {
88        my($str) = @_;
89        $self->run( decode => $str );
90    } else {
91        $LATIN1->decode(@_);
92    }
93}
94
95sub cat_decode {
96    my $self = shift;
97
98    if ($self->is_dan) {
99        my(undef, undef, $idx, $quot) = @_;
100        my ( $rdst, $rsrc, $rpos ) = \@_[ 0, 1, 2 ];
101        my $pos = $idx;
102        while ((my $tmp = index $$rsrc, $quot, $pos) > 0) {
103            $pos = $tmp + 1;
104            last unless substr($$rsrc, $tmp - 1, 1) eq "\\";
105        }
106        $$rpos = $pos;
107
108        my $capt = substr($$rsrc, $idx, ($pos - $idx) - 1);
109        $$rdst = $self->run( cat_decode => $capt ) . $quot;
110        1;
111    } else {
112        $LATIN1->cat_decode(@_);
113    }
114}
115
1161;
117__END__
118
119=head1 NAME
120
121dan - The literal unread
122
123=head1 SYNOPSIS
124
125  use dan;
126  print "foo"; # not displaying
127  no dan;
128  print "foo"; # foo
129
130
131it is possible to solve it with force though there are utf8 pragma and no compatibility.
132
133  use utf8;
134  use dan force => 1;
135  print "foo"; # not displaying
136  no dan;
137  print "foo"; # foo
138
139=head1 DESCRIPTION
140
141dan is not Dan Kogai.
142dan the unread to literal strings.
143
144it is a present for perl 20 years old and 5.10 release commemoration.
145
146=head1 OPTIONS
147
148=over 4
149
150=item cat_decode
151
152  use dan cat_decode => sub {
153      my $str = shift;
154      $str =~ s/Jcode/Encode/;
155      $str;
156  };
157  print "Jcode";# Encode
158
159or
160
161  use utf8;
162  use dan force => 1, cat_decode => sub {
163      my $str = shift;
164      $str =~ s/Jcode/Encode/;
165      $str;
166  };
167  print "Jcode";# Encode
168
169=item force
170
171  use utf8;
172  use dan force => 1;
173  print "foo"; # not displaying
174
175=back
176
177=head1 AUTHOR
178
179Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
180
181=head1 LICENSE
182
183This library is free software; you can redistribute it and/or modify
184it under the same terms as Perl itself.
185
186=cut
Note: See TracBrowser for help on using the browser.