#!/usr/bin/env perl # example.pl — Demonstrates core Perl features for Doom Emacs workflow. # Navigate: C-M-a/e (sub boundaries), SPC s i (imenu), gd (definition) # Format: SPC m f (perltidy), Run: SPC m r, Debug: SPC m d use strict; use warnings; use Carp qw(croak); # --- Subroutine with documentation --- =head2 parse_config($filename) Reads a simple key=value config file. Returns a hashref. =cut sub parse_config { my ($filename) = @_; croak "Filename required" unless defined $filename; my %config; open my $fh, '<', $filename or die "Cannot open '$filename': $!\n"; while (my $line = <$fh>) { chomp $line; next if $line =~ /^\s*(?:#|$)/; # skip comments and blanks if ($line =~ /^(\w+)\s*=\s*(.*)$/) { $config{$1} = $2; } } close $fh; return \%config; } # --- Array, hash, references --- my @languages = qw(Perl Python Go); my %scores = (Perl => 95, Python => 90, Go => 88); my $lang_ref = \@languages; my $score_ref = \%scores; printf "Languages: %s\n", join(', ', @{$lang_ref}); printf "Perl score: %d\n", $score_ref->{Perl}; # --- Regex demo --- my $text = 'Error: connection refused at 2024-01-15 10:30:00'; if ($text =~ /^(Error|Warning):\s+(.+?)\s+at\s+(\d{4}-\d{2}-\d{2})/) { printf "Level: %s, Message: %s, Date: %s\n", $1, $2, $3; } # --- Error handling with eval/die --- eval { my $cfg = parse_config('/tmp/nonexistent.cfg'); print "Loaded config\n"; }; if ($@) { warn "Caught error: $@"; } print "Done.\n";