nickolayratchev
nickolayratchev

Reputation: 1206

Perl Strange Buffered Output Behavior

I've recently been trying to teach myself Perl and have been doing some basic exercises. In one of them, you have a hard-coded hash of last names to first names. The user inputs a last name and you output the first name -- relatively straightforward. The code is as follows:

#!/usr/bin/perl -w

use strict;
use warnings;

my %first_name = (
    Doe => 'John',
    Johnson => 'Bob',
    Pitt => 'Brad',
);

print "What is your last name?\n";
chomp (my $last_name = <STDIN>);
print "Your first name is $first_name{$last_name}.\n";

Now, something strange happens. The "What is your last name?\n" line is not displayed until I input something to the program (and press Enter), after which, the following is printed:

What is your last name?
Your first name is .
Use of uninitialized value within %first_name in concatenation (.) or string at     test.pl line 14, <STDIN> line 1.

Now, I understand the notion of buffered output and all that, and, if I add $| = 1 at the beginning of my program, it works. However, my expectation would be that even without that line, even though print statement strings might not print immediately, my input string will still be placed in the $last_name variable, which it is not. So, I have two questions regarding this:

  1. Why is this happening? Is it an OS thing (I'm running on Windows)?
  2. Why is it that adding a \n does not flush the output (as various sources say it does)?

Note: If I replace the last line that accesses the %first_name hash with a simple printing of the $last_name variable, then, even though the output is still "delayed", the variable has the correct value.

Note #2: Alternatively, if the code after printing the last name is replaced with this,

if (exists $first_name{$last_name}){ 
   print "Your first name is $first_name{$last_name}.\n";
}

else{
    print "Last name is not in hash.\n";
}

then $last_name does get assigned the correct value from <STDIN>. I am not sure what to make of this.

Upvotes: 2

Views: 145

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21666

You are not checking in your program whether the last name is in the hash or not, if it is not then you should display some message like "$lastname not found".

By the way your program is working fine on my side if I enter the correct lastname (which exists in the hash).

So you may edit your program like this:

#!/usr/bin/perl

use strict;
use warnings;

my %first_name = (
    Doe => 'John',
    Johnson => 'Bob',
    Pitt => 'Brad',
);

print "What is your last name?\n";
chomp (my $last_name = <STDIN>);

# Check if the last_name exists in hash or not
if (exists $first_name{$last_name}){ 
   print "Your first name is $first_name{$last_name}.\n";
}

# If it doesn't then show a custom message
else{
    print "not found";
}    

Perhaps you are suffering from buffering.

Upvotes: 3

Related Questions