I learn something about tell(), then abuse it.
I learned a new thing today, or remembered a forgotten one. I can use tell to affect the file handle that $.
uses.
It all started very simply. I was going too far in my answer to How do I add the elements of a file to a second one as columns using Perl?, a question I found by looking for the most down voted open questions without an accepted answer. As usual, I thought the answer would be easy. And, for the most part it was.
Then I wanted to make it even easier. I thought Perl might not be necessary at all when we have things like paste
and head
and tail
and other command-line thingys. The problem was a header in one input file and no corresponding header in the other. How could I make paste
ignore the header?
I bet there's something that I'm missing, but I started working with the Perl Power Tools version of paste
. To fast forward through a file to get to the right starting point, I wanted to look at $.
to know when to stop, but that only works for the last read filehandle. To use it on another filehandle, I need to do something to to that handle without disturbing the data. tell was just the thing.
tell( $fh )
readline( $fh ) while $. < $starting_line - 1;
But, now I think that's also stupid because I didn't need the magic because I don't need to know the number of the currently read line:
readline( $fh ) foreach 1 .. $starting_line - 1;
As Perl gives, so Perl takes away (brain cells).
The link to tell just points to http://perldoc.perl.org/?
I went looking for Perl Power Tools documentation, and couldn't find any (at least, not without downloading everything, which seems a shame). Did I miss it?
In the past, when I've been dancing along the edge between "I can do this in a line or two of shell" and "I should really switch to Perl for this", I've used "head -1" to grab the header line, and "tail -n +2" to grab the rest of the lines. Then I'd paste the headless files, and then cat together the header and the output from paste. Or maybe decide it was getting too complicated, and switch to Perl as you did!
Or you could have used $fh->input_line_number() instead of $.
Ah yeah, I keep forgetting to use file handles as objects. I started Perl too early to immediately jump to those.
install C/CW/CWEST/ppt-0.14.tar.gz