Perl: Matching multiple patterns with a regex

Perl has regular expressions built in to the core of the language and they are very powerful. It's easy enough to find a single match with a regexp:

# Find the *first* three letter word in the string
my $str = "one two three four five six seven eight nine ten";
my @x   = $str =~ m/\b(\w{3})\b/; # ("one")

If you want to find all of the three letter words you can add the g modifier to the end of your regex to tell it to match "globally".

# Find *all* the three letter words
my $str = "one two three four five six seven eight nine ten";
my @x   = $str =~ m/\b(\w{3})\b/g; # ("one", "two", "six", "ten")

You can also iterate on your global regexp if you want to get the matches one at a time:

my $str = "one two three four five six seven eight nine ten";
my @x   = ();
while ($str =~ m/\b(\w{3})\b/g) {
    push(@x, $1);
}

print join(",", @x); # "one,two,six,ten"
Leave A Reply

PHP: Disable PrivateTmp

Rocky Linux uses PrivateTmp to give each process it's own walled off section of /tmp/. This is good for security sometimes, but it can also lead to frustration because files written to /tmp/ or /var/tmp/ will not show up when you need to debug. To disable PrivateTmp for php-fpm you need to modify the systemd configuration for php-fpm. This is done by running systemctl edit php-fpm and adding a section that says:

[Service]
PrivateTmp=false

Once the change is in place you will need to systemctl daemon-reload and then restart systemctl restart php-fpm.

Note: Borrowed from Stack Overflow.

Leave A Reply

Kea DHCP4 systemd file

I'm using Kea as a DHCP server and I need the service to start on boot. I'm not sure why it doesn't ship with a systemd .service file. Here is the file I ended up using:

# Put this in /etc/systemd/system/kea-dhcp4.service
# Reload systemd so it picks it up `systemctl daemon-reload`
# Enable it on boot: `systemctl enable kea-dhcp4`

[Unit]
Description=ISC KEA IPv4 DHCP daemon
Documentation=man:kea-dhcp4(8)
Wants=network-online.target mariadb.service
Requires=kea-ctrl-agent.service
After=network-online.target mariadb.service mysql.service

[Service]
ExecStart=/usr/sbin/kea-dhcp4 -c /etc/kea/kea-dhcp4.conf

[Install]
WantedBy=multi-user.target

This is borrowed from Skywalker-11.

Leave A Reply

Basic snmpd.conf file to monitor ethernet ports of a Linux box

I need to monitor the Ethernet interfaces of a Linux VM. This is the perfect job for snmpd which you can get by installing net-snmp and then applying a basic config. Here is a simplified config that will get you basic read-only access for a community and subnet.

# /etc/snmp/snmpd.conf
rocommunity snmp-read 165.92.231.0/24
rocommunity snmp-read 10.3.1.0/24
rocommunity snmp-read localhost
syslocation "City, State"
syscontact  "Admin <user@domain.com>"

You can test your new SNMP configuration with snmpwalk

snmpwalk -v 2c -c snmp-read 127.0.0.1
Leave A Reply

Books of 2024

List of books I read in 2024. Also see the list of 2023. The date indicated denotes the date I started reading the book.

Drizzt novels

2024-01-08 - Homeland - Drizzt #1 by R. A. Salvatore - 343 pages
2024-02-06 - Exile - Drizzt #2 by R. A. Salvatore - 343 pages
2024-xx-xx - Sojourn - Drizzt #3 by R. A. Salvatore - 346 pages
2024-xx-xx - The Crystal Shard - Drizzt #4 by R. A. Salvatore - 344 pages
2024-xx-xx - Streams Of Silver - Drizzt #5 by R. A. Salvatore - 377 pages
2024-xx-xx - The Halfling's Gem - Drizzt #6 by R. A. Salvatore - 378 pages

Dark Tower novels (Paperback collection)

2024-01-13 - The Gunslinger: Revised - Dark Tower #1 by King, Stephen - 253 pages
2024-01-28 - The Gunslinger: Original - Dark Tower #1 by King, Stephen - 224 pages
2024-02-12 - The Drawing Of The Three - Dark Tower #2 by Stephen King - 459 pages
2024-03-14 - The Waste Lands - Dark Tower #3 by Stephen King - 606 pages
2024-xx-xx - Wizard and Glass - Dark Tower #4 by Stephen King - 893 pages
2024-xx-xx - The Wind Through The Keyhole - Dark Tower #4.5 by Stephen King - 309 pages
2024-xx-xx - Wolves Of The Calla - Dark Tower #5 by Stephen King - 709 pages
2024-xx-xx - Song Of Susannah - Dark Tower #6 by Stephen King - 411 pages
2024-xx-xx - The Dark Tower - Dark Tower #7 by Stephen King - 845 pages

Redwall novels

2024-01-16 - Redwall by Brian Jacques - 351 pages
2024-02-19 - Mossflower by Brian Jacques - 432 pages
2024-03-08 - Mattimeo by Brian Jacques - 446 pages
2024-xx-xx - Mariel Of Redwall by Brian Jacques - 387 pages
2024-xx-xx - Salamandastron by Brian Jacques - 391 pages
2024-xx-xx - Martin The Warrior by Brian Jacques - 376 pages

Various novels

2024-01-01 - Revival by Stephen King - 466 pages
2024-01-21 - White Death by Clive Cussler - 419 pages
2024-01-31 - Red Rising by Pierce Brown - 382 pages
2024-02-25 - Star Wars: Exile by Aaron Allston - 337 pages
2024-xx-xx - Empire Of Pain by Patrick Radden Keefe - 452 pages

Leave A Reply

Perl: How to profile Perl code to improve your code quality

If you've written some Perl and you want to improve upon the execution speed you can use a profiler. There are several profilers available, but the best one I've found is Devel::NYTProf. Once you have the module installed you run your Perl script as normal but invoke the profiler:

perl -d:NYTProf term-colors.pl

This will result in a nytprof.out file being created in the current directory. This file contains raw stats about function calls and code timings. You can turn this data into something human readable by converting it to HTML.

nytprofhtml nytprof.out --out perl-profile/

This will create a nice HTML page with all kinds of information about how the Perl interpreter ran your script. With this information hopefully you can find places in your code that could use improvement.

Leave A Reply

Keeping Apache and PHP from leaking version information

PHP and Apache like to advertise themselves in the HTTP headers of every connection. It's probably best not to advertise your versions. You can disable that with these configuration entries:

# In /etc/php.ini
expose_php = Off
# In your Apache config
ServerTokens Prod
ServerSignature Off
Leave A Reply

SuperGenPass is great

I'm a big fan of SuperGenPass so I decided to learn how it works. The algorithm is pretty simple so I decided to implement it in two of my favorite languages: PHP and Perl.

Hopefully this will help someone else trying to understand the concepts. Special thanks to Iannz for the great Bash implementation I based my code on.

Leave A Reply

Perl: Array of all regexp captures

Perl v5.25.7 added support for the @{^CAPTURE} variable which wrapped all the regexp parenthesis captures up into an array. If you need this functionality in an older version of Perl you can use this function:

my $str =  "Hello Jason Doolis";
$str    =~ /Hello (\w+) (\w+)/;

my @captures = get_captures(); # ("Jason", "Doolis")
sub get_captures {
    no strict 'refs';

    my $last_idx = scalar(@-) - 1;
    my @arr      = 1 .. $last_idx;
    my @ret      = map { $$_; } @arr;

    return @ret;
}
Leave A Reply

PHP: Creation of dynamic property Class::$Var is deprecated

I upgraded to PHP 8.2 today and started getting a lot of Creation of dynamic property Class::$Var is deprecated errors. This is just PHP's way of telling you that you need to pre-declare your class variables before you use them. To me this just meant adding a lot of:

