Home > Archive > PERL Beginners > February 2005 > strange if-else problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
strange if-else problem
|
|
| Graeme McLaren 2005-02-28, 8:55 am |
| Morning all. I have a strange problem with an if-else statement. Both
parts of the statement are being executed and I have no idea why, the
offending code is this:
if($password =~ /\S\S\d\d\S\S/){
#if there is a password user then exists in ldap, update the
username
$error = ChangeUsername($old_username, $new_username, 'suppliers',
'select', 'dci');
# make sure user has "approved" status
record_username_success(\@users, $count);
print "username changed from \"$old_username\" to
\"$new_username\" using password \"$password\" \n";
}else{
handle_error($error, \@users, $count);
}
It prints off the username that has been changed and its password. Then the
handle_error function is called.
Anyone got any ideas?
Cheers in advance,
Graeme :)
| |
| Marcello 2005-02-28, 8:55 am |
| Graeme McLaren ha scritto:
> Morning all. I have a strange problem with an if-else statement. Both
> parts of the statement are being executed and I have no idea why, the
> offending code is this:
Try to add two warnings to see if handle_error is called from the "else"
branch if this statement or from one of the routines in the "if" branch.
>
> if($password =~ /\S\S\d\d\S\S/){
warn "Inside IF part";
> #if there is a password user then exists in ldap, update the username
> $error = ChangeUsername($old_username, $new_username, 'suppliers', 'select', 'dci');
> # make sure user has "approved" status
> record_username_success(\@users, $count);
> print "username changed from \"$old_username\" to \"$new_username\" using password \"$password\" \n";
> } else {
warn "Inside ELSE part";
> handle_error($error, \@users, $count);
> }
>
>
> It prints off the username that has been changed and its password. Then
> the handle_error function is called.
>
> Anyone got any ideas?
>
>
> Cheers in advance,
>
> Graeme :)
>
>
>
Just my two cents.
Marcello
| |
|
| On Mon, 28 Feb 2005 10:07:49 +0000, Graeme McLaren
<iamnotregistered@hotmail.com> wrote:
> Morning all. I have a strange problem with an if-else statement. Both
> parts of the statement are being executed and I have no idea why, the
> offending code is this:
>
> if($password =~ /\S\S\d\d\S\S/){
> #if there is a password user then exists in ldap, update the
> username
> $error = ChangeUsername($old_username, $new_username, 'suppliers',
> 'select', 'dci');
> # make sure user has "approved" status
> record_username_success(\@users, $count);
> print "username changed from \"$old_username\" to
> \"$new_username\" using password \"$password\" \n";
> }else{
> handle_error($error, \@users, $count);
> }
>
> It prints off the username that has been changed and its password. Then the
> handle_error function is called.
>
> Anyone got any ideas?
>
> Cheers in advance,
>
> Graeme :)
>
> --
Graeme,
We need a little more code here, or at least some more information:
First, where is handle_error declared? Do you declare handle_error
inside the loop that contains the if block? Since you're not calling
it with &, you must be predeclaring it, and it might be reusing some
variables in way you aren''t expecting. More imporatntly, you haven't
scoped $error properly. It remains set, and will trigger whatever
tests you have for it. Until you unset it. Where else are you
setting it and tersting fo it? Obviously, you expect it to be set in
the else block to call handle_error on it; how is that assignment
happening? Probably when the else block executes, it's using $error
from the last successful execution of the if block.
This script here is exactly whay people constant advise using strict
and warnings in all programs, Turn them on, get your variables
properly scoped and your problems will find themselves.
--jay
|
|
|
|
|