Learning Perl with @Rubenerd - Part 1


Perl with Ruben

This was actually yesterday, but procrastination prevails. I haven't the foggiest idea what the appropriate terminology is and I won't be teaching anyone very soon, so let's just dump everything below:

You need this line to start off, I gather, unless you're using Perl on Windows. Not really sure what you call it, but I remember shebang from bash scripting in Web Systems (wow, I used to be fairly decent at that). Also apparently this is used in the event Perl happens to be installed elsewhere (with warnings and strict just in case you make silly errors).

#!/usr/bin/env perl -w

use strict;

But Ruben uses this, so I gather this is fine for on Mac:

#!/usr/local/bin/perl

And we all like to print things.

#!/usr/bin/env perl -w

use strict;

print("Hi Ruben\n");

I always get that new line slash the wrong way.

So the main reason I want to use Perl is for scripting and I'm not really inclined to use the Automator (besides I don't think it does what I want it to anyway), so loops are good for batch processing things.

For some reason while loops make more sense to me than foreach loops. I'm sorry, and I really have no idea how to use them, haha. I don't like that example though because Arrays send me running for cover. Oh Java how you scar me.

I believe <> is used when I want to, er, do things in the current folder. Someone correct me. Wildcarding it with the trusty asterisk will let you/me (wow, the tensing in this) go through every file, or just use the asterisk how it was meant to be used. /assumptions

Oh, and system(); lets you issue Linux commands.. Bring on the Linux. $_ uses the current file. I would say this seems similar to JavaScript, but that doesn't really go through files, just uses the $ sign as well...

Adding extensions to files that start in 'test':

while () {
  system("mv $_ $_.txt");
}

Now I want to add the extension of .txt to any file that doesn't have an extension, so we look for a partial match (using =~, kekekek) for any file that has a . (full stop/period) in it, since that's what these files have in common.

This script wasn't working at first, but then Ruben realised we needed to escape the full stop/period before it could find it in the files:

while (<*>) {
  unless ($_ =~ /./) {
    system("mv $_ $_.txt");
  }
}

Oh right, and using unless instead of if, unlike Java where you use the exclamation mark to make it NOT something. So in the above case, unless the file contains a full stop (i.e. already has an extension), then add the .txt extension.

Next example, if I want to change anything with the extension .txt to .md. While there are files in the folder, if contains .txt, swap .txt with blank and then move these to .md... otherwise the files will be appended with .txt.md. Didn't realise that at first.

while (<*>) {  
  if ($_ =~ /\.txt/) {
    $_ =~ s/\.txt//;
    system("mv $_.txt $_.md");
  }
}

Been using wildcards, but we can specify a match from the start or the end... I think. Anyway, they look something like this:

=~ /a$/    # from the end
=~ /^a/    # from the start

Used like in this case with Imagemagick...

while (<*jpg>) {
  $_ =~ s/jpg$//; 
  system("convert -resize 500x -quality 96 $_jpg $_v2.jpg");
}

I don't know what I'm talking about, so if that made sense to you, that's great. Also, someone here should hire Ruben out as Perl teacher. Yes/yes?