#! perl -slw use strict; my @deptids = qw[ dept1 dept2 dept3 ]; my %fh; open $fh{ $_ }, '+>', "/data/logs/${_}current.log" or die "$_: $!" for @deptids; open (LOGFILE, "/data/logs/access.log") or die $!; while( defined( my $line = ) ) { my $match; for( @deptids ) { $match = $_ and last if $line =~ m[\Q$_] }; if( $match ) { ## Note: The {}s around the file handle are required. print { $fh{ $match } } $line; } else { print STDERR "'$line' didn't match any dept"; } } close $_ for values %fh; close LOGFILE;