I have the dubious task of taking someone else's code, and making it work using warnings and strict pragma. The problem is (aside from not using strict and -w)that this person created variables which persist from subroutine to subroutine. Here's an example:
#!/usr/bin/perl $program = "cluster_f$%k"; @list = ("Maleah"); $sshPort = 22; require "flush.pl"; @taskSequence = ( 'establishSession~~connecting to $clusterHost' ); foreach $clusterHost (@list) { $executionStatus = &executeTaskSequence($clusterHost, @taskSequence) | +| $totalErrorCount++; print "\n$prog_name: aborted reactivation of $clusterHost\n\n"; if ($executionStatus || $cleanUpStatus ) { $totalErrorCount++; print "\n$prog_name: aborted reactivation of $clusterHost\n\n"; } else { print "\n$prog_name: copied all files from $clusterHost\n\n"; } if (@list) { local ($operations) = "host"; printf "\n$prog_name statistics:\n"; printf " %d ${operations}s attempted\n", scalar(@list); printf " %d $operations(s) succeeded\n", @list - $totalErrorCount +; printf " %d $operations(s) failed\n", $totalErrorCount; } ############################################################# sub establishSession { local @sshPort; if ($clusterHost =~ s/:(\d+)$// || $sshPort =~ /(\d+)/) { @sshPort = ("port", $1); } &xferOpen (SESSION, $clusterHost, "compression", "yes", @sshPort) || return &error ("couldn't connect to $clusterHost: $xferError"); return $success; } ############################################################# sub xferOpen { local ($sessionHandle, $remoteHost, %options) = @_; if ($xferSessionInfo{$sessionHandle}) {$xferError = "session already open"; return 0} # find fastest supported cipher for host foreach $cipher (@xferSshCiphersToTryInOrderOfSpeed) { local ($xferError); $xferSessionInfo{$sessionHandle} = join("~", "host", $remoteHost, "cipher", $cipher, %options); &xfer_ssh("/bin/true") || return 1; } return 0; } ############################################################# sub xfer_ssh { local ($remoteCommand) = @_; local ($command, *READ, *WRITE, *ERROR, @error, $pid); local (%opt) = split(/~/, $xferSessionInfo{$sessionHandle}); if (! %opt) {$xferError = "invalid session handle"; return 0} $command = $xferSshCommand; if ($opt{'port'}) {$command .= " -p $opt{'port'}"} if ($opt{'username'}) {$opt{'host'} = "$opt{'username'}\@$opt{'ho +st'}"} if ($opt{'identity'}) {$command .= " -identity $opt{'identity'}"} if ($opt{'cipher'}) {$command .= " -c $opt{'cipher'}"} if ($opt{'compression'} eq "yes") {$command .= " '-oCompression yes' +"} if ($opt{'compression'} eq "no") {$command .= " '-oCompression no'" +} $command .= " $opt{'host'} $remoteCommand"; print "command var in xf +er_ssh = $command\n"; $debug && print "xfer.pl: spawning $command\n"; $pid = &open3 (WRITE, READ, ERROR, $command); close (WRITE); @output = <READ>; @error = <ERROR>; close (READ); close (ERROR); waitpid($pid, 0); $? || return 1; $xferError = "@error";print "xfererror = $xferError\n"; return 0; } ############################################################# sub error { local ($_) = @_; print "$program: ERROR: $_\n"; return $failure; } ############################################################# sub executeTaskSequence { local ($objectName, @taskSequence) = @_; local ($errorCount); foreach (@taskSequence) { ($task, $executionConstraints, $taskDescription) = split(/\s*~\s*/ +, $_); if (! $executionConstraints || eval "$executionConstraints") { if ($reportProgress && $taskDescription) {&reportProgress ($objectName, $taskDescription)} &flush (STDOUT); &$task ($objectName) && ++$errorCount && last } &flush (STDOUT); } return ($errorCount) } ############################################################# #sub reportProgress { local ($objectName, $taskDescription)=@_; if ($taskDescription = eval "\"$taskDescription\"") {printf "\n$program: %s (%s)\n", $taskDescription, $objectName} } #############################################################
The code above, run as is, (assuming I haven't forgot to include all variables =) ), will attempt to establish an ssh connection to localhost (for testing). However, I need to incorporate this code, and about 800 lines just like it into another program, which runs using warnings and strict pragma.

The problem I am having, is sharing variables between each subroutine. I don't know how to do it.
For example, in &xferOpen, I need to make %xferSessionInfo available to &xfer_ssh, and a few other subroutines. If someone, could explain how to make variables available throughout each subroutine, and possibly some general advice regarding transposing code which was written without -w and strict, into a program using -w and strict. That would be MUCH appreciated!

Thanks,
Steve


In reply to 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.