ive had this markov chain irc bot for a long time, and have recently been hacking on it. the thing is, it wont run with 'use strict/warnings' for very long. i cant figure out why, all the nested data structures are driving me insane. can anyone tell me why iy gives me errors (especially at lines 244, 208, 249)? id be very grateful! code:
#!/usr/bin/perl use warnings; use strict; use Storable; use Data::Dumper; use Net::IRC; my $mynick = "wabbylegs"; my $ident = "quetzal"; my $server = "irc.whackpack.com"; my $channel = "#"; my $public = 1; my $learn = 1; my @words = ({}); my %wordhash = (); my $sleeptime = 5; my $irc = new Net::IRC; my $connection = $irc->newconn(Nick => $mynick, Username => $ident, Server => $server, Port => "6667", Ircname => "dont whois me" ) or die "Cant connect to $server.\n"; my %commands = (savedict => \&savedict, loaddict => \&loaddict, die => \&die, quiet => \&quiet, loud => \&loud, stats => \&stats, dumpdict => \&dumpdict, ); $connection->add_handler('376', \&on_connect); # end of motd $connection->add_handler('public', \&on_public); $connection->add_handler('msg', \&on_private); $connection->add_global_handler('disconnect', \&on_disconnect); $irc->start; ## ### Subroutines. ## sub on_connect() { my $self = shift; $self->join($channel); } sub on_public { my($self, $event) = @_; my($text) = ($event->args); my $from = $event->nick; $text = lc $text; if($sleeptime <= 0) { if($public) { $self->privmsg($channel, spew()); } $sleeptime = sprintf "%d", (rand 2) + 5; } $sleeptime--; print "Msgs to spew: $sleeptime\n"; print "msg: $text\n"; if($text =~ /^$mynick: (\w+)/) { my $cmd = $commands{$1}; print "Got command: $1 ($cmd)\n"; if(defined($cmd)) { &$cmd($self, $event, $from, $channel); } } else { if ($learn) { add_sentence($text); } } } sub on_private { my $i = 0; my($self, $event) = @_; my($text) = ($event->args); my $pnick = $event->nick; $text =~ /^(\w+)/; if(defined($commands{$1})) { my $cmd = $commands{$1}; print "Got private command: $1 ($cmd)\n"; &$cmd($self, $event, $pnick, $pnick); } else { $self->privmsg($pnick, spew()); } } sub savedict($$$$) { my ($self, $event, $nick, $chan) = @_; store \@words, "mydict"; $self->privmsg($chan, "Saved " . scalar @words . " words to mydict +."); } sub loaddict() { my ($self, $event, $nick, $chan) = @_; my $w = retrieve "mydict"; @words = @{$w}; $self->privmsg($chan, "Loaded ".scalar @words." from mydict."); } sub die() { my ($self, $event, $nick, $chan) = @_; $nick = $event->{nick}; print "got die from $nick in $chan\n"; if($nick eq "nick") { $self->privmsg($channel, "$nick killed me!!"); $self->quit("$nick killed me."); exit 0; } else { $self->privmsg($nick, "I won't die unless nick tells me.\n"); return 0; } } sub stats() { my ($self, $event, $nick, $chan) = @_; my $nwords = scalar @words; my $average = 0; foreach(@words) { $average += scalar @{$_->{num}}; } $average /= $nwords; $average = sprintf "%.2f", $average; my $stats = "I know $nwords words, with an average of $average con +nections between each word. I am waiting for $sleeptime mo$ $self->privmsg($chan, $stats); } sub quiet() { my ($self, $event, $nick, $chan) = @_; $public = 0; $self->privmsg($chan, "Shutting up, $nick."); } sub loud() { my ($self, $event, $nick, $chan) = @_; $public = 1; $self->privmsg($chan, "I'll speak now, $nick."); } sub dumpdict() { my ($self, $event, $nick, $chan) = @_; my $i = 0; open FILE, ">textdict"; foreach(@words) { printf FILE "%4d Word: $_->{word} ", $i; print FILE join "|", @{$_->{num}}; print FILE "\n"; $i++; } close FILE; } sub spew() { my $rnd = sprintf "%d", rand scalar @{$words[0]->{num}}; my $start = @{$words[0]->{num}}[$rnd-1]; my $s = "\u$words[$start]->{word}"; my $next = $start; while($next != -1) { $rnd = sprintf "%d", rand scalar @{$words[$next]->{num}}; $next = @{$words[$next]->{num}}[$rnd]; if($next != -1) { $s .= " $words[$next]->{word}"; } } $s .= "."; $s =~ s/ /, /g; return $s; } sub add_sentence() { my $string = lc shift; my @s = split "[.,!?]", $string; foreach(@s) { s/[^a-z-A-Z0-9 \t']+//g; my @w = split " "; my $next_push = -1; my $old_index = 0; foreach(@w) { my $index = scalar(@words); my $i = find_word($_); my $new_index = ($i == -1)?$index:$i; if($i == -1) { $words[$new_index] = {word => $_}; } else { } push @{$words[$old_index]->{num}}, $new_index; $string =~ /^(\w+)/; if($1 eq $_) { push @{$words[0]->{num}}, $index; } $string =~ /(\w+)$/; if($1 eq $_) { push @{$words[$new_index]->{num}}, -1; } $old_index = $new_index; } } } sub find_word() { my $word = shift; if(defined $wordhash{$word}) { return $wordhash{$word}; } for(my $i = 0; $i < scalar(@words); $i++) { if($words[$i]->{word} eq $word) { $wordhash{$word} = $i; return $i; } } return -1; } sub on_disconnect { my ($self, $event) = @_; print "Disconnected from ", $event->from(), " (", ($event->args()) +[0], "). Attempting to reconnect...\n"; $self->connect(); }

In reply to wont run for long with strict. by rdnzl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.