Perl

176 readers
1 users here now

founded 1 year ago
MODERATORS
1
2
3
4
5
6
7
2
fun with /dev/random (lemmy.sdf.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

I've never had a need to use /dev/random before, 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!nx
8
2
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

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 |

humpdays.pl

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
9
 
 

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!

10
2
Chaining Substitutions (lemmy.sdf.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

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 ...

11
 
 

At $work, we've been in the process of phasing out perl for a couple of years now, mostly in favor of go. There is still perl maintenance type work to do, and occasionally new perl code is written if it doesn't yet seem prudent to move any useful dependencies needed over to go, but I'm finding myself missing working in perl as much as I used to, and I don't currently have any side projects.

I have noticed a recent bit of activity around Padre. I've always been more of a back-end developer, so nearly everything there will be way out of my comfort zone, which seems like a good reason to see if I can dive in and learn anything in the process.

What perl things are you working on? I think this should be open to anything, even if its just a small toy, or things from $work you are allows to share, or anything else perl related.