Here's the general process.

Go through all of the subroutines and ensure that every global variable that should be a parameter to the function is actually passed as a parameter. Also ensure that all return information from the function is returned via return.

You now have a whole load of parts that you can prove individually must work, if their inputs are sane. This is good.

If you're confused about how to keep information persistent between subroutine calls, and you "outgrow" simple use of "return", here is my advice: Pass a reference to a data structures holding the information you want as the first argument to a function. eg:

sub xfer_ssh { my ($xferSessionInfo, @other_args) = (@_); # set something in %xferSessionInfo - note use of the # -> operator to dereference. $xferSessionInfo->{foo} = "bar"; # exactly the same thing, written a way that some find # easier to grok. ${ $xferSessionInfo }{foo} = "bar"; return "ok"; } my %xferSessionInfo; &xfer_ssh(\%xferSessionInfo, @other_args); print $xferSessionInfo{foo}; # prints "bar"

Group your functions by what you are expecting to use as this first argument. eg, all of the functions that deal with "%xferSessionInfo" hashes together. Where possible, only make changes to the state variable that is the first argument to the function.

Practice this coding style for a while; it should be well within your reach. After you have mastered this technique, then read the perltoot man page (I wouldn't recommend reading it just yet).


In reply to Re: persistent variables between subroutines (long) by mugwumpjism
in thread persistent variables between subroutines (long) by Tuna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.