File Coverage

blib/lib/String/FixedLen.pm
Criterion Covered Total %
statement 12 12 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1             # String::FixedLen.pm
2             #
3             # Copyright (c) 2007 David Landgren
4             # All rights reserved
5              
6             =head1 NAME
7              
8             String::FixedLen - Create strings that will never exceed a specific length
9              
10             =head1 VERSION
11              
12             This document describes version 0.02 of String::FixedLen, released
13             2007-08-03.
14              
15             =head1 SYNOPSIS
16              
17             use String::FixedLen;
18              
19             tie my $str, 'String::FixedLen', 4;
20              
21             $str = 'a';
22             $str .= 'cheater; # "ache"
23             $str = "hello, world\n"; # "hell"
24             $str = 9999 + 12; # "1001"
25              
26             # and so on
27              
28             =head1 DESCRIPTION
29              
30             C is used to create strings that can never exceed a fixed length.
31             Whenever an assignment would cause the string to exceed the limit, it is clamped
32             to the maximum length and the remaining characters are discarded.
33              
34             =head1 DIAGNOSTICS
35              
36             None.
37              
38             =head1 NOTES
39              
40             The source scalar that is being assigned to a String::FixedLen
41             scalar may be huge:
42              
43             my $big = 'b' x 1_000_000;
44             $fixed = $big;
45              
46             but at no point will the FixedLen string ever exceed its upper limit.
47              
48             =head1 BUGS
49              
50             Please report all bugs at
51             L
52              
53             Make sure you include the output from the following two commands:
54              
55             perl -MString::FixedLen -le 'print $String::FixedLen::VERSION'
56             perl -V
57              
58             =head1 ACKNOWLEDGEMENTS
59              
60             The idea for this module came up during a discussion on the French
61             perl mailing list (perl@mongueurs.net).
62              
63             =head1 AUTHOR
64              
65             David Landgren, copyright (C) 2007. All rights reserved.
66              
67             http://www.landgren.net/perl/
68              
69             If you (find a) use this module, I'd love to hear about it. If you
70             want to be informed of updates, send me a note. You know my first
71             name, you know my domain. Can you guess my e-mail address?
72              
73             =head1 LICENSE
74              
75             This library is free software; you can redistribute it and/or modify
76             it under the same terms as Perl itself.
77              
78             =cut
79              
80             package String::FixedLen;
81              
82 1     1   38746 use strict;
  1         2  
  1         54  
83              
84 1     1   6 use vars '$VERSION';
  1         2  
  1         301  
85             $VERSION = '0.02';
86              
87             sub TIESCALAR {
88 1     1   13 my $class = shift;
89 1         2 my $len = shift;
90 1         7 return bless { s => undef, len => $len}, $class;
91             }
92              
93             sub STORE {
94 9     9   2926 my $self = shift;
95 9 100       69 $self->{s} = length $_[0] > $self->{len}
96             ? substr($_[0], 0, $self->{len})
97             : $_[0]
98             ;
99             }
100              
101             sub FETCH {
102 14     14   3749 $_[0]->{s};
103             }
104              
105             1;