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

Fairly neub here, but here is my issue. I built a custom tool for Unix user accounts to sync multi server environment for (lazy) management reasons. In the tool I do lots of Unix commands, changing passwords, creating users, removing users, locking users, you get the idea. Each task is fairly simple but for ease of maintenance each on is built as a subroutine. I have several global variables that I pass back and forth between the sub's. For a while this was fine, the sub would modify the global var and the other subs could read it just fine. But after a while they stopped being able to read the global vars after they were modified by the other subs. I currently have 23 subroutines defined. Seems like this happens 2 to 3 subs deep. I started just passing the info direct to the next sub, but there has got to be a reason for this behavior, something that I just don't understand about Perl yet. Any thoughts on this would be greatly appreciated, I just don't know what else to look at.
  • Comment on Global var's that don't hold between subroutines

Replies are listed 'Best First'.
Re: Global var's that don't hold between subroutines
by John M. Dlugosz (Monsignor) on May 12, 2009 at 18:20 UTC
    Re I have several global variables that I pass back and forth between the sub's. ... the sub would modify the global var and the other subs could read it just fine

    What do you mean by "pass back and forth" there? If each sub just accessed the global, you're not passing anything. If you are "passing" something too, maybe you're getting something mixed up.

    You need to post an example of what you are doing. If that is indeed odd, we'll be able to tell.

    Oh, and make sure you use strict and use warnings.

    —John

      Ah the simple answer is usually the best answer. I forgot I had turned of use strict a while back and not turned it back on. Once I fixed the 30+ errors it pointed out. I looked closer and found the following newb error:

      print "CCL:08::MyServers = @MyServers\n";
      if(undef @MyServers){ &ChooseLocation; }
      print "CCL:09::MyServers = @MyServers\n";

      The print statements were simply to point out that I did not understand the correct use for undef, as an action and not as a test, or my syntax is wrong for it. I changed it to the following:

      unless(@MyServers){ &ChooseLocation; }

      Thanks for pointing me back to the basics, it is usually that anyway. =-D
        Dear Initiate,

        I'm glad we've been of help, and so quickly, with your first foray into the Monastery.

        In your posts, you should look at the available markup. I know you already figured out to use explicit paragraph markers, which is something that often trips up first-timers. The next thing to know is the <code> tag, around your code items and preformatted listings.

        You can always go back and edit your post. So you could, if you wanted to practice at it, insert the code tags and fix your typo (missing 'f').

        Yes, undef is an action, not a test. The test is defined. So you could write unless (defined $A) {...}

        Your final line could also be written the other way around:

        &ChooseLocation unless @MyServers;
        which has its advantages. Besides the lack of braces and parens, think about how it flows linguistically, once you get used to the idea. You see the action up front. The rest of it, "oh, skip that step if it's not applicable" is off to the side, not the main part of the statement. The core logic reads cleaner.

        Also, know that you are testing for an empty array, not defined-ness. I'm pretty sure that a named array variable can't test as undef itself. It can hold any number of elements, some of which may be undef. But itself is empty, still existing as a container.

        Oddly enough the documentation (undef) shows an example of undef'ing an array, but doesn't say what it means. Meanwhile testing for definedness on arrays doesn't work, and used to mean something a bit different.

        —John