http://www.perlmonks.org?node_id=1004097

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

Hi, I am having problems with a small game in perl, when someone types !present in a channel on irc, it keeps saying " <rainbowwarrior> Stop hogging all the best pitches Nobody, let someone else try first! " Can someone please help me so when someone types in !present it gives them a present please ? Thank You , my code is below :-
#!/usr/bin/perl # # Flashy's bloodsports. http://digdilem.org/ - "Because thems trouts a +in't gonna catch themselves!" # (Credit to the original Eggdrop TCL script by Nerfbendr) # # Adds silly !hunt, !fish and !trophy and !present public triggers # Also !newmonth will clean out the trophy cupboard. (Only if you do i +t, or the nick in $owner_nick) # # Optional Configuration (Will work fine without changing these, but y +ou can if you like) my $owner_nick='rainbowwarrior'; # Your nick - you can only reset the +scores remotely if you use this nick. my $scale = 'kg'; # Say here whether you want to measure weights in lb + or kg. my $catch_percent=90; # How often you catch or shoot something. my $trophy_cabinet = Xchat::get_info( 'xchatdir' ) . "\\trophies.txt"; + # File to keep the trophies in. # End user configuration use strict; Xchat::register( "rainbowwarrrior's Huntin' 'n Fishin' and present unw +rapping", 'v.004', "Hunting", "" ); Xchat::hook_print('Channel Message', "hunting"); Xchat::hook_print('Your Message', "hunting"); Xchat::hook_print('Private Message', "hunting"); Xchat::hook_print('Private Message to Dialog', "hunting"); my $bigfish=0,my $fishman='Nobody',my $bighunt=0,my $huntman='Nobody', +my $bigpresent=0,my $presenthunt='Nobody',my $present_unwrapper; my $fishtype='Trout',my $fishplace='Pool'; my $hunttype='bear', my $huntplace='Bushes'; my $presenttype='present', my $presentplaces='xmas'; my $last_hunter; my $last_fisher; my $last_presenthunter; my @hunts = ( "bear","gopher","rabbit","hunter","deer","fox","duck","m +oose","pokemon named Pikachu","park ranger","Yogi Bear","Boo Boo Bear +","dog named Benji","cow","raccoon","koala bear","camper","channel la +mer"); my @fish = ( "Salmon","Herring","Yellowfin Tuna","Pink Salmon","Chub", +"Barbel","Perch","Northern Pike","Brown Trout","Arctic Char","Roach", +"Brayling","Bleak","Cat Fish","Sun Fish","Old Tire","Rusty Tin Can"," +Genie Lamp","Love Message In A Bottle","Old Log","Rubber Boot","Dead +Body","Loch Ness Monster","Old Fishing Lure","Piece of the Titanic"," +Chunk of Atlantis","Squid","Whale","Dolphin","Porpoise","Stingray","S +ubmarine","Seal","Seahorse","Jellyfish","Starfish","Electric Eel","Gr +eat White Shark","Scuba Diver","X","Lag Monster","Virus","Soggy Pack +of Smokes","Pile of Weed","Boat Anchor","Pair Of Floaties","Mermaid", +"Merman","Halibut","Tiddler","Sock","Trout"); my @presents = ( "a new car","cake","guitar","dvd","xbox360","playstat +ion3","hairdryer","trampoline","dog","cat","electric shock game","a b +oard game","a snake","tickets to a rock concert","a blowup doll","boo +k"); my @huntplaces = ("in some bushes","in a hunting blind","in a hole","u +p in a tree","in a hiding place","out in the open","in the middle of +a field","downtown","on a street corner","at the local mall"); my @fishplaces = ("Stream","Lake","River","Pond","Ocean","Bathtub","Ki +ddie's Swimming Pool","Toilet","Pile of Vomit","Pool of Urine","Kitch +en Sink","Bathroom Sink","Mud Puddle","Pail of Water","Bowl of Jell-O + (tm)","Wash Basin","Rain Barrel","Aquarium","SnowBank","WaterFall"," +Cup of Coffee","Glass of Milk"); my @presentplaces = ("in a cupboard","under a xmas tree","in the liver +room","up in a tree","in a hiding place","out in the open","upstairs" +,"on a boat","on a street corner","at the local mall"); Xchat::print("Path=($trophy_cabinet"); if (open (DH,"<$trophy_cabinet>")) { ($bigfish,$fishman,$fishtype,$fishplace) = split(/\|/,<DH>); chomp($fishplace); ($bighunt,$huntman,$hunttype,$huntplace) = split(/\|/,<DH>); my ($bigpresent,$presenthunt,$presenttype,$present_unwrapper,$ +presentplaces) = split(/\|/,<DH>); close (DH); Xchat::print("Peered in the trophy cabinet: ($bigfish$scale $fisht +ype by $fishman) ($bighunt$scale $hunttype by $huntman) ($bigpresent$ +scale $presenttype by $present_unwrapper"); } else { Xchat::print("\002Woo, looks like we've not gone hunting before. L +et's make a trophy cabinet..."); save_trophy(); } Xchat::print("\002Loaded Flash's Huntin' 'n Fishin'\002 (!hunt, !fish +!present !trophy - Current records are $bigfish$scale and $bighunt$sc +ale)"); sub hunting { my @pubwords = split(/ /,$_[0][1]); my $hunter = Xchat::strip_code($_[0][0]); my $channel = Xchat::get_info('channel'); if (lc($pubwords[0]) eq '!hunt') { if ($hunter eq $last_hunter) { Xchat::command("say Stop hogging all the best pitches $hun +ter, let someone else try first!"); return Xchat::EAT_NONE; } else { $last_hunter = $hunter; } my $huntplace = @huntplaces[rand(scalar @huntplaces)]; my $hunt = @hunts[rand(scalar @hunts)]; my $weight = 1+int(rand($bighunt+10)); Xchat::command("msg $hunter You hide $huntplace and wait for s +omething to wander by..."); Xchat::command("msg $hunter ."); Xchat::command("msg $hunter .."); Xchat::command("msg $hunter ..."); Xchat::command("msg $hunter You think you hear something and f +ire wildly in that direction!"); if (rand(100)<$catch_percent) { Xchat::command("msg $hunter Congratulations, $hunter! You +just bagged yourself a $weight$scale $hunt!"); Xchat::command("msg $channel $hunter just bagged a $weight +$scale $hunt."); if ($weight > $bighunt) { Xchat::command("msg $hunter Wow!!! That's a new record +! Way to go, $hunter! Type !trophy to see it!"); Xchat::command("msg $channel Wow! That breaks the old +record of a $bighunt$scale $hunttype! $hunter is amazing!"); $bighunt=$weight; $huntman=$hunter; $hunttype=$hunt; save_trophy(); } } else { Xchat::command("msg $hunter Rats...you missed it, $hunter! + Better luck next time!"); Xchat::command("msg $channel $hunter is useless, they miss +ed by a mile!"); } } if (lc($pubwords[0]) eq '!fish') { $fishplace = @fishplaces[rand(scalar @fishplaces)]; my $fishy = @fish[rand(scalar @fish)]; my $weight = 1+int(rand($bigfish+10)); if ($hunter eq $last_fisher) { Xchat::command("say Stop hogging all the best pitches $hun +ter, let someone else try first!"); return Xchat::EAT_NONE; } else { $last_fisher = $hunter; } Xchat::command("msg $hunter You cast your line into a $fishpla +ce and wait for a bite..."); Xchat::command("msg $hunter ."); Xchat::command("msg $hunter .."); Xchat::command("msg $hunter ..."); Xchat::command("msg $hunter You feel a tug on your line and re +el it in..."); if (rand(100)<$catch_percent) { Xchat::command("msg $hunter Congratulations, $hunter! You +just caught yourself a $weight$scale $fishy!"); Xchat::command("msg $channel $hunter just caught a $weight +$scale $fishy"); if ($weight > $bigfish) { Xchat::command("msg $hunter Wow!!! That's a new record +! Way to go, $hunter! Type !trophy to see it!"); Xchat::command("msg $channel Brilliant! That breaks th +e old record of a $bigfish$scale $fishtype! $hunter is the world's be +st!"); $fishman=$hunter; $bigfish=$weight; $fishtype=$fishy; save_trophy(); } } else { Xchat::command("msg $hunter Rats...it got away, $hunter! B +etter luck next time!"); Xchat::command("msg $channel $hunter is useless, they fail +ed to catch anything!"); } } if (lc($pubwords[0]) eq '!trophy') { Xchat::command("say $fishman holds the fishing record when the +y caught a $bigfish$scale $fishtype"); Xchat::command("say $huntman holds the hunting record when the +y bagged a $bighunt$scale $hunttype"); Xchat::command("say $presenthunt holds the present unw +rapping record when they unwrapped a $bigpresent$scale $presenttype") +; } if (lc($pubwords[0]) eq '!present') { my $presentplaces = @presentplaces[rand(scalar @presentplaces) +]; my $present = $presents[int rand @presents]; my $weight = 1+int(rand($present+10)); if ($present_unwrapper eq $last_presenthunter) { Xchat::command("say Stop hogging all the best pitches $pre +senthunt, let someone else try first!"); return Xchat::EAT_NONE; } else { "my \$last_present = " } Xchat::command("msg $present_unwrapper You unwrap your present + from $presentplaces and see what you get..."); Xchat::command("msg $present_unwrapper ."); Xchat::command("msg $present_unwrapper .."); Xchat::command("msg $present_unwrapper ..."); Xchat::command("msg $present_unwrapper You unwrap and see ..." +); if (rand(100)<$catch_percent) { Xchat::command("msg $present_unwrapper Congratulations, $p +resent_unwrapper! You just unwrapped a $weight$scale $present!"); Xchat::command("msg $channel $present_unwrapper just got a + $weight$scale $present"); if ($weight > $present) { Xchat::command("msg $present_unwrapper Wow!!! That's a + new record! Way to go, $present_unwrapper! Type !trophy to see it!") +; Xchat::command("msg $channel Brilliant! That breaks th +e old record of a $present$scale $presenttype! $present_unwrapper is +the world's best!"); $present_unwrapper=$present_unwrapper; $present=$weight; $presenttype=$present; save_trophy(); } } else { Xchat::command("msg $present_unwrapper Sorry no present th +is time $present_unwrapper! Better luck next time!"); Xchat::command("msg $channel $present_unwrapper is useless +, they failed to get a present!"); } } if (lc($pubwords[0]) eq '!newmonth') { my $curnick = Xchat::get_info('nick'); if ((lc($hunter) eq lc($owner_nick)) or ($hunter eq Xchat::get +_info('nick'))) { $bigfish=0; $fishman='Nobody'; $fishtype='Tiddler'; $fishp +lace='Toilet'; $bighunt=0; $huntman='Nobody'; $hunttype='Haggis'; $huntpl +ace='Bush'; $bigpresent=0; $present_unwrapper='Nobody'; $p +resenttype='xmas'; $presentplaces='everywhere'; save_trophy(); Xchat::command("say It's a new month, all existing huntin' + 'n fishin' and present unwrapping records are reset!"); } else { Xchat::command("say Who are you, $present_unwrapp +er to tell me to change the month?"); } } } sub save_trophy { open (DH, ">$trophy_cabinet") or die("Bah! Can't open the trophy c +abinet to push this 'ere trophy in!"); print (DH "$bigfish|$fishman|$fishtype|$fishplace\n"); print (DH "$bighunt|$huntman|$hunttype|$huntplace"); print (DH "$bigpresent|$present_unwrapper|$presenttype|$presen +tplaces"); close (DH); }

