in reply to printing complex data structures

If you want to figure this out yourself, one thing that's helpful is to use e.g. Data::Dump to show the data structure. So for example, immediately after foreach my $planet (keys %$h_ref){, add dd $planet, $h_ref->{$planet}; to look at what you're dealing with at that point. You'll see that $planet is a string (the key of the hash), and that $h_ref->{$planet} (the value of the hash) is the hash reference that you should be dereferencing on the next line, i.e. foreach my $aa (keys %{ $h_ref->{$planet} }), and you have the same issue on the following line.

Extra credit: Because you're using Prototypes in sub base_code (\%), when you write base_code (%codes);, that is actually being translated by Perl to &base_code(\%codes). My recommendation would be to not use prototypes (sub base_code {...) and instead call the sub as base_code(\%codes);. See my recent post Fun with Prototypes for a demonstration of one of the problems with prototypes.

Update: Just a minor note: since hashes are unordered, keys will return the keys of the hash in an apparently random order. If you want your output to be consistent across different runs of the program, you could use foreach my ... (sort keys %...).