🌉 bridged from @dark776174657273@lemmy.sdf.org by Tidepool
umbraklat
@dark776174657273.lemmy.sdf.org.ap.tdpl.io
fun with /dev/random
I've never had a need to use
/dev/randombefore, but then I saw this in some documentation I was reading:$ head -c20 /dev/random | base64 ZZXB6yF9bZvp103CI3lcREcBVEA=
Which got me thinking about my password generator. This is trivial, but I thought it was fun.
better-password-gen.pl
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use feature qw< say >; my @CList = ('a'..'z', 'A'..'Z', 0..9, split(//, q<\<\>\\/()[]{}:;"'`~#!@$%^&*-_=+>)); my $pwdLen = shift @ARGV // 12; my $BUFF = "\x0" x $pwdLen; open( my $RND, '<', '/dev/random' ) or die $!; for (0..9){ read $RND, $BUFF, $pwdLen; say join('', map {$CList[ord($_) % @CList]} split(//, $BUFF)); } close($RND);output:
$ ./better-password-gen.pl 25 M6V/$U(CGQ\p!d\lg=9>GpO[x hlAN*"XiXnShRy)DB6GOCv;7j NRXqeqSCZ^I\Tt2Bore4t1hYX ~4dIXVo+opRJfu6'b}u'y=N$: 6he=<mYdH'{P>Z4nDlfe4fk1T Ffv28dOEu1fZxr9<VMhl$nS"n yt[ol0;me'Rd2'6#ZD7!7plf[ =LZ^68!EOtP6EYJNq^;3T48HJ V<3fcA%\b@:+"wghSX^)\S/cH *1q08-+Zo`PHibJA<b)oeM!nxFun with Calendar::Simple
Calendar::Simple takes a few parameters and returns an array of array refs representing the weeks and days of the month. That's it! You can use it to display text calendars, or find all of the dates for a particular day of the week for a year, or use with an HTML builder module to create a calendar web page, or whatever you want. One caveat, which is in the documentation though I missed it, is that weeks start on Monday by default. You need that third param to specify Sunday if you want that. I also used enum and Text::Reform Do you have any modules you've found that really get the old brain juices flowing? july-thru-december.pl
| July | | Mo | Tu | We | Th | Fr | Sa | Su | | | | | | | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | 31 | | | | | | | | August | | Mo | Tu | We | Th | Fr | Sa | Su | | | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | | | | | September | | Mo | Tu | We | Th | Fr | Sa | Su | | | | | | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | 26 | 27 | 28 | 29 | 30 | | | October | | Mo | Tu | We | Th | Fr | Sa | Su | | | | | | | | 1 | | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 23 | 24 | 25 | 26 | 27 | 28 | 29 | | 30 | 31 | | | | | | | November | | Mo | Tu | We | Th | Fr | Sa | Su | | | | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 20 | 21 | 22 | 23 | 24 | 25 | 26 | | 27 | 28 | 29 | 30 | | | | | December | | Mo | Tu | We | Th | Fr | Sa | Su | | | | | | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
All Wednesday's in 2023 Jan: 4, 11, 18, 25 Feb: 1, 8, 15, 22 Mar: 1, 8, 15, 22, 29 Apr: 5, 12, 19, 26 May: 3, 10, 17, 24, 31 Jun: 7, 14, 21, 28 Jul: 5, 12, 19, 26 Aug: 2, 9, 16, 23, 30 Sep: 6, 13, 20, 27 Oct: 4, 11, 18, 25 Nov: 1, 8, 15, 22, 29 Dec: 6, 13, 20, 27
metacpan.org
Data::CTable - Read, write, manipulate tabular data
OK, why didn't anyone tell me about this module? I do a lot of work with CSV and TSV files, usually pulling and transforming reports. My usual goto modules are Text::CSV::Slurp and when that's not enough, I use Text::CSV. Then, I do all of the data manipulation myself. I'm finding that Data::CTable makes reading, modifying, and writing tabular data files almost trivially easy. I can act on the data row-by-row (of course) but also column-by-column. I can add, delete, re-arrange, and rename columns. I can select a subset of rows and then perform actions, like calculating the value of a new column, only on the selected rows. I can load two files and merge columns into a third, writing the result as a new CSV. I wasn't even looking for this. I had been searching metacpan for "fixed width template" (which was satisfied with Text::Reform) when I stumbled on this module. The irony, is that it's exactly what I needed for a current work report due tomorrow!
metacpan.org
Chaining Substitutions
I was recently reading through Perl Regular Expression Tutorial (perlretut) when I found the section on using the 'r' option. I'd seen this before but rarely had any reason to use it. Then it occurred to me that you could chain these together. I've been programming Perl for 30+ years and don't think I've ever seen this before. Here's an example that shows how I would usually do this, i.e. a series of
var =~ s///lines, and how to do the same thing in one line while initializing a variable.#!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $T = "one two trash THREE random"; my $T2 = $T; $T2 =~ s/trash|random//g; $T2 =~ s/(THREE)/lc($1)/e; $T2 =~ s/\s+/ /g; $T2 =~ s/^/Count with me: /; my $T3 = $T =~ s/trash|random//gr =~ s/(THREE)/lc($1)/er =~ s/\s+/ /gr =~ s/^/Count with me: /r; say "T := $T\nT2 := $T2\nT3 := $T3";
Output:
$ ./perl-subst-test.pl T := one two trash THREE random T2 := Count with me: one two three T3 := Count with me: one two three
Anyways, I had to tell someone ...