View Single Post
  #2  
Old 09-11-2009, 11:06 AM
Congdar
Developer
 
Join Date: Jul 2007
Location: my own little world
Posts: 751
Default

Quote:
Originally Posted by joligario View Post
Just to caveat, you should also check if the global is defined before making a == check.
Code:
if(defined($qglobals{rin_quest}) && $qglobals{rin_quest} == 1)
Checking if the global is defined first is good practice. Not doing that will get your log files filled up with perl messages about undefined variable use. The script will still work if you don't check, but when an actual error happens and you need to study the log files, it's nice to not have to wade through Perl error messages that aren't related to the error you are looking for.

In this case where all you are doing is setting a flag once, the second part is not needed.
Code:
if(defined($qglobals{rin_quest}))
is the same as
Code:
if(defined($qglobals{rin_quest}) && $qglobals{rin_quest} == 1)
The second part would only be needed if you were to use the global for further questing and updating the qglobal value to 2, 3, etc.


Additionally, organization of the code helps readability for others should the script need to be updated to extend the quest or change it.
Code:
EVENT_SAY {

if(defined($qglobals{rin_quest}) {

    if($text=~/Hail/i) {
        quest::say("This is the flagged conversation section");
    }

}
else {

    if($text=~/Hail/i) {
        quest::say("This is the NON-flagged conversation section");
    }

}
}
then if later you want to extend the quest using the same qglobal
Code:
EVENT_SAY {

if(defined($qglobals{rin_quest}) {

    if($qglobals{rin_quest} == 1) {

        if($text=~/Hail/i) {
            quest::say("This is the flagged==1 conversation section");
        }
    }

    if($qglobals{rin_quest} == 2) {

        if($text=~/Hail/i) {
            quest::say("This is the flagged==2 conversation section");
        }
    }

}
else {

    if($text=~/Hail/i) {
        quest::say("This is the NON-flagged conversation section");
    }

}
}

Last edited by Congdar; 09-11-2009 at 11:20 AM..
Reply With Quote