A small problem I can't quite work out.

S

sj

I'm making a program just for the joy of it, really, but I'm getting a
bug I just can't work out. Basically, when I run it, I get the message
'uninitiated value $whatever in pattern match (m//) at C:\wherever
line whatever'.
The problem is, they are initiated...I can post the code if it would
make this easier, but all variables are declared with 'my' not
'local'...I've tried putting them in different places in the code,
within loops or all at the begining etc...it used to work :/ maybe I
should just start again!
 
T

Tad J McClellan

sj said:
Subject: A small problem I can't quite work out.


Please put the subject of your article in the Subject of your article.

Subject: Use of uninitialized value

or some such.

I'm making a program just for the joy of it, really, but I'm getting a
bug I just can't work out. Basically, when I run it, I get the message
'uninitiated value $whatever in pattern match (m//) at C:\wherever
line whatever'.


Please post the *actual text* of messages rather than paraphrase them.

Have you seen the Posting Guidelines that are posted here frequently?

The problem is, they are initiated...


The word is "uninitialized", not uninitiated.

I can post the code if it would
make this easier,


It would not make it easier.

It would make it *possible*.

but all variables are declared with 'my' not


"declaring a variable" and "defining a variable" are separate
and distinct concepts.

my() has to do with declaring variables.

Your message has to do with defining variables.

'local'...I've tried putting them in different places in the code,
within loops or all at the begining etc...it used to work :/ maybe I
should just start again!


We cannot repair code that we cannot see...
 
S

sj

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...

#use strict;
#use warnings;

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;
}

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;
my $checked;
$checked = special($test2);
unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {
$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: $!");
print CLEAR "";
my $newans1 = input();
return $newans1;
}else {
return $test;
}
return "/n"
}

sub intro {
open (INTRO,"< intro.txt") || die("can't find the intro: $!");
while (<INTRO>) {
print $_;
}
close INTRO;
}

sub writechat {
my $answer = shift;
open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
print CHATW $answer;
close CHATW;
}

sub printchatall {
open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
while (<CHATR>) {
print $_;
}
close CHATR;
}
 
S

sj

Okay, fixed it myself *is embarassed*
tried to delete the thread, but did it wrong.
Oh gosh, I'm a complete n00b...Never mind.
 
J

Jim Gibson

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 "";

print $clear "";

however, printing an empty string is a no-operation.
 
S

sj

Thank you for making the effort to help us help you.




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.














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;
  }





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.


  $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.






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.

Cheers, that really helps a lot! I'll keep all that in mind. The
strict and warnings were commented out because I was trying to see
what difference it makes, I only just did it and forgot to undo it
before I posted. I'll make notes of all of this :)
 
M

Martien Verbruggen

This routine can be simplified to:

sub input {
return <>;
}

Except that the original always uses <> in a scalar context, while yours
uses <> in whatever context the input subroutine was used in.

Martien
 
M

Martijn Lievaart

Cheers, that really helps a lot! I'll keep all that in mind. The strict
and warnings were commented out because I was trying to see what
difference it makes, I only just did it and forgot to undo it before I
posted. I'll make notes of all of this :)

Keep in mind that people are really sensitive to such errors. Post the
exact code, with use strict and use warnings and the exact output from
the exact code you posted. Doing so will tremendously increase the
response from this group.

M4
 
U

Uri Guttman

JG> This routine can be simplified to:

JG> sub input {
JG> return <>;
JG> }

JG> or even

JG> sub input {
JG> <>;
JG> }

not exactly. his will always return 1 line but yours could slurp in the
whole <> until eof if called in list context. but there is no need for
this sub to begin with.

JG> but you might want to remove the newline at the end of the input string:
JG> sub input {
JG> my $line = <>;
JG> chomp($line);
JG> return $line;
JG> }

now it becomes a slightly more useful sub but not really if it is only
called in 1 place.


JG> You need to rethink the logic behind your user input processing logic.

the OP is new at coding and stuck in the old way of using flags for
state. it will take some real world experience and time for him to
unlearn this.

JG> print $clear "";

JG> however, printing an empty string is a no-operation.

actually that would print "" to the handle in $clear! :)

uri
 
T

Tad J McClellan

[ snip 200 lines ]

Cheers, that really helps a lot! I'll keep all that in mind.


When composing a followup, quote only enough text to establish the
context for the comments that you will add.

Quoting 200 lines and adding 5 lines is seen as impolite.

Have you seen the Posting Guidelines that are posted here frequently?
 
A

A. Sinan Unur

Keep in mind that people are really sensitive to such errors. Post the
exact code, with use strict and use warnings and the exact output from
the exact code you posted. Doing so will tremendously increase the
response from this group.

