On 21/10/2011 01:54, David Christensen wrote:
On 10/19/2011 07:04 PM, David Christensen wrote:
 > I am looking for a class that stores zero or more items (e.g. a list)
 > and that provides accessors that accept and return scalar, list,
 > and/or array references depending upon context and object content
 > (similar to CGI.pm, but not exactly the same):
 > use WishfulThinkingListClass;
 > my $o = WishfulThinkingListClass->new();
 > $o->set('foo'); # saves one-item list
 > my $v = $o->get(); # returns scalar 'foo'
 > my @v = $o->get(); # returns one-item list ('foo')
 > $o->set('bar', 'baz'); # saves two-item list
 > $v = $o->get(); # returns arrayref ['bar', 'baz']
 > @v = $o-get(); # returns list ('bar', 'baz')
 > $o->set(['bozo']); # saves one-item list
 > In addition to "set", "get", I can envision instance methods
 > "mypush", "mypop", "myshift", and "myunshift".
 > Is there such an thing in standard Perl (5.10.1) or on CPAN?

I think you should be wary of returning a simple scalar only when there
is just a single item in the list. You will end up having to check to
see if the return value is a reference before you know how to handle it.

Also, think about what the return should be if the list is empty?

On 10/20/2011 07:27 AM, Randal L. Schwartz wrote:
With the right amount of definitions, I did something exactly like this
with Moose. Unfortunately, the actual code is at $former_client's SVN,
so I can't pull it out just now.

Thank you for your reply, and thank you for your contributions to Perl. :-)


I've also built something similar, in the form of a data structure (hash
of WishfulThinkingListClass'es) that I use for holding arguments to
CGI::header(). I was hoping for an existing general-purpose type that I
could use and/or extend as required (such as arguments to CGI::redirect()).

Why are you insisting on an existing class, especially if you are
intending to extend it?

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to