Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks,
I have a couple modules, to control registration/logging in for a cgi page. I've got a module to handle my cookies, a module to handle my database(DBM) stuff, and a module to print out errors for me, along with my .cgi files to pull it all together and print out the website. I'm trying to split the contents of a certain entry in the database into an array, which is then passed to another module, which then passes the array to another module. Can anyone explain how I would do this? I've tried using this:

my @stats = split(/,/,$USERS{$uname . 'stats'});


And then having the second module do something like:

my @stats = Module::SplitEntries;


And then in my final .cgi file, having IT say:

my @stats = OtherModule:GetSplitEntries;


But this does not seem to work. Does anyone know how I would do this?

Thanks,
Spidy

Replies are listed 'Best First'.
Re: Passing an array between multiple modules?
by duff (Parson) on Sep 30, 2004 at 03:40 UTC

    Bad design notwithstanding, you haven't shown us enough code to tell what you're doing wrong. Is it Module::SplitEntries that does the split? Does that routine return the @stats array? Does the OtherModule::GetSplitEntries also return this array? What happens?

•Re: Passing an array between multiple modules?
by merlyn (Sage) on Sep 30, 2004 at 00:41 UTC
    I'm trying to split the contents of a certain entry in the database into an array, which is then passed to another module, which then passes the array to another module.
    I smell a bad design.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Well, there are actions being performed on the array as it passes through each module. Basically, I just need to figure out how to pass it around.

      And it probably is a bad design, but it's what I have to work with right now.
        s/probably is/definitely is/

        In 3 different modules you have 3 things named @stats. They are different. That means that no matter how you're thinking about your code, at least two of them have confusing names. For more detail, see the advice on variable naming in Code Complete II.

        To answer your original question, there are three likely confusions that I can see. One is that you're trying to call functions in another module without calling use or require first. The second is that you've written the module but didn't use the package command to make the module name match the names of its functions. The third is that you've somewhere built up a data structure in a function but forgot to return it.

        Still think about merlyn's design comment. Even if you don't know how to avoid a bad design right now, understanding what he reacted to will help you learn.

Re: Passing an array between multiple modules?
by Spidy (Chaplain) on Sep 30, 2004 at 22:02 UTC
    Thanks for all of the design oriented help everyone, but the problem was actually solved a short while after midnight, using references.