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

Hello, I've got a strange possibly obvious problem here. I'm trying to cleanup a perl application and I'm not getting the variable passed to another module for some reason. This thing is kind of a maze but its getting better. Heres a brief rundown:
mat.pl -------------- use lib "lib"; use Utility; use Add; use Group; Add::Add_Acct();
lib/Add.pm -------------- package Add; ### I tryed exporter here with no luck, maybe I did it wrong? ### require Exporter; @ISA = qw(Exporter); @EXPORT = qw($USERID); sub Add_Acct { print "Adding an Account"; &ID; &LAST ; &FIRST; ##a prior dev did this sub calling, its crap I know # +## &USERID; ##This sub takes user input ## print "ID: $ID - UserID: $USERID \n"; ## All vars defined and as e +xpected here ## Group::Groups(); .... ...
lib/Group.pm ------------------- package Group; sub Groups { print "ID: $ID - USERID: $USERID \n"; ### $ID is ok but $USERID is undefined ## .... ..... }
sub USERID { $USERID = Utility::Single_Input('Input Account Name', $USERID, $REQ +UIRED); print "UserInput entered is $USERID \n"; }
Ok so a main program Mat.pl calls the Add.pm module which loads some variables from other subroutines and then calls another module, Group.pm . Right before it calls the other module both $ID and $USERID are defined and good. Once it gets into the Group module the $USERID variable is not defined, its just empty yet $ID seems ok. Whats going on here. Maybe I should add that the &USERID subroutine takes user input, but the user input is returned just fine to the Add_Acct sub and its defined right before the call to Groups. What am I missing here?

Replies are listed 'Best First'.
Re: Strange behaviour with modules
by PerlSufi (Friar) on Mar 06, 2014 at 19:40 UTC
    Hello,
    Honestly, I have no idea what is going on here either. Your description of the problem '..it's kind of a maze..' seems to sum it up well. My first suggestion would be to use Data::Dumper to Dump the contents of every variable you can to make SURE that what you think you are doing is correct.

    Additionally, I would consider completely rewriting the script and module because it is so messy. I wish I could help you more, but a lot of important things seem to be missing from your post- such as the logic sub 'Groups', 'USERID', etc.

    Lastly, I would consider creating a test file with Test::Most while you rewrite this. If you want to be even more awesome to make the code maintainable, use Moose http://search.cpan.org/~ether/Moose-2.1204/lib/Moose/Manual.pod :)
Re: Strange behaviour with modules
by graff (Chancellor) on Mar 06, 2014 at 22:52 UTC
    I'm puzzled about what you're trying to show us. I don't see a module called "Utility" on CPAN, and (assuming it's something specific to your app) I don't see any snippet to show what the "Single_Input()" function does. Could the internals of that module be relevant to the problem?

    I also don't see "use strict" in any of your snippets. If you're allowing all variables to be global by default, then it seems like you're working against the general logic of modules: that they be independent and self-contained with regard to variables.

    Of course, if you do use strict in all your modules, then I gather that you would (should?) also be using our $USERID; where necessary, to make sure that this variable is actually shared across modules as needed (as opposed to being global by default, or "strictly" distinct in each module).

Re: Strange behaviour with modules
by vinoth.ree (Monsignor) on Mar 07, 2014 at 01:57 UTC

    Hey,

    For more clarity add ID and USERID functions.


    All is well