File Coverage

blib/lib/Data/GUID/URLSafe.pm
Criterion Covered Total %
statement 21 21 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
1 1     1   592 use strict;
  1         1  
  1         26  
2 1     1   4 use warnings;
  1         2  
  1         40  
3             package Data::GUID::URLSafe 0.007;
4             # ABSTRACT: url-safe base64-encoded GUIDs
5              
6             #pod =head1 SYNOPSIS
7             #pod
8             #pod use Data::GUID::URLSafe;
9             #pod
10             #pod my $guid = Data::GUID->new;
11             #pod
12             #pod my $string = $guid->as_base64_urlsafe;
13             #pod
14             #pod my $same_guid = Data::GUID->from_base64_urlsafe;
15             #pod
16             #pod This module provides methods for L that provide for
17             #pod URL-safe base64 encoded GUIDs, as described by
18             #pod L.
19             #pod
20             #pod These strings are also safer for email addresses. While the forward slash is
21             #pod legal in email addresses, some broken email address validators reject it.
22             #pod (Also, without the trailing equals signs, these strings will be shorter.)
23             #pod
24             #pod When Data::GUID::URLSafe is C'd, it installs methods into Data::GUID using
25             #pod L.
26             #pod
27             #pod =cut
28              
29 1     1   458 use Data::GUID ();
  1         16556  
  1         40  
30 1         6 use Sub::Exporter -setup => {
31             into => 'Data::GUID',
32             exports => [ qw(as_base64_urlsafe from_base64_urlsafe) ],
33             groups => [ default => [ -all ] ],
34 1     1   7 };
  1         2  
35              
36             #pod =method as_base64_urlsafe
37             #pod
38             #pod my $string = $guid->as_base64_urlsafe;
39             #pod
40             #pod This method returns the URL-safe base64 encoded representation of the GUID.
41             #pod
42             #pod =cut
43              
44             sub as_base64_urlsafe {
45 1     1 1 1854 my ($self) = @_;
46 1         4 my $base64 = $self->as_base64;
47 1         47 $base64 =~ tr{+/=}{-_}d;
48              
49 1         3 return $base64;
50             }
51              
52             #pod =method from_base64_urlsafe
53             #pod
54             #pod my $guid = Data::GUID->from_base64_urlsafe($string);
55             #pod
56             #pod =cut
57              
58             sub from_base64_urlsafe {
59 1     1 1 1356 my ($self, $string) = @_;
60              
61             # +/ should not be handled, so convert them to invalid chars
62             # also, remove spaces (\t..\r and SP) so as to calc padding len
63 1         3 $string =~ tr{-_\t-\x0d }{+/}d;
64 1 50       5 if (my $mod4 = length($string) % 4) {
65 1         3 $string .= substr('====', $mod4);
66             }
67              
68 1         4 return $self->from_base64($string);
69             }
70              
71             1;
72              
73             __END__