This poster has already established a good example for others to follow
by responding positively to Tad's suggestions.

To the OP: It is a good idea to read the posting guidelines for this
group a few times. Also, Don't wait until you have a question to read
the documentation. Read the FAQ once every few months even though the
questions and the answers may not make sense yet. Take a look at perldoc
perltoc, and select topics that look interesting and/or relevant to what
you are doing.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
S

sj

  >> sub input {
  >> my $input = <>;
  >> return $input;
  >> }

  JG> This routine can be simplified to:

  JG>   sub input {
  JG>     return <>;
  JG>   }

  JG> or even

  JG>   sub input {
  JG>     <>;
  JG>   }

not exactly. his will always return 1 line but yours could slurp in the
whole <> until eof if called in list context. but there is no need for
this sub to begin with.

  JG> but you might want to remove the newline at the end of the input string:
  JG>   sub input {
  JG>     my $line = <>;
  JG>     chomp($line);
  JG>     return $line;
  JG>   }

now it becomes a slightly more useful sub but not really if it is only
called in 1 place.

  JG> You need to rethink the logic behind your user input processing logic.

the OP is new at coding and stuck in the old way of using flags for
state. it will take some real world experience and time for him to
unlearn this.

  JG>   print $clear "";

  JG> however, printing an empty string is a no-operation.

actually that would print "" to the handle in $clear! :)

uri

--
Uri Guttman  ------  (e-mail address removed)  --------  http://www.sysarch.com--
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training ---http://perlhunter.com/college.html---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------

Thanks for everyones help once again! I know it's a bit of a useless
program in reality but it's really helping me learn how to deal with
the basics, I'm learning a lot just from reading your replies. I
learned what I know thusfar about the concepts of programing from a
friend, in Visual Basic which he used to own, as well as bits and bobs
from the internet, which is probably why I'm a bit behind with my
techniques. I'll keep lurking here and pick up as much second hand
knowledge as I can :)

as for the code:

print $clear "";

I was using this to empty the file of contents, I don't know any other
way to do this so any advice would be appreciated.
Cheers again,
Sam
 
T

Tad J McClellan

Never quote a .signature (unless that is what you are commenting on).

Have you seen the Posting Guidelines that are posted here frequently?

from a
friend, in Visual Basic which he used to own,


Microsoft owns Visual Basic.

Your friend only licensed it from them.

print $clear "";

I was using this to empty the file of contents,


That could never have worked.

It was not that print() statement that emptied the file, it might
have been an open() that emptied it though.

I don't know any other
way to do this so any advice would be appreciated.


perldoc -f truncate
 
S

sj

Never quote a .signature (unless that is what you are commenting on).

Have you seen the Posting Guidelines that are posted here frequently?


Microsoft owns Visual Basic.

Your friend only licensed it from them.



That could never have worked.

It was not that print() statement that emptied the file, it might
have been an open() that emptied it though.


    perldoc -f truncate

I apologise for my misuse of 'owned', I should have said he owned a
licence for Visual Basic.
Thank you for showing me truncate, I'll give that a go now.
Cheers,
Sam
 
J

Jürgen Exner

sj said:
I'm making a program just for the joy of it, really, but I'm getting a
bug I just can't work out. Basically, when I run it, I get the message
'uninitiated value $whatever in pattern match (m//) at C:\wherever
line whatever'.

I don't believe you. That is not an error message issued by perl. Maybe
you meant "uninitialized" instead of 'uninitiated'? Please copy and
paste error messages, the exact wording is important.
The problem is, they are initiated...

Maybe. I don't know what an initiated variable is. Whoever it has no
bearing on a variable being initialized.
I can post the code if it would
make this easier,

That would certainly make it easier. Finding an error in a piece of code
without seeing the code requires some unusual levels of paranormal
abilities.
but all variables are declared

The declaration of a variable has nothing to do with its initialization.
with 'my' not
'local'...I've tried putting them in different places in the code,
within loops or all at the begining etc...

Well, fine, but those declare variables, they don't assign any value to
them.
it used to work :/ maybe I
should just start again!

It might be easier to just assign a value to the variable $whatever.

jue
 
J

Jürgen Exner

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...

Your code does not have a variable $whatever, which -as you claimed in
your first posting- was named in the error message.
#use strict;
#use warnings;

Why are you disabling the two most helpful weapons against frequent
programmer mistakes in perl's arsenal? Enable them, fix all the warnings
and error messages you get now, and then we can talk again. In all
likelyhood your original problem will be gone by then.

[rest of code snipped]

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top