Created
May 28, 2017 09:29
-
-
Save zoffixznet/c69db292652816a3f64d6cf0e8ead8ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just some setup for the files we'll be reading: | |
'file1'.IO.spurt: "a\nb\nc"; | |
'file2'.IO.spurt: "d\ne\n"; | |
'file3'.IO.spurt: "f"; | |
my $line; | |
# CatHandle accepts any mix of Cool:D, IO::Path, IO::Handle, IO::Pipe: | |
my $kitty = IO::CatHandle.new: 'file1', 'file2'.IO, 'file3'.IO.open, | |
:on-switch{ $line = 1 }; # define what to do on handle switch | |
say "{$kitty.path} on line {$line++} has $_" for $kitty.lines; | |
# OUTPUT: | |
# file1 on line 1 has a | |
# file1 on line 2 has b | |
# file1 on line 3 has c | |
# file2 on line 1 has d | |
# file2 on line 2 has e | |
# file3 on line 1 has f | |
$kitty = IO::CatHandle.new: <file1 file2 file3>, on-switch => -> $new, $old { | |
# A bit more elaborate formatting: | |
$new and $old and say(); # extra line between files | |
$new and "๐บ๐บ๐บ $new ๐บ๐บ๐บ".say; # heading for each file | |
$line = 1; | |
}; | |
say "Line {$line++}: $_" for $kitty.lines; | |
# OUTPUT: | |
# ๐บ๐บ๐บ file1 ๐บ๐บ๐บ | |
# Line 1: a | |
# Line 2: b | |
# Line 3: c | |
# | |
# ๐บ๐บ๐บ file2 ๐บ๐บ๐บ | |
# Line 1: d | |
# Line 2: e | |
# | |
# ๐บ๐บ๐บ file3 ๐บ๐บ๐บ | |
# Line 1: f | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment