The code you showed still does not work because it suffers from the problem 1nickt and I explained: With "make_menu_items();", you're not giving it any arguments, so inside make_menu_items, $items is still undef, this is the reason you get the "Use of uninitialized value" warning. Again, use Data::Dumper or Data::Dump to actually look at the values you are working with, see the Basic debugging checklist.
but then I have to pass the array size.
While necessary in languages like C, it's not necessary in Perl because you can easily get the size of an array via scalar(@array), or the index of its last element via $#array. For an array reference like the one you're passing to new, you can get its size via scalar(@{$arrayref}), or the index of its last element via $#{$arrayref}. There are also shorter ways to express the size of an array, but the aforementioned should work in just about any situation.
Update: Plus, another central point is that Perl provides the foreach loop, where you don't even need to know the size of the array you are iterating over, and that kind of loop is almost always better. Plus, it "aliases" the iterator variable to the actual array item, so you can modify array items through it. The following prints "("Laziness is cool", "Impatience is cool", "Hubris is cool")":
use warnings;
use strict;
use Data::Dump;
my @array = ('Laziness', 'Impatience', 'Hubris');
for my $item (@array) {
$item = "$item is cool";
}
dd @array;
-
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.