in reply to seek and ye shall find

Hmm... very nice. You just taught me something I didn't know about <DATA>. :) Namely that the position of it, as returned by tell, seems to be counted from the start of the file it is in. And that seems to be the solution to the puzzle too.
#!/usr/bin/perl -w just another perl hacker
The part after perl -w that are not switches are simply ignored, moving us to the next line.

print+seek(DATA,$=*.3,@-)?scalar<DATA>:$:__DATA__
That is the magic part - it sets the file pointer of <DATA> to 18, counted from the start of the file, which puts the next read right after -w. Why 18? Because $= defaults to 60. Update:Forgot to mention that @- is the same as a zero, thus giving the third argument to seek that is needed.

The seek also succeeds, returning true so what happens is this:


print+seek(DATA,$=*.3,@-)?scalar<DATA>:$:__DATA__
And forcing <DATA> to be printed in scalar mode makes it only print the first line - and there you go. :)

Side notes:


print+seek(DATA,$=*.3,@-)?scalar<DATA>:$:__DATA__
$: is just a placeholder, or to throw us off, and it defaults to zero or an empty string (don't remember). The __DATA__ part is needed for this to work, otherwise perl will complain about an unopened filehandle.

Hope I didn't forget, or misunderstand anything. Correct me then, please. :) And thank you for teaching me something new. I think it is a very nice sig. :)


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Solution !: seek and ye shall find
by domm (Chaplain) on Mar 07, 2002 at 16:37 UTC
    You just taught me something I didn't know about <DATA>. :) Namely that the position of it, as returned by tell, seems to be counted from the start of the file it is in.

    The thing is that if you use __DATA__, perl automatically seeks to the beginning of the DATA-section. But at the same time it sets the DATA-filehandle to the current file, so if you seek it yourself, you actually seek the source code itself.

    I learned this from TheDamians SelfGOL.

    And BTW, your solution is correct (and very well written, IMO)

    The only thing worth mentioning is that
    seek+print
    is actually the same as
    seek print
    as the + is used as the unary plus in front of a term, basically having no effect on anything.

    --
    #!/usr/bin/perl -w just another perl hacker
    print+seek(DATA,$=*.3,@-)?~~<DATA>:$:__DATA__
    
      Isn't this rather print+seek instead of seek+print ?

      -= Bobinours =-