Hi Fetaelin,
As you already know, it often is possible to create one-liners for a lot of the most typical parsing needs. However, I generally avoid this as anything more than a curiousity.
In fact your currect script, if it's hoping to do what I think it is, is actually broken. I believe that the most important goal should be to make clear readable code, not ultra compact.
To that end, here is how I personally would do the project that you have listed:
-
#!/usr/bin/perl
-
-
use File::Slurp qw(read_file);
-
-
use strict;
-
-
my $file1 = 'marenden';
-
my $file2 = 'file2.txt';
-
my $outfile = 'result';
-
-
my %match = map {$_ => 1} grep {! /^\s*$/} read_file($file1);
-
-
open(IN, $file2) or die "Can't open $file2: $!";
-
open(OUT, ">>$outfile") or die "Can't open $outfile: $!";
-
-
while (<IN>) {
-
print OUT if $match{$_};
-
}
-
-
close(IN);
-
close(OUT);
-
As you can see, this is actually more lines of code because of the separation of the file name declarations. It also would be longer still if I hadn't used the File::Slurp module as a shortcut for reading in $file1. However, this code is more readable, and definitely easier to debug.
I suggest that you first aim to write a script like this before you obsess about one-liners.
- Miller