Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Mixing the file read operator (<code>&lt; ... &gt;</code>) with <code>getc</code> is a bad idea. It's not doing what you think it is.</p> <p>Try putting some debug output into the program to see what's going on. I tested the program by running it on itself (<code>./getc getc</code>).</p> <p>At the start of the <code>while</code> loop, the <code>&lt;INPUT&gt;</code> reads a line from your file and stores it in <code>$_</code>. You then use <code>getc</code> to read the next character from the file. That will be the first character from the second line of your file (probably a newline character - which is likely to be the only character on that line).</p> <p>The next time round the loop, the <code>&lt;INPUT&gt;</code> reads the next line of input. That's the <code>use strict</code> line. The <code>getc</code> reads the next character which is the 'u' from <code>use warnings</code>.</p> <p>And so it continues to the end of the file. The <code>&lt;INPUT&gt;</code> reads a line and then the <code>getc</code> reads the first character from the next line.</p> <p>That's not what you want at all. If you want to read a character at a time then you just need the <code>getc</code>.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $input = shift; open(my $file, '&lt;', $input); while (defined(my $c = getc $file)) { print "$c\n"; } </code></pre> <p>Another alternative would be to just use <code>&lt; ... &gt;</code> and split each line as you read it.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $input = shift; open(my $file, '&lt;', $input); while (&lt;$file&gt;) { foreach my $c (split //) { print "$c\n"; } } </code></pre> <p>But mixing the two approached is never going to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload