Simple code but wierd Perl behavior. Why does this happen?

Hello. I have a very simple subroutine that opens a file and reads the lines into an array, then prints out the array text. (I’m experimenting with arrays – not planning to print out all the lines directly so please don’t tell me how to do that).

sub printFile{
my ($file) = @_;
open(HANDLE, "< $file") || die("Failed to open file!");
while(@line = <HANDLE>){
chomp(@line);
print @line;
}
close(HANDLE);
}

So when I run the code it shows only one line of it that changes as it prints out the text and then it finishes with the last line shown. I would have expected it to fill the whole console window with text, but all it does is update a single line.

Comments

  1. Ajith A says:

    Hi Shadow,

    I suspect that the reason you see one line updating is that your terminal isn’t wrapping lines. The code as written is removing the newlines from your input text (chomp), and is printing the text without appending any line termination. On most terminals, you would see all the text wrapped around to many lines, when in fact its just a single line of output.

    So as its written, your code will take every line from the input file, and print it as one single line. If you want to "fill your console" then you should insert a newline when you print, or refrain from chomping the original newline.

    If it is in fact your intent to create a single line of output, then you should do some research into your terminal’s capabilites (termcap/terminfo) to figure out how to let it wrap the long line.

legitimate work from home jobs | Event Planning Bay Area | rent video games | Homeloans | Plumbing Courses in Manchester | increase fertility | Green Drink | Photo Keyring | Vitamin D Deficiency Symptoms | skip bags | Order Fulfillment Software | virtual receptionist | Educators Credit | bodybuilding workouts | window cleaning
payday loan payday loans fibromyalgia ezetimibe buy ziagen buy cipro newyork travel narrow angle glaucoma headache Poker Software sciatic nerve damage cluster headache sciatica glaucoma fibromyalgia syndrome