Locale::Maketext::TieHash::nbsp - Tying subroutine to a hash


Locale-Maketext-TieHash-nbsp documentation Contained in the Locale-Maketext-TieHash-nbsp distribution.

Index


Code Index:

NAME

Top

Locale::Maketext::TieHash::nbsp - Tying subroutine to a hash

VERSION

Top

1.02

SYNOPSIS

Top

    use strict;
    use warnings;

    use Locale::Maketext::TieHash::nbsp;

without a special configuration (using entities)

    tie my %nbsp, 'Locale::Maketext::TieHash::nbsp';
    print $nbsp{'15 pieces'};
    # result: '15 pieces'

configuration of unicode separator string

    use charnames qw(:full);

    tie my %nbsp, 'Locale::Maketext::TieHash::nbsp', separator => "\N{NO-BREAK SPACE}";
    print $nbsp{'15 pieces'};
    # result is eq "15\N{NO-BREAK SPACE}pieces"

configuration of visible string

To test the script, store an visible string.

    tie my %nbsp, 'Locale::Maketext::TieHash::nbsp', separator => q{~};
    print $nbsp{'15 pieces'};
    # result: '15~pieces'

configuration using a subroutine

    tie my %nbsp, 'Locale::Maketext::TieHash::nbsp', sub => sub {
        (my $string = shift) =~ s{ }{*}msg;
        return $string;
    };
    print $nbsp{'15 pieces'};
    # result: '15*pieces'

write/read configuration

    my $former_code_ref = tied(%nbsp)->config(separator => $separator);

or

    my $former_code_ref = tied(%nbsp)->config(sub => $new_code_ref);

EXAMPLE

Top

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

DESCRIPTION

Top

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.

Sometimes the subroutine 'sub' expects more than 1 parameter. Then submit a reference on an array as hash key.

SUBROUTINES/METHODS

Top

method TIEHASH

    tie my %nbsp, 'Locale::Maketext::TieHash::nbsp';

'TIEHASH' ties your hash and set the options defaults.

method config

Stores the seperator string or a subroutine.

    tied(%nbsp)->config(
        sub => sub {
            (my $string = shift) =~ s{ }{ }msg;
            return $string;
        },
    );

or

    tied(%nbsp)->config(
        separator => ' ',
    );

'config' accepts all parameters as Hash and gives a Hash back with all set attributes.

method FETCH

Give your string as key of your hash. 'FETCH' will substitute the whitespace to ' ' and give it back as value.

    # Substitute
    print $nbsp{$string};

DIAGNOSTICS

Top

All methods can croak at false parameters.

CONFIGURATION AND ENVIRONMENT

Top

nothing

DEPENDENCIES

Top

parent

Tie::Sub

Params::Validate Comfortable parameter validation

INCOMPATIBILITIES

Top

not known

BUGS AND LIMITATIONS

Top

not known

SEE ALSO

Top

Locale::Maketext Localisation framework

Tie::Hash

AUTHOR

Top

Steffen Winkler

LICENSE AND COPYRIGHT

Top


Locale-Maketext-TieHash-nbsp documentation Contained in the Locale-Maketext-TieHash-nbsp distribution.

package Locale::Maketext::TieHash::nbsp; ## no critic (Capitalization)

use strict;
use warnings;

our $VERSION = '1.02';

use parent qw(Tie::Sub);

use Params::Validate qw(:all);

my $get_substitute_sub = sub {
    my $separator = shift || ' ';

    return sub {
        (my $string = shift) =~ s{ }{$separator}msg;
        return $string;
    };
};

## no critic (ArgUnpacking)

sub TIEHASH {
    my ($class, %init) = validate_pos(
        @_,
        {type => SCALAR},
        ( ({type => SCALAR}, 1) x ((@_ - 1) / 2) ),
    );

    my $self = $class->SUPER::TIEHASH( $get_substitute_sub->() );
    $self->config(%init);

    return $self;
}

sub config {
    return shift->SUPER::config(@_) if caller eq 'Tie::Sub';
    # object, hash
    my ($self, %init) = validate_pos(
        @_,
        {isa => __PACKAGE__},
        ( ({type => SCALAR}, 1) x ((@_ - 1) / 2) ),
    );
    validate_with(
        params => \%init,
        spec   => {
            sub       => {
                type      => CODEREF,
                optional  => 1,
                callbacks => {
                    separator_not_exists => sub {
                        return ! defined $init{separator};
                    },
                },
            },
            separator => {
                type      => SCALAR,
                optional  => 1,
                callbacks => {
                    sub_not_exists => sub {
                        return ! defined $init{sub};
                    },
                },
            },
        },
        called => 'the config hash',
    );

    if (exists $init{sub}) {
        return $self->SUPER::config( $init{sub} );
    }
    if (exists $init{separator}) {
        return $self->SUPER::config(
            $get_substitute_sub->( $init{separator} )
        );
    }

    return;
}

# $Id$

1;

__END__