in reply to Re: using a variable as a filehandle
in thread using a variable as a filehandle
Unlike unix, it's common but not standard to put a \n on the last line in DOS/Windows. <FILE> handles the missing \n, but your code chops off the last line. The following handles both the presence and absense of the last \n:
#!/usr/bin/perl -w use strict; my @files_to_process= ( {data=>"The quick\n" . "brown fox\n" . "ran over\n" . "the purple\n" . "witch. So\n". "there!"} ); foreach my $record (@files_to_process) { while ($record->{data} =~/(.*\n|.+$)/g) { chomp(my $line = $1); print "[$line]\n"; } }
|
|---|