Wiggins d Anconia wrote:
Charles K. Clarkson wrote:

Andrew Gaffney <[EMAIL PROTECTED]> wrote:
: : I think that 'my' is bad because I have something similar to:
: : my %tree;
: : sub return_an_arrayref() {
: my @array = ('thing1', 'thing2', 'thing3');
: return [EMAIL PROTECTED];
: }
: : sub build_tree() {
: foreach(@thing) {
: $tree{$_} = return_an_arrayref();
: }
: }
: : use Data::Dumper;
: print Dumper(%tree);
: : The output shows a bunch of empty arrays or arrays with
: all undef elements under each key in %tree. I know
: return_an_arrayref() is returning data because I can
: print all the elements out in build_tree().


Remember Finagle's Third Law:

"In any collection of data, the figure most obviously
 correct, beyond all need of checking, is the mistake."


Your results indicate that return_an_arrayref() does sometimes return a reference to an empty array. Show us unedited code for more help.

Alright, you asked for it. In order to run this program, you will need

to be


running Gentoo Linux as this program uses the Portage tree and

Portage's config


files.


Ok, slow down killer :-).


#!/usr/bin/perl

use strict;
#use warnings;

#use Getopt::Long;

my %masks;
my %use;
my @pkglist;
my %pkgdeps;

Why are these declared with a global scope? If they must be then something is wrong with your subs. Move these to after your sub listing, if your program still works then they are fine as globals, otherwise you have broken encapsulation and that leads to more complex code. It also means you are not letting 'strict' help you in the manner it was designed to.

Nope, I need %masks, %use, and %pkgdeps to be global because multiple subroutines will need access to them.


[snip subs]

init();

my $original = build_deptree($ARGV[0]);
use Data::Dumper;
print Dumper(%pkgdeps);

Based on this snippet you should have need for only one global, $original. Everything else should be lexical non-file scoped. Remember that subs should take values and return values, your init() does neither which means its contents can be moved into main, or into build_deptree, or you need to be passing it something.

This program is far from complete. There will be many more functions that will need access to those global variables. I put those configuration subroutine calls into init() because it cleans up the main code a bit.


Try moving all of your subs into a library and keeping your main
separate, use 'strict' in both files, make every variable 'my'd, when
that works I suspect your problems will be solved.

-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to