I get an error from this unless I alter the tell line to my $discard=tell(STDIN); then indeed it gives the advertised output (I am on an old perl version here, client requirements)
./flipflop
Useless use of tell in void context at ./flipflop line 14.
perl -v
This is perl, version 5.005_03 built for sun4-solaris
...after the above edit....
root@tivpre-master:/home/robinp # ./fixedflop
flipflipflipflop
that aside how does this work ? the docco says tell normaly returns -1 against STDIN, what dark juju is going on to make 0 true and what else could I use that I can trust never to be true ?
Updated
It took some digging but I've got it now. the binary range operator .. will compare a given constant value to the value of the current input line number $. (actualy int(EXPR)==int($.) I was naively using 0 in its meaning of false and you cunningly reset $. to 0 (with tell(STDIN)) so 0 got matched. I offer up a fixed sub to avoid this gotcha.
sub logit {
local $.=1;
my $line = shift @_;
$line =~ s/\0//g;
return unless (/\S/..0);
print $line
}
Many Thanks,
R.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.