Perl Notes

This didn’t go very far because what was going to get done in Perl was not to be ultimately.


Welcome to Perl...

Handy subroutine for Perl (at the point where I am now anyway):

	sub println
	{
	   my( @args ) = @_;           # pick up subroutine arguments
	   my  $arg;

	   foreach my $arg in (@args)  # arguments treated as a list/array
	   {                           # hmmm... braces required anyway!?
	      print "$arg";            # double quotes ensure "interpolation"
	   }

	   print "\n";                 # this is the "ln" part of println
	}

Here’s another one:

	sub askyesno
	{
	   my( $prompt ) = @_;
	   my  $output = undef;       # hey, it’s interpretive, so who cares?

	   print "$prompt";

	   do
	   {
	      print "(yes/no) ";      # (insist: but we’ll also tolerate y/n)
	      $output = <STDIN>;
	      chomp $output;          # stdin also leaves the \n on $output
	   }
	   until ($output eq "yes" || $output eq "n" || $output eq "no" || $output eq "n");

	   return ($output eq "y") ? "yes" : ($output eq "n") ? "no" : $output;
	}

	my $output = askyesno("Are you having fun yet? ");

	print "$output\n";


More handy stuff...

Sample response is given here...

	$ perl -e '$! = 16;print "$!\n"'
	> Device or resource busy

	$ perl -mErrno -e '$!=16; print grep($!{$_}, keys(%!)), ": $!\n";'
	> EBUSY: Device or resource busy

	# search and replace across a bunch of files...
	$ perl -pi -e 's,pattern,replacement,' *

	--------------------- some-file.pl -----------------------------------------
	#!/bin/env perl

	use Errno;

	die("Usage: errno \n") unless @ARGV == 1; $!=$ARGV[0]; print grep($!{$_}, keys(%!)), ": $!\n";
	----------------------------------------------------------------------------