Here's the current code. When I run it under the debugger, all is
well until I get to the my $hdr line. When I step through this
and (in the debugger) type p $hdr, it returns nothing; the same
is true when I type p @colnames even though p $data returns the whole data array. I'm new
at this, so I don't really understand how the FILEHANDLEs work.
Even though this syntax seems correct, it doesn't seem to really open
that array. I'm lost.
sub subgen {
my($data) = shift;
chomp($data);
my($cur_segno) = 0;
my(@sst) = ();
open(FILE, $data); # or die "Cannot open: @data\n";
my $hdr = <FILE>; #read in the first line
chomp $hdr;
my @colnames = split /, ?/, $hdr; #split the header into an array
| [reply] [d/l] |
Why would $hdr be empty? Two possibilities. First, it's a blank line. chomping it will remove the newline, leaving a possibly empty string. The second possibility is that the open failed. Here's a better error line:
open(FILE, $data) or die "Can't open ($data): $!";
That gives you the name of the file, surrounded by non-whitespace characters as a sanity check, along with the system error message. You'll have better luck if you let Perl help you.
Update: If it's not a file, don't open it. open works on files (in the Unix sense). If you have the data you want to munge already in an array or a scalar, use split or a regex to get at it. That's the problem.
| [reply] [d/l] |
Ok - in the spirit of letting perl help me, how does open handle
arrays that are not explicitly files? In my code, $data is just
what I read in from @_, not a file. But when I run this (even in the debugger)
all the contents of $data are written to STDOUT and the program
terminates (not the desired output!).
Update: I did what you suggested, and get the following error:
): File name too long at ocean_return2b.pl line 176, <STDIN> line 4.
Debugged program terminated. Use q to quit or R to restart,
line 176: open(FILE, $aref) or die "Cannot open: ($aref): $!";
It seems that it tries to open the entire contents of $aref, as that is
what is returned to the screen. | [reply] [d/l] |