use strict; use warnings; use feature 'say'; use Path::Tiny; my %file_map = ( tag1 => Path::Tiny->tempfile, tag2 => Path::Tiny->tempfile ); chomp( my @lines = ); for ( @lines ) { my ( $txt, $tag ) = split / : /; path( $file_map{ $tag } )->append("$txt\n"); } for ( keys %file_map ) { say $file_map{ $_ }; say $file_map{ $_ }->slurp; } __END__ test/foo/bar : tag1 test/abc/xyz : tag1 test/def/abc : tag2 test/bar/foo : tag2 test/dummy/foo : tag1 #### perl 1206430.pl /tmp/k6iEPAjgjR test/def/abc test/bar/foo /tmp/aFliecLlJN test/foo/bar test/abc/xyz test/dummy/foo