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   979 use strict;
  1         3  
  1         29  
2 1     1   4 use warnings;
  1         2  
  1         155  
3             package Data::GUID::URLSafe 0.008;
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   586 use Data::GUID ();
  1         16061  
  1         41  
30 1         5 use Sub::Exporter -setup => {
31             into => 'Data::GUID',
32             exports => [ qw(as_base64_urlsafe from_base64_urlsafe) ],
33             groups => [ default => [ -all ] ],
34 1     1   9 };
  1         3  
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 2179 my ($self) = @_;
46 1         4 my $base64 = $self->as_base64;
47 1         74 $base64 =~ tr{+/=}{-_}d;
48              
49 1         5 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 1316 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       6 if (my $mod4 = length($string) % 4) {
65 1         3 $string .= substr('====', $mod4);
66             }
67              
68 1         5 return $self->from_base64($string);
69             }
70              
71             1;
72              
73             __END__