Tuna has asked for the wisdom of the Perl Monks concerning the following question:
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.#!/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 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: persistent variables between subroutines (long)
by chipmunk (Parson) on Aug 08, 2001 at 05:29 UTC | |
|
Re: persistent variables between subroutines (long)
by premchai21 (Curate) on Aug 08, 2001 at 06:27 UTC | |
|
Re: persistent variables between subroutines (long)
by mugwumpjism (Hermit) on Aug 08, 2001 at 08:41 UTC | |
by John M. Dlugosz (Monsignor) on Aug 09, 2001 at 02:40 UTC | |
by mugwumpjism (Hermit) on Aug 09, 2001 at 04:31 UTC | |
|
Re: persistent variables between subroutines (long)
by simon.proctor (Vicar) on Aug 08, 2001 at 20:05 UTC | |
|
Re: persistent variables between subroutines (long)
by perlknight (Pilgrim) on Aug 09, 2001 at 00:21 UTC | |
by Tuna (Friar) on Aug 09, 2001 at 00:45 UTC | |
|
Re: persistent variables between subroutines (long)
by John M. Dlugosz (Monsignor) on Aug 09, 2001 at 02:46 UTC |