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".
| [reply] [d/l] [select] |
|
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";
}
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
Re: returns not working
by davido (Cardinal) on Aug 24, 2005 at 15:51 UTC
|
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.
| [reply] [d/l] [select] |
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.
| [reply] [d/l] |
Re: returns not working
by Fletch (Bishop) on Aug 24, 2005 at 15:51 UTC
|
| [reply] [d/l] |
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. | [reply] |
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? | [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
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?
| [reply] |
|
|
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.
| [reply] |