in reply to Re: blank lines up to a point
in thread blank lines up to a point

Never say never :)   0 as a second condition can be true.
use strict; use warnings; my $firsttime = 1; my $line1 = <DATA>; print neverflop(); $firsttime = 0; my $line2 = <DATA>; print neverflop(); scalar tell(STDIN); print neverflop(); print neverflop(); sub neverflop { ($firsttime .. 0) ? "flip" : "flop"; } __DATA__ line1 line2 line3 __END__ Output: flipflipflipflop

Replies are listed 'Best First'.
Re^3: blank lines up to a point
by Random_Walk (Prior) on Sep 15, 2004 at 11:09 UTC

    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.

Re^3: blank lines up to a point
by periapt (Hermit) on Sep 15, 2004 at 13:59 UTC
    This works as a quirk of the flip/flop operator. From the documentation "If either operand of scalar ``..'' is a constant expression, that operand is implicitly compared to the $. variable, the current line number." The statement scalar tell(STDIN) must set $. = 0 (although I don't know why) changing the line from scalar tell(STDIN); to $. = 0; gives the same result.

    use strict; use warnings; my $firsttime = 1; my $line1 = <DATA>; print neverflop(); $firsttime = 0; my $line2 = <DATA>; print neverflop(); #scalar tell(STDIN); $. = 0; print neverflop(); print neverflop(); sub neverflop { my $statecount = ($firsttime .. 0); ($firsttime .. 0) ? "flip$statecount\t" : "flop$statecount\t"; } __DATA__ line1 line2 line3 __END__ flip1 flip2 flip3E0 flop

    PJ
    use strict; use warnings; use diagnostics; (if needed)
      $. is stored per-filehandle, and automatically refers to the last input file. Any of readline, tell, seek, sysseek, or eof will change the last input file.

      You could just use:

      /\S/ .. my $false
      instead.
        Can any operator that inspires this much confusion be a GoodThing (TM)?

        Don't answer that. It's a rhetorical question.