Brian d foy introduced the Perl community to the idea of a script and module in the same file (the so-called modulino concept). Essentially, the idea is that you can create a script that provides a command line interface to your Perl module. Essentially, the implementation looks something like this:

 package Foo

 caller or __PACKAGE__->main();

 # package guts
 ...

 sub main {
  my $foo = Foo->new;

  use Getopt::Long;

  # do something interesting here...

  exit 0;
 }

 1;

This model comes in quite handy when writing scripts or just exercising your module during it’s creation.

I sometimes find it useful to then create a bash script that invokes my modulino. Since I might want to point to a development version of the script, I might set my PERL5LIB path to my local development environment. Running the modulino script, I would then expect it to use the version in my path. Hence the following modulino script.

 #!/usr/bin/env bash
 # -*- mode: sh; -*-

 MODULINO="Amazon::Credentials"
 MODULINO_PATH="${MODULINO//::/\/}.pm"

 MODULINO_RUN=$(perl -M$MODULINO -e 'print $INC{"'$MODULINO_PATH'"}';)

 test -n "$DEBUG" && echo $$MODULINO_RUN

 if test -z "$MODULINO_RUN"; then
     echo "$MODULINO is not installed"
     exit 1;
 fi

 perl $MODULINO_RUN "$@"

…and then

 $ amazon-credentials.sh -h
 amazon-credentials.sh options

 Formats credentials found in env, config, SSO, role

 Options
 -------
 --help, -h     this
 --ec2,         get credentials from server IAM profile
 --env          get credentials from environment variables
 --profile, -p  get credentials from profile in credentials configuration
 --role         get credentials from SSO role
 --account      use with --role, specify AWS account id

  $ amazon-credentials.sh --profile=test
   export AWS_ACCESS_KEY_ID=AKI*****************
   export AWS_SECRET_ACCESS_KEY=****************************************

  $ aws sso login
  $ amazon-credentials.sh --role my-sso-role --account 01234567890

More information about modulinos can be found here:


Next post: My New Favorite Perl Module

Previous post: Supercharging Your Perl Development