File Coverage

blib/lib/autolocale.pm
Criterion Covered Total %
statement 25 39 64.1
branch 1 2 50.0
condition n/a
subroutine 9 10 90.0
pod n/a
total 35 51 68.6


line stmt bran cond sub pod time code
1             package autolocale;
2 2     2   62469 use strict;
  2         5  
  2         65  
3 2     2   9 use warnings;
  2         3  
  2         46  
4 2     2   40 use 5.010000;
  2         9  
  2         145  
5 2     2   769 use POSIX qw(setlocale LC_ALL);
  2         6953  
  2         10  
6              
7             our $VERSION = '0.07';
8              
9             my $wiz = wizard(
10             set => sub {
11             my $hinthash = ( caller(0) )[10];
12             return unless $hinthash->{"autolocale"};
13             my $arg = shift;
14             if ( ref $arg ne 'SCALAR' ) {
15             die q{You must store scalar data to %ENV};
16             }
17             my $locale = ${$arg};
18             return unless $locale;
19             setlocale( LC_ALL, $locale );
20             return;
21             }
22             );
23              
24             BEGIN {
25 2     2   1517 use strict;
  2         2  
  2         78  
26 2     2   10 use warnings;
  2         4  
  2         451  
27 2 50   2   4 if ( eval { require Variable::Magic; 1 } ) {
  2         2270  
  2         2610  
28 2         315 Variable::Magic->import(qw/wizard cast/);
29             }
30             else {
31             # Fallback Pure-Perl mode when can't use Variable::Magic
32             {
33 0         0 package # Hiding package
34             autolocale::Tie::Scalar;
35 0         0 require Tie::Scalar;
36 0         0 our @ISA = qw(Tie::StdScalar);
37              
38             sub STORE {
39 0     0   0 my ( $self, $value ) = @_;
40 0         0 ${$self} = $value;
  0         0  
41 0         0 @_ = ( \$value );
42 0         0 goto $wiz;
43             }
44             }
45              
46             *wizard = sub {
47 0         0 my ( undef, $handler ) = @_;
48 0         0 return $handler;
49 0         0 };
50              
51             *cast = sub (\$$) {
52 0         0 my $target = shift;
53 0         0 tie $$target, 'autolocale::Tie::Scalar';
54 0         0 };
55             }
56             }
57              
58             sub import {
59 4     4   35 $^H{"autolocale"} = 1;
60 4         2112 cast $ENV{"LANG"}, $wiz;
61             }
62              
63             sub unimport {
64 1     1   64 $^H{"autolocale"} = 0;
65             }
66              
67             1;
68             __END__