Gordon Rowell > CGI-FormMagick-0.89 > CGI::FormMagick::Sub

Download:
CGI-FormMagick-0.89.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
Stalled  9
View Bugs
Report a bug
Source  

NAME ^

CGI::FormMagick::Sub - Call subs by name.

SYNOPSIS ^

    use CGI::FormMagick::Sub;
    my %sub = (
        package => $some_package_name,
        sub => $some_sub_name,
        args => \@array_of_args,
        comma_delimited_args => $string_of_comma_delimited_args,
    );

    CGI::FormMagick::Sub::exists(%sub) or return undef;
    return CGI::FormMagick::Sub::call(%sub);

DESCRIPTION ^

(Intended for internal use only.)

Used for calling subs whose names are dynamically generated.

STATIC METHODS ^

exists(...)

exists() takes a hash with keys "package" and "sub". Returns true if the sub exists, false otherwise.

call(...)

call() takes a hash with keys "package" and "sub", and optional "args" and "comma_delimited_args". The "comma_delimited_args" are split up and pushed into the array of args to be sent to the sub when called. Returns the return of the called sub itself.

If the sub doesn't exist, it will return undef. If $^W is true, it will also complain.

{ package main; sub f { return 'Ok' } }

{ package ArbitraryPackage; sub hey { return 'You found me.'; }

    sub with_arg {
        return $_[0] x 2;
    }

    sub with_args {
        my $results = '';
        $results .= $_ for (reverse @_);
        return $results;
    }
}

{ package Ness::ted;

    sub attack {
        return 'PK Fire!'; # (Sorry -- Smash Bros. reference. =))
    }
}

foreach my $expectations ( { expected => 'Ok', call_with => { package => 'main', sub => 'f' } }, { expected => 'You found me.', call_with => { package => 'ArbitraryPackage', sub => 'hey' } }, { expected => 'PK Fire!', call_with => { package => 'Ness::ted', sub => 'attack' } }, { expected => 'RepeatRepeat', call_with => { package => 'ArbitraryPackage', sub => 'with_arg', args => [ 'Repeat' ], } }, { expected => 'Backwards', call_with => { package => 'ArbitraryPackage', sub => 'with_args', args => [ qw(wards Back) ] } }, { expected => 'abc', call_with => { package => 'ArbitraryPackage', sub => 'with_args', args => [ qw(c b a) ] } }, { expected => 'abc', call_with => { package => 'ArbitraryPackage', sub => 'with_args', comma_delimited_args => 'c,b,a' } }

# We could do parsing for this, but we'll defer it until it's needed. # , { # expected => 'OneTwoThree', # call_with => { # package => 'ArbitraryPackage', # sub => 'with_args', # comma_delimited_args => '"Thr,ee","Two","One"' # } # }

) { my $expected = $expectations->{expected}; my %call_with = %{$expectations->{call_with}}; my ($package, $sub) = @call_with{qw(package sub)};

    my $description = "$package\::$sub";

    if (exists $call_with{args}) {
        $description .= "('" . join("', '", @{$call_with{args}}) . "')";
    } else {
        $description .= '()';
    }

    my $actual = CGI::FormMagick::Sub::call(%call_with);

    is($actual, $expected, $description);
}