class user {
    var $name  = "";
    var $id    = 0;
    var $perms = [];

to the tops of my classes. This is the right way to fix things, and what I did in 99% of the cases where I was seeing this error.

However I do have a couple of classes where it's necessary to be able to add dynamic properties on the fly, and PHP does offer a work around for this. If you use a special declaration immediately before your class statement PHP will allow dynamic properties to be added without throwing any errors.

#[\AllowDynamicProperties]
class user {

Note: The \ in the brackets is required

Leave A Reply

Arduino: Using an ATX power supply as a hobbyist power source

Running some RGB LEDs had me thinking about alternate sources of power. Specifically I need a mix of +5v and +12v to power different types of LEDs. Old ATX computer power supplies are a good/cheap source of power because they are easy to harvest from old PCs. They're also usually rated for high wattage.

After some research I found that you need to do a little work to turn on a PC power supply that's not hooked up to a motherboard. Specifically you need to short the green PS-ON pin to ground. This simulates pressing the power button on your PC and triggers the supply to turn on. You can tell the power supply is on when the fan starts to spin. This can be accomplished by cutting the green and (any) black wire out of the ATX connector and connecting them together.

Alternately the power supply will supply +5v of power even in off or standby mode if you use the purple +5vSB line. If all you need is a small amount (less than 2 amps) of 5v power using the +5vSB line is a great alternative and will be quieter because the fan will not be on.

Once the power supply is on you can use the other wires as follows:

Color Type
Black Ground
Orange +3.3v
Red +5v
Yellow +12v

Other pins are less useful but available:

Color Type
Green Power Supply On
Blue -12v
White -5v
Purple +5v standby

Be sure to check the amperage ratings for each voltage on the side of your power supply before use.

Leave A Reply

PHP: Serializing data to save it for caching

I need to cache some data to disk between PHP requests so I decided to compare the various methods available.

Using 100000 element array as source data

Serialize save: 2.958 ms (1.5M)
Serialize read: 5.447 ms

JSON save: 1.880 ms (574.96K)
JSON read: 6.876 ms

PHP save: 8.684 ms (1.7M)
PHP read: 26.863 ms

Memcache set: 5.651 ms
Memcache get: 2.465 ms

IGBinary save: 1.377 ms (720.08K)
IGBinary read: 2.245 ms

MsgPack save: 1.389 ms (359.92K)
MsgPack read: 2.930 ms

I was surprised to see IGBinary and MsgPack so much faster than native JSON. JSON is easy and super portable, but not the fastest.

Leave A Reply

Linux: repeatedly run a command to monitor output

If you need to repeatedly run a command and view the output changes over time then check out my cli_watch.pl script. I was watching a RAID array rebuild slowly and needed a way to see the progress over time.

Usage: cli_watch.pl [--delay 15] [--line 3] command

Run my_command every 15 seconds:

cli_watch.pl --delay 15 'my_command | grep stuff'

Filter output to only line 2:

cli_watch.pl --line 2 'ping -c 1 192.168.5.222'

Leave A Reply - 1 Reply

Perl: Add an element to the middle of an array

If you want to add an element to the middle of an existing array you can use the splice() function. Splice modifies arrays in place. Splice takes four arguments for this: the array to modify, the index of where you want to modify, the number of items you want to remove, and an array of the elements to add.

my @x = qw(one three);

# At the 2nd index, add (replace zero elements) a one element array
splice(@x, 1, 0, ('two'));

print join(" ", @x); # "one two three"
Leave A Reply

Perl: Get certain elements of an array the lazy way

I learned that you can extract various elements from a Perl array in a very creative/simple way. Using this syntax may simplify some of your code and save you a lot of time.

my @colors = ("red", "blue", "green", "yellow", "orange", "purple");

my @w = @colors[(0, 3)];    # ("red", "yellow");
my @x = @colors[(0, 2, 4)]; # ("red", "green", "orange");

# First and last element
my @y = @colors[(0, -1)];   # ("red", "purple");

# First ten items
my @z = @array[0 .. 10];    # Using the `..` range operator

Basically any call to an array where the payload is an array of indexes will return a new array with those items extracted.

my @colors = ("red", "blue", "green", "yellow", "orange", "purple");

# You can also use an array variable to specify the elements to extract
my @ids = (1,3,5);
my @x   = @colors[@ids]; # ("blue", "yellow", "purple")

Note: Since you are referencing the whole array (not one element) you use the @ sigil instead of $.

Leave A Reply