in reply to Re^4: string manipulation
in thread string manipulation
foreach my $line (<MAKFILE>) {
I personally believe you should know that almost always in Perl (5) the Right™ way to iterate over the lines of a file is a while loop. You should also be using lexical filehandles.
if ($line =~ m/SOURCE=\.\\(.*)$/i) {
I generally chomp too, but incidentally $ matches right before \n too; you may also push the return value of the match operator.
$fileLst = join " ", @fileLst;
Perhaps you prefer to be explicit, and I'm sure many people would say that you're right, but there's a very convenient shortcut for join " ", @fileLst, which is "@fileLst"; also, I see no reason to create an intermediate temporary variable just to return it.
All in all, I will give you an example subroutine which will accept a filename and give you your expected output:
sub domkfile { my ($fname, @flist)=shift; open my $fh, '<', $fname or die "Can't open `$fname': $!\n"; push @flist, /SOURCE=\.\\(.*)$/i while <$fh>; "@flist"; }
If slurping the whole file at once is not a problem, and indeed it should not be in this case, you may avoid the use of the @flist array with a map:
sub domkfile { my $fname=shift; open my $fh, '<', $fname or die "Can't open `$fname': $!\n"; join ' ' => map /SOURCE=\.\\(.*)$/i, <$fh>; }
|
|---|