| Launcher-Cascade documentation | Contained in the Launcher-Cascade distribution. |
Launcher::Cascade::ListOfStrings - a wrapper around an array to make it inherit from Launcher::Cascade::Printable
use Launcher::Cascade::ListOfStrings;
my $l = new Launcher::Cascade::ListOfStrings
-list => [ 'some', 'strings', 'to', 'start', 'with' ],
;
push @$l, 'and', 'then', 'some';
print $l->as_string;
print "$l"; # same as above
# alter the formatting
$l->separator(q{, });
$l->preparator(sub { qq{"$_"} });
print "$l\n"; # prints quoted strings separated by comas
A coderef used to prepare each element in list() before including it the
generated string. The coderef will be invoked with $_ locally aliased to the
current element. By default, preparator() is the identity function (i.e., it
returns $_ untouched).
The string to insert between each element in list() when generating a string
(sort of like Perl's $" built-in variable. See perlvar). Defaults to the
empty string.
Strings to prepend and, respectively, append, to the string representation of the list(). Both default to the empty string.
The reference to the array containing the elements. This can also be accessed by dereferencing the object as if it were an array reference (see SYNOPSIS).
Returns a string representation of the object. Each element in list() is first passed on to the coderef in preparator(), and the list of results from preparator() is concatenated with the value of separator().
This method is called when the object is "stringified", i.e., when it is interpolated in a double-quoted string.
my $l = new Launcher::Cascade::ListOfStrings -list => [ qw( frodo pippin merry sam ) ];
$l->separator(q{, });
$l->preparator(sub { ucfirst });
print "$l\n"; # "Frodo, Pippin, Merry, Sam" and a newline
Cédric Bouvier <cbouvi@cpan.org>
Copyright (C) 2006 Cédric Bouvier, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Launcher-Cascade documentation | Contained in the Launcher-Cascade distribution. |
package Launcher::Cascade::ListOfStrings;
use strict; use warnings; use base qw( Launcher::Cascade Launcher::Cascade::Printable );
Launcher::Cascade::make_accessors_with_defaults string_before => q{}, # empty string string_after => q{}, # empty string separator => q{}, # empty string preparator => sub { $_ }, # identity ;
sub list { my $self = shift; my $old = $self->{_list} ||= []; $self->{_list} = $_[0] if @_; return $old; } use overload '@{}' => '_as_list'; sub _as_list { my $self = shift; $self->list(); }
sub as_string { my $self = shift; return $self->string_before() . join($self->separator(), map $self->preparator()->(), @{$self->list()}) . $self->string_after(); }
1; # end of Launcher::Cascade::ListOfStrings