blib/lib/Scalar/String.pm

filter: Statementstmt 86.7% 59 / 68 Branchbran 40.0% 4 / 10 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 84.6% 83 / 98 CC 3 SCAR 2.3
CC
3
Cov
81%
BEGIN
2.1
__ANON__
2.0
BEGIN
1.0
CRAP 1.3
↑ Summary lib/Scalar/String.xs »
1 =head1 NAME
2  
3 Scalar::String - string aspects of scalars
4  
5 =head1 SYNOPSIS
6  
7 use Scalar::String
8 qw(sclstr_is_upgraded sclstr_is_downgraded);
9  
10 if(sclstr_is_upgraded($value)) { ...
11 if(sclstr_is_downgraded($value)) { ...
12  
13 use Scalar::String qw(
14 sclstr_upgrade_inplace sclstr_upgraded
15 sclstr_downgrade_inplace sclstr_downgraded
16 );
17  
18 sclstr_upgrade_inplace($value);
19 $value = sclstr_upgraded($value);
20 sclstr_downgrade_inplace($value);
21 $value = sclstr_downgraded($value);
22  
23 =head1 DESCRIPTION
24  
25 This module is about the string part of plain Perl scalars. A scalar has
26 a string value, which is notionally a sequence of Unicode codepoints, but
27 may be internally encoded in either ISO-8859-1 or UTF-8. In places, and
28 more so in older versions of Perl, the internal encoding shows through.
29 To fully understand Perl strings it is necessary to understand these
30 implementation details.
31  
32 This module provides functions to classify a string by encoding and to
33 encode a string in a desired way.
34  
35 This module is implemented in XS, with a pure Perl backup version for
36 systems that can't handle XS.
37  
38 =head1 STRING ENCODING
39  
40 ISO-8859-1 is a simple 8-bit character encoding, which represents the
41 first 256 Unicode characters (codepoints 0x00 to 0xff) in one octet each.
42 This is how strings were historically represented in Perl. A string
43 represented this way is referred to as "downgraded".
44  
45 UTF-8 is a variable-width character encoding, which represents all
46 possible Unicode codepoints in differing numbers of octets. A design
47 feature of UTF-8 is that ASCII characters (codepoints 0x00 to 0x7f)
48 are each represented in a single octet, identically to their ISO-8859-1
49 encoding. Perl has its own variant of UTF-8, which can handle a wider
50 range of codepoints than Unicode formally allows. A string represented
51 in this variant UTF-8 is referred to as "upgraded".
52  
53 A Perl string is physically represented as a string of octets along with
54 a flag that says whether the string is downgraded or upgraded. At this
55 level, to determine the Unicode codepoints that are represented requires
56 examining both parts of the representation. If the string contains only
57 ASCII characters then the octet sequence is identical in either encoding,
58 but Perl still maintains an encoding flag on such a string. A string
59 is always either downgraded or upgraded; it is never both or neither.
60  
61 When handling string input, it is good form to operate only on the Unicode
62 characters represented by the string, ignoring the manner in which they
63 are encoded. Basic string operations such as concatenation work this way
64 (except for a bug in perl 5.6.0), so simple code written in pure Perl is
65 generally safe on this front. Pieces of character-based code can pass
66 around strings among themselves, and always get consistent behaviour,
67 without worrying about the way in which the characters are encoded.
68  
69 However, due to an historical accident, a lot of C code that interfaces
70 with Perl looks at the octets used to represent a string without also
71 examining the encoding flag. Such code gives inconsistent behaviour for
72 the same character sequence represented in the different ways. In perl
73 5.6, many pure Perl operations (such as regular expression matching)
74 also work this way, though some of them can be induced to work correctly
75 by using the L<utf8> pragma. In perl 5.8, regular expression matching
76 is character-based by default, but many I/O functions (such as C<open>)
77 are still octet-based.
78  
79 Where code that operates on the octets of a string must be used by code
80 that operates on characters, the latter needs to pay attention to the
81 encoding of its strings. Commonly, the octet-based code expects its
82 input to be represented in a particular encoding, in which case the
83 character-based code must oblige by forcing strings to that encoding
84 before they are passed in. There are other usage patterns too.
85  
86 You will be least confused if you think about a Perl string as a character
87 sequence plus an encoding flag. You should normally operate on the
88 character sequence and not care about the encoding flag. Occasionally you
89 must pay attention to the flag in addition to the characters. Unless you
90 are writing C code, you should try not to think about a string the other
91 way round, as an octet sequence plus encoding flag.
92  
93 =cut
94  
95 package Scalar::String;
96  
97 4 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 2 our $VERSION = "0.003";
102  
103 4 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 our @EXPORT_OK = qw(
105 sclstr_is_upgraded sclstr_is_downgraded
106 sclstr_upgrade_inplace sclstr_upgraded
107 sclstr_downgrade_inplace sclstr_downgraded
108 );
109  
110 2 eval { local $SIG{__DIE__};
111 2 require XSLoader;
112 2 XSLoader::load(__PACKAGE__, $VERSION);
113 };
114  
115 2 ▶if($@ eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ eq '') { } 2 0 2
if ($@ eq '') { } 0 2 2
116 0 close(DATA);
117 } else {
118 0 (my $filename = __FILE__) =~ tr# -~##cd;
119 0 local $/ = undef;
120 0 my $pp_code = "#line 128 \"$filename\"\n".<DATA>;
121 0 close(DATA);
122 {
123 0 local $SIG{__DIE__};
124 0 eval $pp_code;
125 }
126 0 ▶ die $@ if $@ ne "";
Branch: 2 branches
BranchTrueFalseTotal
if $@ ne "" 0 0 0
if $@ ne "" 0 2 2
127 }
128  
129 2 ▶1;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 ▶ 
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if "$]" >= "5.008" 2 0 2
131 __DATA__
↑ Summary lib/Scalar/String.xs »