| Tie-Plural documentation | Contained in the Tie-Plural distribution. |
Tie::Plural - Select a string variant based on a quantity.
This documentation describes version 0.01 of Plural.pm, January 07, 2005.
$var = $pl{$num};
$var = $pl{$num, $plural};
$var = $pl{$num, $plural, $singular};
$var = $pl{$num, $plural, $singular, $zero};
$var = plural($num);
$var = plural($num, $plural);
$var = plural($num, $plural, $singular);
$var = plural($num, $plural, $singular, $zero);
This module provides a simple way to pluralize words within strings. More precisely, it provides a way to select a string from a number of choices based on a quantity. This is accomplished by a tied hash, so it is very easy to incorporate these choices into output strings, which is generally where you need them.
$variant = $pl{$number, $plural_form, $singular_form, $zero_form};
Based on $number, returns one of $plural_form, $singular_form, or
$zero_form. If $number is 0, $zero_form is returned. If $number
is 1, $singular_form is returned. Otherwise, $plural_form is returned.
Only $number is required. $plural_form defaults to 's'.
$singular_form defaults to '' (the empty string). $zero_form defaults
to whatever $plural_form is.
$variant = plural($number, $plural_form, $singular_form, $zero_form);
Based on $number, returns one of the other arguments. Works the same
as the %pl tied-hash does.
This function is not exported by default.
for $num (0..3)
{
print "I have $num dog$pl{$num}.\n";
}
# The above prints:
I have 0 dogs.
I have 1 dog.
I have 2 dogs.
I have 3 dogs.
$num = 700;
print "My wife owns $pl{$num,'many','one','no'} dress$pl{$num,'es'}.";
#
"My wife owns many dresses."
The variable %pl is exported by default. The function plural is
available for export. The tag :all will export all available
symbols into your namespace.
Damian Conway's excellent Lingua::EN::Inflect provides a much more full-featured way to produce plural forms automagically.
Eric J. Roode, roode@cpan.org
Copyright (c) 2005 by Eric J. Roode. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Tie-Plural documentation | Contained in the Tie-Plural distribution. |
use strict; package Tie::Plural; $Tie::Plural::VERSION = '0.01'; use Carp; # If exporting symbols: use Exporter; use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %pl/; @ISA = qw/Exporter/; @EXPORT = qw/%pl/; @EXPORT_OK = qw/plural/; %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); tie %pl, 'Tie::Plural'; sub plural ($;$$$) { my ($num, $plural, $sing, $zero) = @_; defined or $_ = '' for $sing; defined or $_ = 's' for $plural; defined or $_ = $plural for $zero; my $s = $num==0? $zero : $num==1? $sing : $plural; return $s; } sub TIEHASH { my $class = shift; my $dummy; # not used; bless \$dummy, $class; } sub FETCH { my $self = shift; my $key = shift; my ($n, $p, $s, $z) = split $;, $key; return plural ($n, $p, $s, $z); } 1; __END__