Thank you for taking the time to type this all out for everyone and me. Have a cookie! Now, I have some questions, though they aren't about OO specifically.
- How would you control explosive growth with this? I could sneeze and come up with a new list of items that I would like to randomize. Do I just keep adding new things to the top under RandThing and maybe keeping a list in a comment? The current set up looks a bit bulky for all of the randomizing I would be doing with this. Below are two lists I came up with while sitting here, and I may actually use them some day.
package RandWritingImplement;
use parent -norequire, 'RandThing';
sub new {
my ($class) = @_;
my @values = qw(pencil pen marker crayon chalk);
return $class->SUPER::new (@values);
}
package RandWritingSurface;
use parent -norequire, 'RandThing';
sub new {
my ($class) = @_;
my @values = qw(paper parchment slate hide stone);
return $class->SUPER::new (@values);
}
- How would RandThing be modified to accept not only arrays but also hashes? There are times where the list comes from a single hash key, the list could be the hash keys, or all of the arrays in the hash jumbled into one large list. Below is a subroutine I wrote which does it all for me, except that it doesn't take direct input.
sub _random {
my ($list,$key) = @_;
my @random_array;
if (ref($list) eq "HASH") {
if ($list->{$key}) {
@random_array = @{$list->{$key}};
}
elsif ($key eq 'keys') {
@random_array = (keys %{$list});
}
elsif ($key eq 'all') {
@random_array = map(@{$list->{$_}},keys %{$list});
}
else {
die q(You need to select a key from the hash, "all" keys, or jus
+t "keys".);
}
}
elsif (ref($list) eq "ARRAY") {
@random_array = @{$list};
}
else {
die q(What are you doing? You didn't enter a hash or an array.);
}
return $random_array[rand @random_array];
}
These are two issues that I don't see an answer for in the current set up. OO does sound appealing, however, it appears to be very bulky for even something as simple as randomizing a lot of lists.
Have a cookie and a very nice day!
Lady Aleena
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|