Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Code Works But Not Sure How

by haukex (Abbot)
on Feb 11, 2018 at 11:42 UTC ( #1208945=note: print w/replies, xml ) Need Help??


in reply to Re: Code Works But Not Sure How
in thread Code Works But Not Sure How

The main issue is that your array is not passed to the subroutine, since you give no arguments in the call.

RichHamilton: I just wanted to highlight this since it is the first thing that jumped out at me when I skimmed your code.

Inside new, you call make_menu_items();, without arguments and not as a method call, so Perl won't pass $self as the first argument. One way to change this is by blessing the object first, i.e. bless $self, $class; $self->make_menu_items();.

So in the code you showed, in the call to sub make_menu_items, $self starts out as undef. The reason the code doesn't fail is autovivification (here's one thread of several on that). As 1nickt said, if you dump $self, you'll see that before the line "my $items = $#{$self->{menu_items}};" it is undef, but after that line it magically becomes { menu_items => [] }, because you first accessed it as a hashref with $self->{...}, and then accessed the hash key menu_items as an array reference with $#{...}.

Replies are listed 'Best First'.
Re^3: Code Works But Not Sure How
by RichHamilton (Novice) on Feb 11, 2018 at 13:18 UTC
    Sounds like I need to go though Chapter 4 of Conway's book again. :) He talks about autovivification there. And I'll check out the thread too. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208945]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-07-21 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (448 votes). Check out past polls.

    Notices?