Perl

From G1Joshi/Agent-Skills · View interactive version

Perl is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Perl assistance covering regular expressions, hashes, CPAN modules, and text processing. Use when maintaining legacy Perl scripts, performing complex regex manipulations, or writing sysadmin tools.

How to use this skill

Skill file

Perl

Perl is a highly capable, feature-rich programming language renowned for powerful regular expression processing, system administration automation, and robust text transformation.

When to Use

Quick Start

#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';

my %scores = ( Alice => 95, Bob => 88, Charlie => 92 );

for my $name (sort keys %scores) {
    say "$name scored $scores{$name}";
}

Core Concepts

Regular Expressions as First-Class Language Primitives

Perl regular expressions are deeply integrated into language syntax:

use strict;
use warnings;
use v5.36;

my $log_entry = '[2026-09-27 10:15:30] ERROR: Payment gateway timeout (code 504)';

if ($log_entry =~ /\[(.*?)\]\s+(\w+):\s+(.*)/) {
    my ($timestamp, $severity, $message) = ($1, $2, $3);
    say "Timestamp: $timestamp";
    say "Severity:  $severity";
    say "Message:   $message";
}

Modern Perl (v5.36+ / v5.38+) Native Subroutine Signatures

Replaces manual @_ unpacking with native typed signatures and experimental class features:

use v5.38;
use experimental 'class';

class Account {
    field $balance :param = 0;

    method deposit($amount) {
        $balance += $amount;
        return $balance;
    }

    method get_balance() { return $balance; }
}

my $acc = Account->new(balance => 100);
$acc->deposit(50);
say "Balance: ", $acc->get_balance(); # 150

Context Awareness (Scalar vs List Context)

Functions dynamically change return values based on whether a scalar or list is expected:

my @items = ("apple", "banana", "cherry");
my $count = @items;     # Scalar context returns array count: 3
my ($first) = @items;   # List context returns first element: "apple"

Common Patterns

Robust File Parsing with Regular Expressions

Problem: Extracting structured fields from inconsistent log files.

Solution:

Use strict mode with named regex capture groups:

use strict;
use warnings;
use feature 'say';

while (my $line = <DATA>) {
    chomp $line;
    if ($line =~ /^\[(?<timestamp>[^\]]+)\]\s+(?<level>INFO|WARN|ERROR)\s+(?<msg>.*)$/) {
        say "Time: $+{timestamp} | Level: $+{level} | Message: $+{msg}";
    }
}

__DATA__
[2025-03-01 12:00:00] INFO Server started successfully
[2025-03-01 12:01:23] ERROR Database connection failed

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------------- | :---------------------------------------------------------------------- | :----------------------------------------------------------------- |

| Global symbol "..." requires explicit package name | Variable used without prior my, our, or state under use strict. | Declare variable with my $var. |

| Can't locate ... in @INC | Required CPAN module not installed in Perl lib path. | Install module using cpanm Module::Name. |

| Modification of a read-only value attempted | Attempting to modify constant or $_ aliased to literal string. | Assign to local mutable variable my $copy = $_ before modifying. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.