File Coverage

blib/lib/XML/Char.pm
Criterion Covered Total %
statement 28 28 100.0
branch 5 6 83.3
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 41 42 97.6


line stmt bran cond sub pod time code
1             package XML::Char;
2              
3 2     2   33260 use utf8;
  2         4  
  2         12  
4              
5             =encoding UTF-8
6              
7             =head1 NAME
8              
9             XML::Char - validate characters for XML
10              
11             =head1 SYNOPSIS
12              
13             use XML::Char;
14             if (not XML::Char->valid("bell ".chr(7))) {
15             die 'no way to store this string directly to XML';
16             }
17            
18             use utf8;
19             use XML::Char;
20             if (XML::Char->valid("UTF8 je pořádný peklo")) {
21             print "fuf, we are fine\n";
22             }
23              
24             =head1 DESCRIPTION
25              
26             For me it was kind of a surprised to learn that C is a valid UTF-8
27             character. All of the 0-0x7F are...
28              
29             Emo: well it's not because that they are valid utf-8 characters that you have to expect XML to accept them
30              
31             Well of course not, now I know :-)
32              
33             L defines which characters XML processors MUST accept:
34              
35             [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
36             /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
37              
38             This module validates if a given string meets this criteria. In addition
39             the string has to be a Perl UTF-8 string (C - see L).
40              
41             =cut
42              
43 2     2   89 use 5.006;
  2         5  
  2         65  
44 2     2   7 use strict;
  2         6  
  2         54  
45 2     2   6 use warnings;
  2         7  
  2         89  
46              
47             our $VERSION = '0.03';
48              
49 2     2   900 use parent qw(DynaLoader);
  2         480  
  2         6  
50              
51 2     2   89 use Exporter 'import';
  2         3  
  2         320  
52             our @EXPORT_OK = qw(
53             );
54              
55             __PACKAGE__->bootstrap;
56              
57             =head2 valid($value)
58              
59             Returns true or false if C<$value> consists of valid UTF-8 XML characters.
60              
61             =cut
62              
63             sub valid {
64 26     26 1 20247 my ($self, $value);
65 26 100       55 if (@_ < 2) {
66 2         3 $self = __PACKAGE__;
67 2         3 $value = shift @_;
68             }
69             else {
70 24         28 $self = shift @_;
71 24         25 $value = shift @_;
72             }
73            
74             die 'bad usage'
75 26 50       31 if not eval { $self->can('valid') };
  26         132  
76            
77             # undef is valid
78 26 100       60 return 1
79             if not defined $value;
80            
81 25         119 return _valid_xml_string($value);
82             }
83              
84             1;
85              
86             __END__