Steffen Winkler > Tie-Sub-1.001 > Tie::Sub

Download:
Tie-Sub-1.001.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.001   Source  

NAME ^

Tie::Sub - Tying a subroutine, function or method to a hash

VERSION ^

1.001

SYNOPSIS ^

initialize

    use strict;
    use warnings;

    use Tie::Sub;

    tie my %subroutine, 'Tie::Sub', sub { ... };

or initialize late

    tie my %subroutine, 'Tie::Sub';
    ( tied %subroutine )->config( sub { ... } );

or initialize late too

    my $object = tie my %subroutine, 'Tie::Sub';
    $object->config( sub { ... } );

interpolate subroutines in a string

usage like function (only 1 return parameter)

    use strict;
    use warnings;

    use Tie::Sub;

    tie my %sprintf_04d, 'Tie::Sub', sub { sprintf '%04d', shift };

    # The hash key and return value are both scalars.
    print "See $sprintf_04d{4}, not $sprintf_04d{5} digits.";

    __END__

    Output:

    See 0004, not 0005 digits.

or more flexible

    use strict;
    use warnings;

    use Tie::Sub;

    tie my %sprintf, 'Tie::Sub', sub { sprintf shift, shift };

    # The hash key is an array reference, the return value is a scalar.
    print "See $sprintf{ [ '%04d', 4 ] } digits.";

    __END__

    Output:

    See 0004 digits.

usage like subroutine

    use strict;
    use warnings;

    use Tie::Sub;
    use English qw($LIST_SEPARATOR);

    tie my %sprintf_multi, 'Tie::Sub', sub {
        return
            ! @_
            ? q{}
            : @_ > 1
            ? [ map { sprintf "%04d\n", $_ } @_ ]
            : sprintf "%04d\n", shift;
    };

    # The hash key and the return value ar both scalars or array references.
    {
        use English qw($LIST_SEPARATOR);
        local $LIST_SEPARATOR = q{};
        print <<"EOT";
    See the following lines
    scalar
    $sprintf_multi{10}
    arrayref
    @{ $sprintf_multi{ [ 20 .. 22 ] } }
    and be lucky.
    EOT
    }

    __END__

    Output:

    See the following lines
    scalar
    0010

    arrayref
    0020
    0021
    0022

    and be lucky.

usage like method

    use strict;
    use warnings;

    use Tie::Sub;
    use CGI;

    my $cgi = CGI->new;
    tie my %cgi, 'Tie::Sub', sub {
        my ($method, @params) = @_;

        my @result = $cgi->$method(@params);

        return
            ! @result
            ? ()
            : @result > 1
            ? \@result
            : $result[0];
    };

    # Hash key and return value are both array references.
    print <<"EOT";
    Hello $cgi{ [ param => 'firstname' ] } $cgi{ [ param => 'lastname' ] }!
    EOT

    __END__

    Output if "http://.../noname.pl?firstname=Steffen&lastname=Winkler":

    Hello Steffen Winkler!

Read configuration

    my $config = ( tied %subroutine )->config;

Write configuration

    my $config = ( tied %subroutine )->config( sub{ yourcode } );

EXAMPLE ^

Inside of this Distribution is a directory named example. Run this *.pl files.

DESCRIPTION ^

Subroutines don't have interpreted into strings. The module ties a subroutine to a hash. The subroutine is executed at fetch hash. At long last this is the same, only the notation is shorter.

Alternative to

    " ... ${\ subroutine('abc') } ... "
    # or
    " ... @{[ subroutine('abc') ]} ... "
    # or
    '...' . subroutine('abc') . '...'

write

    " ... $subroutine{abc} ... "

Sometimes the subroutine expects more than 1 parameter. Then submit a reference on an array as 'hash key'. The tied subroutine will get the parameters always as list.

Use any reference to give back more then 1 return value. The caller get back this reference. There is no way to return a list.

SUBROUTINES/METHODS ^

method TIEHASH

    use Tie::Sub;
    my $object = tie my %subroutine, 'Tie::Sub', sub { yourcode };

'TIEHASH' ties your hash and set options defaults.

method config

'config' stores your own subroutine

You can get back the previous code reference or use the method config in void context. When you configure the first subroutine, the method will give back undef.

    $previous_coderef = ( tied %subroutine )->config( sub { yourcode } );

The method calls croak if you have a parameter and this parameter is not a reference of 'CODE'.

method FETCH

Give your parameter as key of your tied hash. This key can be a string or an array reference when you have more then one. 'FETCH' will run your tied subroutine and give back the returns of your subroutine. Think about, return value can't be a list, but reference of such things.

    ... = $subroutine{param};

DIAGNOSTICS ^

All methods can croak at false parameters.

CONFIGURATION AND ENVIRONMENT ^

nothing

DEPENDENCIES ^

Carp

Params::Validate

INCOMPATIBILITIES ^

not known

BUGS AND LIMITATIONS ^

not known

SEE ALSO ^

Tie::Hash

http://perl.plover.com/Identity/

http://perl.plover.com/Interpolation/

Interpolation # contains much things

Tie::Function # maybe there is a problem near '$;' in your Arguments

Tie::LazyFunction

AUTHOR ^

Steffen Winkler

LICENSE AND COPYRIGHT ^

Copyright (c) 2005 - 2012, Steffen Winkler <steffenw at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: