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

I have a program that generates a hash and starts passing it to misc subroutines for them to act on. I am trying to generate a return code from GPG encryption (one of the subroutines) and if it errors, have the return code be passed back out to the main subroutine which then prints an error and exits if the return code is "1". The problem Im having is that its not working - here is an ex of how its layed out:
if (%hash = sub(%temphash) != 0) { print "ERROR! - sumthin broke\n"; $error = 1; exit; } (nothing important here) return(1) if ($error != 0);
where the subroutine just does the encryption of a text file and moves it to a different directory. I have the encryption subroutine exit and print $@ if it has a value, but the whole thing still exits... ideas? (I ask knowing you are prolly gonna want the actual code here, but sorry - its at work) oakley

Replies are listed 'Best First'.
Re: return codes from embedded subroutines
by chromatic (Archbishop) on Oct 24, 2000 at 02:03 UTC
    Are you looking for something like this?
    use vars qw( $error ); sub do_lots { $error = 0; # assume things are correct unless told otherwise my %hash = @_; %hash = mangle_once(%hash); return $error if $error; %hash = mangle_twice(%hash); return $error if $error; # ... as necessary }
    That assumes the embedded subs set the global $error when there's a problem.

    You could also return hash refs from your embedded subs, capture them in a temporary value, and check for definedness:

    my $result; $result = mangle_once(%hash); return $error unless defined $result; # et cetera
    Returning either an error code or a flattened hash can be a little trickier, and I probably wouldn't do it as it would make the code less clear.
Re: return codes from embedded subroutines
by Fastolfe (Vicar) on Oct 24, 2000 at 18:33 UTC
    You're using words like 'subroutine' but you seem to imply that you are returning your values via exit and $@, which tells me you're evaling code instead of calling it as a straightforward subroutine.

    If this is the case, your 'exit' return value isn't going to magically appear in $@. You'll need to examine the value of $? if you want to do it that way (but be warned, the value of this will need to be << 8'd to get at the number you passed to exit).

    sub some_sub { ... return 5; # not 'exit', as this terminates the script } $returned = &some_sub; # 5 eval "exit(&some_sub)"; # exits block with return value of 5 $returned = $? << 8; # 5
    Obviously, the first method is far more efficient and practical than evaluating code. I hope I'm not misunderstanding what you're doing here. You may be interested in perlsub and documentation for eval and perlvar (for $? and $@).
Re: return codes from embedded subroutines
by oakley (Scribe) on Oct 25, 2000 at 16:13 UTC
    my apologies on not being clearer. You would be correct in what you are thinking. Lemme kinda give a flow here:
    1. hash is created 2. passed to statinfo subroutine 3. passed to encrypt or decrypt subroutine from statinfo a. eval is run over gpg encryption/decryption 4. file(s) are moved to where they need to be 5. email is sent (proggie done)
    Here is a few snippets from the program:
    my(%temphash) = %workhash; if (%workhash = &statinfo(%temphash) != 0) { print "ERR -> statinfo sub\n"; exit; } sub statinfo { # bunch of stuff here - actually gathering of information # from stat function my(%temphash) = %workhash; if ( $workhash{abbr} =~ /lmr/lamer/ ) { %workhash = &ezip(%temphash); } elsif ( ($workhash{encrypt} eq "pgp") && ($workhash{tranmeth} eq "put +") ) { %workhash = &encrypt(%temphash); } elsif ( ($workhash{encrypt} eq "pgp") && ($workhash{tranmeth} eq "get +") ) { %workhash = &decrypt(%temphash); } else { print "ERR! - dont know what to do!\n"; exit(1); } }
    Now, inside any of THOSE subs (ones directly above), an eval{} runs on the process to make sure it went alright.. im starting to think im a complete idiot here and not sure where to go from this point... did this help any? I hope someone can give this partial newbie a hand.. thanks