I ran a quick test of your initial program:
#! /usr/bin/env perl
#
use strict;
use feature qw(say);
use warnings;
my $out = "test.out";
my $list = "2";
my $in = "test.txt";
open OUT,'>',$out || die "cannot open $out";
my $file =`cut -f $list $in`;
print OUT $file;
close OUT;
My test file looked like this:
this that
who knows
who cares
I know
My test.out
file looks like this:
that
knows
cares
know
If the test file didn't exist, the program still runs, and I get an error printed out on STDERR by cut
. If I give it a bad field spec, the program still runs and I get another error printed on STDERR by cut
.
I've tried a few things, but couldn't get the program to fail outright with a segmentation fault.
So, the question is what is producing that segmentation fault. Is it cut
or Perl? Put a print statement after the my $file =
cut -f $list $in;
and the print OUT $file;
lines and see if they print anything. If they do, the problem is with cut
and not with Perl. If not, somehow your Perl interpreter is having problems. So, then the question is why does it work on my system and not yours:
- What version of Perl are you using?
- What exactly is the OS? Is this Linux, Mac OSX, Cygwin running under Windows, or some old ancient version of Solaris/SunOS?
- Is this the entire message?
- Was any of your
$out
file written?
The program you wrote should work and does on my systems. I'm using Perl 5.14 on Mac OS X 10.9 and on Perl 5.8.8 on a Linux RHEL box. I haven't tried this on Cygwin.