How to:
(Type::Params module)
A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.
Jump to
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:
Use MooseX::Params::Validate, or Params::Validate.
- 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.
Labels:
parameters,
validate
bash default parameters
# Set dir to $1, or "foo" if $1 isn't set
DIR=${1:-foo}
Labels:
bash,
default,
parameters,
script
sudo doesn't list files properly
Problem:
You can list a directory using sudo:
$ sudo ls /var/log/apache/
access_log error_log
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)
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
ls: cannot access /var/log/apache/*_log: No such file or directory
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 #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.
Labels:
error,
moose,
parameters,
perl
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
] ]
}
);
# ...
}
Labels:
data,
moose,
parameters,
perl,
validation
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 [[ ]]
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 [[ ]]
Labels:
bash,
if,
parameters,
variables
Perl subroutine parameters
Call subs like this:
Define subs like this:
sub_name(
param1 => 'value1',
param2 => 'value2',
);Define subs like this:
sub sub_name {
my $p1 = {@_}->{param1};
my $p2 = {@_}->{param2};
# etc
my $p1 = {@_}->{param1};
my $p2 = {@_}->{param2};
# etc
}
Labels:
namespaces,
parameters,
perl,
subroutines
Pass a parameter to XSL in Cocoon
In the sitemap:
In the XSL:
<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>
Labels:
cocoon,
parameters,
variables,
xsl
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.
Labels:
commandline,
files,
parameters,
php
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
Labels:
bash,
idea,
linux,
menu,
parameters,
productivity,
scripting
Bash getopts
Use named command line parameters in Bash scripts.
Labels:
bash,
linux,
parameters,
shell
Subscribe to:
Posts (Atom)