Replies are listed 'Best First'.
Re: Help with perl game
by GrandFather (Saint) on Nov 16, 2012 at 04:35 UTC
    "my \$last_present = ";

    looks pretty strange. What do you mean by it?

    True laziness is hard work
Re: Help with perl game
by Anonymous Monk on Nov 16, 2012 at 05:30 UTC

    Since Grandfather appears to have solved your problem already, may I recommend dominus's Higher-Order Perl, chapter 2 "Dispatch tables"?

      Hi, i am still not sure what is wrong with it, please help and thank you.

        Hi, i am still not sure what is wrong with it, please help and thank you.

        If you're not sure what its supposed to do that its not doing, how is anyone?

        This looks wrong  if (open (DH,"<$trophy_cabinet>")) {

        You probably meant if( open DH, '<', $trophy_cabinet ){

        I didn't look past that glaring error , because I don't Xchat::

        Well, what is wrong with line 132? (The line Grandfather mentioned.) Compare it to the similar blocks of code on lines 62 and 96. What is different? And what about line 128? Compare that one to line 132 too.

        If I turn on the use warnings pragma, I get the following error from Perl:

        Useless use of a constant (my $last_present = ) in void context at hun +ter2.pl line 132.

        It sounds like you're throwing something away. A string, to be precise. Is there supposed to be a string there? Or is it supposed to be a variable assignment?