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

Jump to

Quick reference

Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Why should you validate parameters to all subroutines?

A list of reasons:

  • If you don't catch bad data immediately, it will be propagated onwards and the program may fail in an unexpected way
    • The problem and may not even be caught by your tests if the bad data is passed on to an external system and not checked by you


Use MooseX::Params::Validate, or Params::Validate.

bash default parameters

# Set dir to $1, or "foo" if $1 isn't set
DIR=${1:-foo}

sudo doesn't list files properly

Problem:

You can list a directory using sudo:
$ sudo ls /var/log/apache/
access_log  error_log

But you can't list files with a wildcard using sudo:
$ sudo ls /var/log/apache/*_log
ls: cannot access /var/log/apache/*_log: No such file or directory

Solution:

Ensure parameter expansion happens in the shell run by sudo, not in the user's shell:
$ sudo bash -c 'ls /var/log/apache/*_log'

(source)

Perl MooseX::Params::Validate gotcha

This error:

Parameter #1 ("1") to Some::Module::a_method did not pass the \'checking type constraint for Different::Module\' callback\n at /some/path/to/some/file/or/other.pm line 42

(but the parameters passed in are actually all correct)

...it could mean you forgot to shift off $self. pos_validated_list doesn't detect $self like validated_list and validated_hash do.

Parameter checking Perl


use MooseX::Params::Validate;
use MooseX::Types::Moose qw{ ArrayRef Int };
use MooseX::Types::Structured qw{ Dict };
use MooseX::Types::Common::Numeric qw{ PositiveInt PositiveOrZeroInt };
use MooseX::Types::Common::String qw{ NonEmptyStr };

sub some_method {
    my ($self, $channel_id, $some_array_of_structs) = validated_list(
        \@_,
        channel_id   => { isa => PositiveInt },
        channel_name => { isa => NonEmptyStr },
        some_array_of_structs => {
            isa => ArrayRef[ Dict[
                some_id    => PositiveInt,
                quantity   => PositiveOrZeroInt,
                cruciality => Int
            ] ]
        }
    );
    # ...
}

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 [[ ]]

Perl subroutine parameters

Call subs like this:
sub_name(
      param1 => 'value1',
      param2 => 'value2',
);

Define subs like this:
sub sub_name {
    my $p1      = {@_}->{param1};
    my $p2      = {@_}->{param2};
    # etc
}

Pass a parameter to XSL in Cocoon

In the sitemap:

<map:transform src="xslt/page.xsl">
<map:parameter name="param1" value="{1}"/>
</map:transform>

In the XSL:

<xsl:param name="param1"/>

...

<xsl:template match="whatever">
<xsl:attribute name="something">
<xsl:value-of select="$param1"/>
</xsl:attribute>
</xsl:template>

PHP command line

php -f file.php

file.php contains:
<?php
print "hello world";
?>

or a one-liner:

php -r "some code;"

Don't forget the trailing semicolon.

Bash menu

Try this menu script for bash scripts:

while getopts ":u:a:s:v" options; do
  case $options in
    u ) uname=$OPTARG;;
    a ) attrs=$OPTARG;;
    s ) searchattr=$OPTARG;;
    v ) att=ALL;;
    h ) echo $USAGE;;
    \? ) echo $USAGE
         exit 1;;
    * ) echo $USAGE
          exit 1;;
  esac
done

echo uname = $uname

Bash getopts

Use named command line parameters in Bash scripts.