Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created May 28, 2017 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/c69db292652816a3f64d6cf0e8ead8ef to your computer and use it in GitHub Desktop.
Save zoffixznet/c69db292652816a3f64d6cf0e8ead8ef to your computer and use it in GitHub Desktop.
# 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