File Coverage

bin/random-string
Criterion Covered Total %
statement 50 51 98.0
branch 20 22 90.9
condition n/a
subroutine 6 6 100.0
pod n/a
total 76 79 96.2


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 23     23   119728 use strict;
  23         39  
  23         1124  
4 23     23   126 use warnings;
  23         37  
  23         1517  
5 23     23   17851 use Getopt::Long qw(:config no_ignore_case);
  23         412421  
  23         154  
6 23     23   18532 use Pod::Usage;
  23         1931590  
  23         3473  
7 23     23   17254 use Data::Random::String::Matches;
  23         112  
  23         51889  
8              
9 23         4445473 our $VERSION = '0.02';
10              
11             # Parse command line options
12 23         316 my %opts = (
13             count => 1,
14             length => undef,
15             smart => 0,
16             unique => 0,
17             separator => "\n",
18             help => 0,
19             man => 0,
20             version => 0,
21             examples => 0,
22             );
23              
24             GetOptions(
25             'count|c=i' => \$opts{count},
26             'length|l=i' => \$opts{length},
27             'smart|s' => \$opts{smart},
28             'unique|u' => \$opts{unique},
29             'separator|S=s' => \$opts{separator},
30             'help|h|?' => \$opts{help},
31             'man|m' => \$opts{man},
32             'version|v' => \$opts{version},
33             'examples|e' => \$opts{examples},
34 23 50       343 ) or pod2usage(2);
35              
36             # Handle special options
37 23 100       34275 if ($opts{version}) {
38 1         6 print "random-string version $VERSION\n";
39 1         0 exit 0;
40             }
41              
42 22 100       97 pod2usage(1) if $opts{help};
43 21 100       87 pod2usage(-exitval => 0, -verbose => 2) if $opts{man};
44              
45 20 100       76 if ($opts{examples}) {
46 1         14 show_examples();
47 1         0 exit 0;
48             }
49              
50             # Get the pattern from command line
51 19         157 my $pattern = shift @ARGV;
52              
53 19 100       97 unless (defined $pattern) {
54 2         170 print STDERR "Error: Pattern required\n\n";
55 2         19 pod2usage(1);
56             }
57              
58             # Validate options
59 17 100       159 if ($opts{count} < 1) {
60 1         24 die 'Error: count must be at least 1';
61             }
62              
63             # Decode escape sequences in separator
64 16         87 $opts{separator} =~ s/\\n/\n/g;
65 16         59 $opts{separator} =~ s/\\t/\t/g;
66 16         44 $opts{separator} =~ s/\\r/\r/g;
67 16         45 $opts{separator} =~ s/\\0/\0/g;
68              
69             # Create generator
70 16         35 my $gen = eval {
71 16         295 Data::Random::String::Matches->new($pattern, $opts{length});
72             };
73              
74 16 50       54 if ($@) {
75 0         0 die "Error creating generator: $@";
76             }
77              
78             # Generate strings
79 16         30 my @results;
80 16         68 for (1 .. $opts{count}) {
81 25         45 my $str = eval {
82 25 100       142 $opts{smart} ? $gen->generate_smart() : $gen->generate();
83             };
84              
85 25 100       1002 if ($@) {
86 1         12 die "Error generating string: $@";
87             }
88              
89 24         128 push @results, $str;
90             }
91              
92             # Output results
93 15         332 print join($opts{separator}, @results);
94 15 100       136 print "\n" unless $opts{separator} =~ /\n$/;
95              
96 15         0 exit 0;
97              
98             sub show_examples {
99 1     1   6 print <<'EXAMPLES';
100             Examples:
101              
102             # Generate a single 4-digit number
103             random-string '\d{4}'
104              
105             # Generate 10 API keys
106             random-string 'AIza[0-9A-Za-z_-]{35}' --count 10
107              
108             # Generate 5 email addresses
109             random-string '[a-z]{5,10}@[a-z]{5,10}\.com' -c 5
110              
111             # Generate phone numbers with custom separator
112             random-string '\d{3}-\d{3}-\d{4}' -c 3 -S ', '
113              
114             # Generate uppercase letters (3 characters)
115             random-string '[A-Z]{3}'
116              
117             # Generate password-like strings
118             random-string '[A-Za-z0-9!@#$%]{16}' -c 5
119              
120             # Generate UUID-like strings
121             random-string '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
122              
123             # Use alternation
124             random-string '(cat|dog|bird)' -c 10
125              
126             # Use backreferences (repeating pattern)
127             random-string '(\w{3})-\1' -c 5
128              
129             # Generate credit card test numbers
130             random-string '4\d{15}' -c 3
131              
132             # Use smart mode for complex patterns (faster)
133             random-string '[A-Z]{3}\d{4}' -c 100 --smart
134              
135             Common patterns:
136             \d{4} - 4-digit PIN
137             [A-Z]{2}\d{3} - License plate style
138             \d{3}-\d{3}-\d{4} - Phone number
139             [a-z]+@[a-z]+\.com - Simple email
140             [A-Fa-f0-9]{32} - MD5 hash
141             [A-Za-z0-9]{20} - Random token
142             (foo|bar|baz) - One of three options
143             (\w{4})-\1 - Repeated pattern
144              
145             EXAMPLES
146             }
147              
148             __END__