Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

returns not working

by Markn (Initiate)
on Aug 24, 2005 at 15:29 UTC ( [id://486233]=perlquestion: print w/replies, xml ) Need Help??

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

I've got a subroutine that I'm trying to use return's in. At first I tried returning a value with return(0); or return(1); which did not seem to work. For the most part, if I just use return; it does work EXCEPT as noted in the code below. What am I not understanding about returns or What did I mess up? *note only one value is passed to the subroutine, a userid.*
sub CheckDeployed { chomp (my $id = $_); my $deployedcounter = 0; foreach (<@deployeduid>) { chomp $_; if ( $id eq $_ ) { print "I found $id in the deployedfile with the timestamp of $de +ployedtimestamp[$deployedcounter]\n"; my $temptime = time; # Set comparetime to current time - 4 days my $comparetime = ($temptime - 86400 * 3); if ($deployedtimestamp[$deployedcounter] < $comparetime) { my $oldtime = $deployedtimestamp[$deployedcounter]; chomp ($deployedtimestamp[$deployedcounter] = $temptime); print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounte +r] 2\n" or die $!; return; ### Jumps back to if ( $id eq $_ ) not sure why. } else { chomp $deployedtimestamp[$deployedcounter]; print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounter] 1 +\n" or die $!; return; } $deployedcounter++; } elsif ( $_ ne "" ) { #reserved for possible processing } $deployedcounter++ } my $temptime = time; chomp ($deployedtimestamp[$deployedcounter] = $temptime); print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounter] 1\n +" or die $!; return; }

Replies are listed 'Best First'.
Re: returns not working
by chromatic (Archbishop) on Aug 24, 2005 at 15:51 UTC

    There are a couple of serious problems here.

    chomp (my $id = $_);

    $_ has nothing to do with the arguments to a subroutine. If you're actually passing in a value, use my $id = shift;. I don't know why you're chomping it here.

    foreach (<@deployeduid>)

    You don't need the angle brackets to iterate over a list. I suspect (but don't have time to confirm) that Perl treats this as a glob operation and doesn't give you the values that you expect. If that's the case, your conditional statements will be completely wrong and it may look like return is "not working".

      Thanks for the reply. I've modified the code per your input. with return 1; it loops back to the: #chomp $_; if ( $id eq $_ ) with just return; it falls out of the subroutine with the error: Use of uninitialized value in numeric eq (==) at esdeploy_new.pl line 184 Calling the code with: *$currentid = SMITHJO* and @deployedfile contains SMITHJO and SMITHJA*
      if ( CheckDeployed( $currentid ) == 0) { # do something }
      sub CheckDeployed { #chomp (my $id = $_); my $id = shift; my $deployedcounter = 0; foreach (@deployeduid) { #chomp $_; if ( $id eq $_ ) { print "I found $id in the deployedfile with the timestamp of $de +ployedtimestamp[$deployedcounter]\n"; my $temptime = time; my $comparetime = ($temptime - 86400 * 3); if ($deployedtimestamp[$deployedcounter] < $comparetime) { my $oldtime = $deployedtimestamp[$deployedcounter]; chomp ($deployedtimestamp[$deployedcounter] = $temptime); print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounte +r] 2\n" or die $!; return "0"; } else { chomp $deployedtimestamp[$deployedcounter]; print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounte +r] 1\n" or die $!; return 1; } $deployedcounter++; } elsif ( $_ ne "" ) { #reserved } $deployedcounter++ } my $temptime = time; chomp ($deployedtimestamp[$deployedcounter] = $temptime); print TEMPDEPLOYED "$id $deployedtimestamp[$deployedcounter] 1\n +" or die $!; return "2"; }

        Do you mean to loop over @deployedfile or @deployeduid?

        I also suspect your conditionals may be incorrect. Your indentation makes it very difficult to follow, though that could be an artifact of copying and pasting.

        Why are you chomping so often? It's an idempotent operation if you've already removed trailing input record separators, but if you clean the data where you have it, you can remove that code.

Re: returns not working
by davido (Cardinal) on Aug 24, 2005 at 15:51 UTC

    This concerns me:

    sub CheckDeployed { chomp (my $id = $_);

    How is $_ getting populated? Are you passing a value into your subroutine through the use of global osmosis? Or did you really mean:

    chomp( my $id = $_[0] );

    That right there could be an issue.

    And it's possible that all of your 'if's are failing, so that the subroutine falls through to the final return which has no argument, and as a result, returns undef in scalar context or an empty list in list context.


    Dave

Re: returns not working
by ikegami (Patriarch) on Aug 24, 2005 at 15:48 UTC

    I don't see any reason why it would do what you say. Are you sure CheckDeployed isn't called more than once? You could verify this by adding warn("Called CheckDeployed.\n") as the first line of the function.

Re: returns not working
by Fletch (Bishop) on Aug 24, 2005 at 15:51 UTC

    Read perldoc perldebug; watch exactly what path it's taking through your code.

    --
    We're looking for people in ATL

Re: returns not working
by Markn (Initiate) on Aug 27, 2005 at 17:58 UTC
    Thank you all for the input and suggestions.

    What I ended up doing was removing all the returns, replacing them with a variable I set based on which condition was met.
    I wrapped it all in a while ( $status = 99 ) and set the status to the exit value I wanted based on the condition.
    Then I used just one return at the end, which passes the $status and variable back to the calling function and it works just fine.
    I'm well aware that I need some serious help in the formating of code and I'll be looking for references on how to do this, but this was (as seems to be a recuring theme) an urgent, must do now sort of thing and with my very limited understanding of any programming in general, I'm just happy I got it to work.

    Again, thank you all for the input.

Re: returns not working
by socketdave (Curate) on Aug 24, 2005 at 15:35 UTC
    I generally don't enclose the return code in parentheses: return 0;

    If you aren't, you should use warnings; it may give you a clue to what's going on. You say that return (1) doesn't seem to work. What does it do?

      The use of parenthesis around the parameter for return has no effect other than possibly to disambiguate the syntax. In other words, return 0; and return(0) will do the same thing.


      Dave

        i think i started doing this out of some vague uneasiness about return handling scalars or lists. now that i think about it, it's pretty much a personal style thing at this point. which do you think is more appropriate?
      I am using warnings in the main body of the code. if I just use a plain return, it falls out at the return (generally). I am stepping through the code line by line and watching this happen.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://486233]
Approved by saberworks
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found