Acme-Sub-Parms

 view release on metacpan or  search on metacpan

examples/function_use_example.pl  view on Meta::CPAN

# Bad Example (will fail runtime assertion for 'other_thing')
foo();

exit;


#############################################
# function with two parameters
# 
#   'thing'       which is optional and defaults to "Something Blue" if not passed
#   'other_thing' which is required and cannot be the undef value
#
sub foo {
    BindParms : (
        my $thing : thing       [optional, default="Something Red"];
        my $other : other_thing [required, is_defined];
    )

    print "thing       = $thing\n";
    print "other_thing = $other\n";
}

examples/object_use_example.pl  view on Meta::CPAN


use strict;
use warnings;

use Acme::Sub::Parms;

#############################################
# object instance constructor with two parameters
# 
#   'thing'       which is optional and defaults to "Something Blue" if not passed
#   'other_thing' which is required and cannot be the undef value
#
sub new {
    my $proto   = shift;
    my $package = __PACKAGE__;
    my $class   = ref($proto) || $proto || $package;
    my $self    = bless {}, $class;

    BindParms : (
        my $thing : thing       [optional, default="Something Blue"];
        my $other : other_thing [required, is_defined];

lib/Acme/Sub/Parms.pm  view on Meta::CPAN


    ######################
    # required 
    if ((! $no_validation) && $spec_tokens->{'required'}) {
        $output .= "unless (exists (\$Acme::Sub::Parms::args\{\'$field_name\'\})) { require Carp; Carp::croak(\"Missing required parameter \'$field_name\'\"); } ";
    }

    ######################
    # is_defined 
    if ($spec_tokens->{'is_defined'}) {
        $output .= "if (exists (\$Acme::Sub::Parms::args\{\'$field_name\'\}) and (! defined (\$Acme::Sub::Parms::args\{\'$field_name\'\}))) { require Carp; Carp::croak(\"parameter \'$field_name\' cannot be undef\"); } ";
    }

    my $type_requirements = $spec_tokens->{'type'};
    my $isa_requirements  = $spec_tokens->{'isa'};
    my $can_requirements  = $spec_tokens->{'can'};

    if (defined ($type_requirements ) || defined($isa_requirements) || defined($can_requirements)) {
        $output .=  "if (exists (\$Acme::Sub::Parms::args\{\'$field_name\'\})) \{";

        #####################

lib/Acme/Sub/Parms.pm  view on Meta::CPAN

                        $self->{'simple_bind'} = 0;
                    }
                } elsif ($bind_field =~ m/^\w+$/) { # my $thing : something;
                	$bind_entry->{'spec'}  = 'required';
                	unless ($no_validation) {
                		$self->{'simple_bind'} = 0;	
                    }
                } else {
                	die("Failed to parse BindParms block line $Acme::Sub::Parms::line_counter: $_");
                }
                undef $trailing_comment;
                undef $bind_var;
                undef $bind_field;
                $_ = '';

            ############################
            # Blank and comment only lines
            } elsif (m/^(\s*|\s*#.*)$/) {
            	my $trailing_comment = $1;
            	$trailing_comment = defined ($trailing_comment) ? $trailing_comment : '';
            	$trailing_comment =~ s/[\r\n]+$//s;
            	
                my $bind_entry = { spec => '', trailing_comment => $trailing_comment};

lib/Acme/Sub/Parms.pod  view on Meta::CPAN

    #....
 )

=back

=over 4

=item is_defined

The 'is_defined' declaration generates a validation requirement that the
parameter B<IF PRESENT> must not have an undefined value - passing an
undefined value results in a runtime 'croak'.

If the parameter is B<optional> it may still be omitted - but must not have an undefined value if passed.

If the the parameter is B<required> then it B<must> be present, B<AND> must not have an undefined value.

Example:

  # Optional but may not be undefined if passed
  BindParms : (
      my $handle : handle [optional, is_defined];
  )

  # Required and may not be undefined
  BindParms : (
      my $handle : handle [required, is_defined];
  )

=back

=over 4

=item can=method | can="method1 method2 method3 ..."

lib/Acme/Sub/Parms.pod  view on Meta::CPAN

  )

=back

=over 4

=item default=something | default="some thing with spaces"

The 'default' declaration allows the setting of default values for
optional parameters (it is implicit that if omitted the default value
is undef).

If the value contains whitespace (or if you want the empty string
value), you will need to use quotes around it.

'default' behaves differently for required and optional parameters:

For an optional parameter, it only activates if the parameter is
omitted completely. It will not kick in for a value that is passed
but has the undefined value.

For a required parameter, it only activates if the parameter is
undefined.

=back

=over 4

=item callback=function_name

The 'callback' declaration lets you specify a function name to be used
to perform validation on the parameter(s). 
The syntax is simple: callback=validation_function_name



( run in 2.018 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )