Perl Expect Bindings - A Simple Example

I ran into a situation recently where I needed a Perl script to launch another program, wait for it to finish, and move on. Nothing fancy. A simple spawn-and-wait. But doing it the naive way — backticks, pipe reads, polling — felt like overkill for what should be a two-liner. That’s where the Expect module comes in. It’s been around since the 90s, originally ported from the Tcl expect tool, and it does exactly what you’d expect: it spawns a process, talks to its stdin, reads its stdout, and waits for specific patterns. For simple cases it’s overkill. For anything that involves an interactive prompt or a non-trivial exit condition, it saves you from writing your own state machine. ...

Getting System Load in Perl

Use qx() to capture the output of uptime and parse the load averages: # 1-minute load average my $load1 = qx(uptime | awk -F "load average: " '{ print $2 }' | cut -d, -f1); # 5-minute load average my $load5 = qx(uptime | awk -F "load average: " '{ print $2 }' | cut -d, -f2); # 15-minute load average my $load15 = qx(uptime | awk -F "load average: " '{ print $2 }' | cut -d, -f3); Note: use qx() not exec() – exec replaces the current process and doesn’t return.

Gearman Perl "syswrite on an undefined value" Error

If you get this error when running Gearman client code: Can't call method "syswrite" on an undefined value at /usr/local/share/perl/5.10.1/Gearman/Taskset.pm line 202. The fix is to specify the port explicitly. Change: $client->job_servers('127.0.0.1'); To: $client->job_servers('127.0.0.1:4730');

Distributing Work with Gearman and Perl

I’ve been playing with Gearman over the past few weeks and I’m impressed by how little friction there is between the idea of distributing work across machines and actually making it happen. The Perl module, Gearman::Client and Gearman::Worker, is straightforward enough that you can have a client and a worker talking to each other through a job server in under an hour. The basic model is simple. You have a client that submits jobs, a worker that performs them, and a job server sitting in the middle dispatching work to whichever worker is free. The client and worker can be written in different languages, run on different machines, and you don’t need to worry about the networking yourself — Gearman handles all of that through TCP sockets to the job server. ...

Reading a File into a Variable in Perl

For large files, consider File::Slurp. It’s faster than the conventional approaches: # Slurp the whole file { local $/ = undef; open my $fh, '<', 'myfile' or die "Couldn't open file: $!"; binmode $fh; my $string = <$fh>; close $fh; } Without binmode: { local $/ = undef; open my $fh, '<', 'myfile' or die "Couldn't open file: $!"; my $string = <$fh>; close $fh; } Join the lines: open my $fh, '<', 'myfile' or die "Couldn't open file: $!"; my $string = join('', <$fh>); close $fh; Append in a loop: open my $fh, '<', 'myfile' or die "Couldn't open file: $!"; my $string; while (<$fh>) { $string .= $_; } close $fh; Read a fixed number of bytes: open my $fh, '<', 'sample.txt' or die "Error: $!\n"; read($fh, my $data, 2000); close $fh; The read function takes a filehandle, a destination variable, and the number of bytes to read. The example above reads 2000 bytes into $data. ...