A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Sorting in Perl

Custom sorting method:

@sorted = sort { 
  if ($a eq 'aardvark') { return 1; }
  elsif ($b eq 'aardvark') { return -1; }
  else { return $a cmp $b; } 
} @words;

Remember to test for the special case for both $a and $b.


Thanks

Common admin tasks in phpBB 3

Change the author of a post:

  • Click on the post
  • Click on the "Moderator Control Panel" link in the top left
  • Click on the "Post details" link on the right-hand side (note: It might look like a triangle with a question mark in it)
  • Scroll down to the bottom of the post, and you will see a drop-down menu to change the poster.

Easiest way to find yesterday's date in Perl

my ($year, $month, $day) = (localtime(time + 7 * 24 * 60 * 60))[5,4,3];
$year += 1900;
$month++;

svn: Files skipped

Is subversion displaying a message that some files/directories were "skipped" ?
If you are using symlinks, try cd'ing into the directory, or using the full path instead.

Prevent spammers from harvesting your email address

When you write your email address on a web page, use @ instead of @, and . instead of the dot.
That way, most email harvesting programs won't recognise it.

Here's a method using Javascript that should stop 100% of email harvesting programs.

However, just using a web contact form would work best.

Find dodgy characters in a file

This will print any characters with values higher than 127, i.e. non-ascii characters that might look like ascii, but really be breaking your processor.

perl -lne'while (my ($c) = $_ =~ m{^(.)}) { print "$c = ".ord($c) if ord($c) > 127; $_ =~ s/^.// }' file_to_check.something


A quick, manual way to confirm some characters really are what you think they are:

cat file_to_check.something | od -bc

Require a parameter/variable in bash

FILE=$1
if [[ -z "$FILE" ]]; then echo "usage: script_name.sh [filename]"; exit 1; fi

# don't forget the quotes around "$FILE", they will be required if $FILE has a space in it, and always use double [[ ]]

Add a pause to a bash script

read -p "Press any key to start backup..."
thanks

How to add a custom PHP page to phpBB3

There is a tutorial explaining how to add a static HTML page, with phpBB header and footer.
This is how to add a dynamic PHP page in a similar way:
  1. Go to ACP | General | Security settings, and set "Allow php in templates" to "Yes".
  2. Follow the tutorial at: http://wiki.phpbb.com/Practical.Add_custom_page
  3. In the template (e.g. about_us.html), replace where it says "Content in here" with <!-- INCLUDEPHP relative/path/to/your/php/page.php -->

Perl: Fix DBI::db->disconnect invalidates 1 active statement handle

Add this subroutine to the module:

sub DESTROY {
    my ($self) = @_;
    # Finish off our statement handle, so we don't get warnings like:
    # DBI::db->disconnect invalidates 1 active statement handle
    my $sth = $self->{data_handle};
    if (blessed($sth) && $sth->can('finish')) {
        $sth->finish;
    }
}

Things lacking in Eclipse for Perl

Using EPIC
  • You can't fold code on 'if' blocks, or any arbitrary block (only on subs and perldocs).
  • Word wrap causes the line numbers to be wrong (a single wrapped line has more than one line number)
  • Syntax highlighting is not a perfect fit (perhaps this or these might help):
    • POD and heredoc are both marked as 'Comment2' and so can't be different colours
    • Even though there is a 'Markup' entry, it doesn't seem to be used for POD
    • The colour of dollar signs can't be changed (it's the same as the variable name in vim)
    • Neither variables nor special characters change colour within interpolated strings (as in vim)

How to change a shape type in Visio

Install the "Super Utilities and Tools" add-in from Paul Herber's Sandrila Ltd.

Use the 'Shape | Substitute' command.

Create Eclipse project with existing files

  • Have your workspace as the directory above the directory containing your files.
  • Create the project with a name that is the same as your files directory.
The problem is, if there are lots of (unrelated) files, Eclipse will take a long time to load up.

There are other options, i.e.:

  • Create a new folder
  • Click the 'Advanced' button
  • Link to an external folder

sshfs on linux

  • sshfs -o uid=1000 -o gid=1000 example.com:/workplace /workplace
Where 1000 is your user and group ID from /etc/passwd

That's it!

Make a layer transparent in GIMP

  • Create a blank layer, filled with white
  • Right-click on the layer and choose 'Add Layer Mask'
  • Select 'Grayscale copy of layer'
  • Fill or draw on the layer with any colour (e.g. shades of gray)
    • the darker black you go, the more transparent the layer will become