blib/lib/Math/Random/example.pl untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 182 CC 11 SCAR 33.1
CC
11
Cov
0%
color
72.0
trim
6.0
file_get_contents
6.0
CRAP 27.4
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5 use v5.16;
6  
7 ###############################################################################
8 ###############################################################################
9  
10 use Math::Random::Xoshiro256;
11  
12 my $iter = int($ARGV[0] || 10);
13  
14 print color('yellow');
15 say "Version: " . $Math::Random::Xoshiro256::VERSION . "\n";
16 print color('reset');
17  
18 my $prng = Math::Random::Xoshiro256->new();
19 #$prng->seed(12345);
20  
21 for (1 .. $iter) {
22 my $num = $prng->rand64();
23 print "Rand64: $num\n";
24 }
25  
26 my @nums;
27 for (1 .. $iter) {
28 my $num = $prng->random_int(-10, 20);
29 push(@nums, $num);
30 }
31  
32 print "\n";
33 print "Range (-10, 20): " . join(", ", @nums) . "\n";
34  
35 print "\n";
36 print "Bytes: 0x" . unpack("h*", $prng->random_bytes($iter)) . "\n";
37  
38 print "\n";
39 @nums = ();
40 for (1 .. $iter) {
41 my $num = $prng->random_float();
42 print "Float: $num\n";
43 }
44  
45 print "\n";
46 my @orig = qw(red green blue white black brown yellow purple pink);
47 for (1 .. $iter) {
48 print "Array: " . join(", ", $prng->shuffle_array(@orig)) . "\n";
49 }
50  
51 ###############################################################################
52 ###############################################################################
53  
54 sub perl_rand64 {
55 my $high = int(rand() * 2**32 - 1);
56 my $low = int(rand() * 2**32 - 1);
57  
58 my $ret = ($high << 32) | $low;
59  
60 return $ret;
61 }
62  
63 sub trim {
64 my ($s) = (@_, $_); # Passed in var, or default to $_
65 if (!defined($s) || length($s) == 0) { return ""; }
66 $s =~ s/^\s*//;
67 $s =~ s/\s*$//;
68  
69 return $s;
70 }
71  
72 # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
73 sub color {
74 my ($str, $txt) = @_;
75  
76 # If we're NOT connected to a an interactive terminal don't do color
77 if (-t STDOUT == 0) { return $txt || ""; }
78  
79 # No string sent in, so we just reset
80 if (!length($str) || $str eq 'reset') { return "\e[0m"; }
81  
82 # Some predefined colors
83 my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
84 $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
85  
86 # Get foreground/background and any commands
87 my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g;
88 my ($bc) = $str =~ /on_(\d{1,3})$/g;
89  
90 if (defined($fc) && int($fc) > 255) { $fc = undef; } # above 255 is invalid
91  
92 # Some predefined commands
93 my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
94 my $cmd_num = $cmd_map{$cmd // 0};
95  
96 my $ret = '';
97 if ($cmd_num) { $ret .= "\e[${cmd_num}m"; }
98 if (defined($fc)) { $ret .= "\e[38;5;${fc}m"; }
99 if (defined($bc)) { $ret .= "\e[48;5;${bc}m"; }
100 if (defined($txt)) { $ret .= $txt . "\e[0m"; }
101  
102 return $ret;
103 }
104  
105 sub file_get_contents {
106 open(my $fh, "<", $_[0]) or return undef;
107 binmode($fh, ":encoding(UTF-8)");
108  
109 my $array_mode = ($_[1]) || (!defined($_[1]) && wantarray);
110  
111 if ($array_mode) { # Line mode
112 my @lines = readline($fh);
113  
114 # Right trim all lines
115 foreach my $line (@lines) { $line =~ s/[\r\n]+$//; }
116  
117 return @lines;
118 } else { # String mode
119 local $/ = undef; # Input rec separator (slurp)
120 return my $ret = readline($fh);
121 }
122 }
123  
124 sub file_put_contents {
125 my ($file, $data) = @_;
126  
127 open(my $fh, ">", $file) or return undef;
128 binmode($fh, ":encoding(UTF-8)");
129 print $fh $data;
130 close($fh);
131  
132 return length($data);
133 }
134  
135 # Creates methods k() and kd() to print, and print & die respectively
136 BEGIN {
137 if (eval { require Data::Dump::Color }) {
138 *k = sub { Data::Dump::Color::dd(@_) };
139 } else {
140 require Data::Dumper;
141 *k = sub { print Data::Dumper::Dumper(\@_) };
142 }
143  
144 sub kd {
145 k(@_);
146  
147 printf("Died at %2\$s line #%3\$s\n",caller());
148 exit(15);
149 }
150 }
151  
152 # vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4
153