<STDIN> chunk 1 - Error 
Author Message
 <STDIN> chunk 1 - Error

Hi everyone,

I just bought the Learning Perl book. I got to page 14 and typed in the
following code as in the book:

### CODE BEGIN ###

#!/usr/freeware/bin/perl -w
%words = qw(
 fred camel
 barney llama
 betty alpaca
 wilma alpaca
);
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
$original_name = $name;
$name =~ s/\W.*//;  # Get rid of everything after first word
$name =~ tr/A-Z/a-z/;  # Lowercase everything
if ($name =~ "randal") {
 print "Hello, Randal! How good of you to be here!\n";

Quote:
}  else {

 print "Hello, $original_name!\n";
 $secretword = $words{$name};  # get the secret word
 print "$secretword\n";   # Displays secret word
 if ($secretword eq "") {  # oops, not found
  $secretword = "groucho"; # sure, why a duck
 }
 print "What is the secret password? ";
 $guess = <STDIN>;
 chomp ($guess);
 while ($guess ne $secretword) {  # keep checking til we know
  print "Wrong, try again. What is the secret word? ";
  $guess = <STDIN>;
  chomp ($guess);
 }

Quote:
}      # end of "not Randal"

### CODE END ###

I get this errormessage:

Use of uninitialized value at ./Guess_Password line 19, <STDIN> chunk 1.

Use of uninitialized value at ./Guess_Password line 20, <STDIN> chunk 1.

whenever I type in a name not listed in the array. I thought the if
(secretword eq "") should take care of non-known names? This might sound
like a stupid quiestion, but I'm lost.

Thanks,
Thomas



Fri, 22 Dec 2000 03:00:00 GMT  
 <STDIN> chunk 1 - Error



Quote:
> Hi everyone,

> I just bought the Learning Perl book. I got to page 14 and typed in the
> following code as in the book:
...
> if ($name =~ "randal") {

Type more carefully.

if ($name eq "randal") {

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Fri, 22 Dec 2000 03:00:00 GMT  
 <STDIN> chunk 1 - Error

Quote:



> > Hi everyone,

> > I just bought the Learning Perl book. I got to page 14 and typed in the
> > following code as in the book:
> ...
> > if ($name =~ "randal") {

> Type more carefully.

> if ($name eq "randal") {

> --
> Larry Rosler
> Hewlett-Packard Laboratories
> http://www.hpl.hp.com/personal/Larry_Rosler/


Thats ok.. the operator =~  means "contains" to ME. I read his line as
saying "$name contains randal"... which should work just as well.

His problem appears to be.. he is printing a possible NULL value. If the
name enterred is NOT in the hash.. he does not test for it.... and tries
to print it.

Quote:
> $secretword = $words{$name};  # get the secret word
> print "$secretword\n";   # Displays secret word

A better choice might be:

if exists($words{$name}) {
        $secretword = $words{$name};  # get the secret word    
        print "$secretword\n";   # Displays secret word

Quote:
} else {

        $secretword = "groucho";

Quote:
}

..yada yada

--
Chuck Maier
CDM Consulting Services
http://www.cdmcon.com
(610) 943-2726



Sun, 24 Dec 2000 03:00:00 GMT  
 <STDIN> chunk 1 - Error
[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]



Quote:



...
> > > if ($name =~ "randal") {

> > Type more carefully.

> > if ($name eq "randal") {
...
> Thats ok.. the operator =~  means "contains" to ME. I read his line as
> saying "$name contains randal"... which should work just as well.

The operator =~ means "matches" to almost everyone else.  The second
problem here is the use of double-quotes to delimit the regex.  Here is
what Friedl (in 'Mastering Regular Expressions', p. 248) says about that:

Similarly, using $test =~ "...string..." could be useful if you wanted
*real* double-quote processing, rather than the doublequote*ish*
processing discussed earlier.  But frankly, I would leave this to an
Obfuscated Perl Contest.

Quote:
> His problem appears to be.. he is printing a possible NULL value. If the
> name enterred is NOT in the hash.. he does not test for it.... and tries
> to print it.

You mean *undefined* value -- he does test for a NULL value ("").

Quote:
> > $secretword = $words{$name};  # get the secret word
> > print "$secretword\n";   # Displays secret word

> A better choice might be:

> if exists($words{$name}) {

An even better choice might be something that compiles:

  if (exists($words{$name})) {

Quote:
>    $secretword = $words{$name};  # get the secret word    
>    print "$secretword\n";   # Displays secret word
> } else {
>    $secretword = "groucho";
> }

This is all discussed on p. 11:

If the name is not found, the value of $secretword will be an empty
string,* ...

* Well, OK, it's the *undef* value, but it looks like an empty string to
the *eq* operator.  You'd get a warning about this if you used -w on the
command line, which is why we omitted it here.

BUT:  He didn't omit it on p. 10, which is where the footnote relates
(but he did omit it at the bottom of p. 11).  That is an erratum, which I
didn't find at
<URL:http://www.oreilly.com/catalog/lperl2/lperl2.current>.

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sun, 24 Dec 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. <<<<<< HELP >>>>>>>>

2. >>>> A Singaporean Girl looking for more penpals!!<<<<<

3. Backspace key and <STDIN>...

4. Input via STDIN without using <CR>

5. <STDIN> in array context

6. Term::Screen and <STDIN>

7. <STDIN> and list or array

8. changing <STDIN>

9. ++ used as <STDIN>

10. Socket <> Stdin/Stdout

11. $name=<STDIN> problem

 

 
Powered by phpBB® Forum Software