seek
ErrorsCollection

seek

Synthesised documentation from type/IO/Handle type/IO/CatHandle

From type/IO/Handle

See Original text in context

method seek(IO::Handle:D: Int:D $offsetSeekType:D $whence --> True)

Move the file pointer (that is, the position at which any subsequent read or write operations will begin) to the byte position specified by $offset relative to the location specified by $whence which may be one of:

From type/IO/CatHandle

See Original text in context

method seek(IO::CatHandle:D: |c)

Calls .seek on the currently active source handle, forwarding it all the arguments, and returns the result. Returns Nil if the source handle queue may get exhausted. NOTE: this method does NOT perform any source handle switching, so seeking past the end of the current source handle will NOT seek to the next source handle in the queue and seeking past the beginning of the current source handle is a fatal error. Also see source handle queue may get exhausted, to learn the details on when source handles are switched.

(my $f1 = 'foo'.IO).spurt: 'foo';
(my $f2 = 'bar'.IO).spurt: 'bar';
 
with IO::CatHandle.new: $f1$f2 {
    .get.say;                     # OUTPUT: «foo␤» 
    .seek: -2SeekFromCurrent;
    .readchars(2).say;            # OUTPUT: «oo␤» 
    .seek: 1000SeekFromCurrent# this doesn't switch to second handle! 
    .readchars(3).say;            # OUTPUT: «bar␤» 
    try .seek: -4;                # this won't seek to previous handle! 
    say ~$!;                      # OUTPUT: «Failed to seek in filehandle: 22␤» 
}