sj said:
Sorry, I'm completely new to both programming and usenet, I'll attempt
to pick it up as fast as possible.
I *think* it's the 'special' and 'checkinput' subs that are having the
problems...
Thank you for making the effort to help us help you.
#use strict;
#use warnings;
use strict and warnings are very valuable tools. However, they don't
work if they are commented out. Some people here will not look at your
code unless both of these are present.
my $checked2;
my $flag = 1;
my $flag2 = 1;
while ($flag) {
print $flag;
my $usec;
if ($flag == 1) {
$usec = start();
} else {
$usec = restart();
}
$flag = $flag + 1;
my $use = checkinput ($usec);
writechat ($use);
printchatall ();
if ($use =~ /exit\n/i) {
$flag = 0;
}
}
sub start {
intro();
my $in = input();
$flag2 = 1;
return $in;
}
sub restart {
print "what would you like me to say?: \n";
my $in2 = input();
$flag2 = 1;
return $in2;
}
sub input {
my $input = <>;
return $input;
}
This routine can be simplified to:
sub input {
return <>;
}
or even
sub input {
<>;
}
but you might want to remove the newline at the end of the input string:
sub input {
my $line = <>;
chomp($line);
return $line;
}
sub helprint {
open (HELP,"< help.txt") || die("can't find help file: $!");
while (<HELP>) {
print $_;
}
close HELP;
return 0;
}
sub checkinput {
while ($flag2 >= 1) {
my $checked2;
my $test2 = shift;
This statement is inside a while loop. Therefore, it can be executed
more than once, depending upon the logic involved in setting the value
of $flag2. While your program logic is convoluted, I do believe that
this loop will iterate more than once unless the user has entered a
special string (empty line, 'help', 'clear', etc.). You have called
this routine with one argument. The first time through the loop, the
argument (the user input) will be assigned to $test2 and shifted off
the @_ array. However, the second time through the loop, the @_ array
will be empty and $test2 will be undefined. This is the source of your
"Uninitialized" warnings.
You need to rethink the logic behind your user input processing logic.
my $checked;
$checked = special($test2);
unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {
$checked =~ /\n/i tests for the presence of a newline character in
the user input. Since such a character will always be present, this
test will never succeed. If you are trying to test for an empty line,
you need to anchor the R.E. at the beginning of the string:
$checked =~ /^\n/;
(there is no need to test case-insentively here).
It is better to use the regular expression meta-character '$' to test
for end of the string, and if you chomp the input, then this becomes:
unless ($checked =~ /^$/;
You are checking user input for special values both here and below. You
should only be doing this in one place. Checking just once will
simplify and improve your program logic.
$flag2 = 0;
}
$flag2 = $flag2 + 1;
if ($flag2 >= 2) {
$checked2 = special($checked);
}
}
return $checked2;
}
sub special {
my $test = (shift);
if ($test =~ /^help\n$/i) {
helprint ();
my $newans = input();
return $newans;
} elsif ($test =~ /^exit\n$/i) {
$flag = 0;
return 0;
} elsif ($test =~ /^clear\n$/i) {
open (CLEAR,"> chatthing.txt") || die("can't clear: $!");
You are using the string 'chatthing.txt' three times in your program.
You should put this string in a variable and use the variable
throughout your program.
Using the three-argument version of open and lexically-scoped (my)
variables for file handles is recommended:
open( my $clear, '>', $outfile ) or die("Can't open $outfile: $!");
print $clear "";
however, printing an empty string is a no-operation.