Skip to content

Commit fe4fa39

Browse files
Rok Ružičracke
Rok Ružič
authored andcommitted
Prevent [import] from using a temporary file for inline data.
This also implicitly fixes the UTF-8 problems reported in #339.
1 parent 9026f0b commit fe4fa39

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/Vend/Data.pm

+4-3
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ sub import_text {
306306
or die ::errmsg("No absolute file names like '%s' allowed.\n", $fn);
307307
}
308308
else {
309-
Vend::Util::writefile($fn, $text)
310-
or die ("Cannot write temporary import file $fn: $!\n");
309+
# data is already in memory, do not create a temporary file
310+
$options->{scalar_ref} = 1;
311+
$fn = \$text;
311312
}
312313

313314
my $save = $/;
@@ -319,7 +320,7 @@ sub import_text {
319320
Vend::Table::Common::import_ascii_delimited($fn, $options);
320321

321322
$/ = $save;
322-
unlink $fn unless $options->{'file'};
323+
unlink $fn unless $options->{'file'} or $options->{scalar_ref};
323324
return 1;
324325
}
325326

lib/Vend/Table/Common.pm

+18-6
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,9 @@ sub import_ascii_delimited {
10461046

10471047
my $realfile;
10481048
if($options->{PRELOAD}) {
1049-
if (-f $infile and $options->{PRELOAD_EMPTY_ONLY}) {
1049+
# do not preload if $infile is a scalar reference
1050+
if ($options->{scalar_ref} or
1051+
(-f $infile and $options->{PRELOAD_EMPTY_ONLY})) {
10501052
# Do nothing, no preload
10511053
}
10521054
else {
@@ -1058,10 +1060,18 @@ sub import_ascii_delimited {
10581060
}
10591061

10601062
if(! defined $realfile) {
1061-
open(IN, "+<$infile")
1062-
or die errmsg("%s %s: %s\n", errmsg("open read/write"), $infile, $!);
1063-
lockfile(\*IN, 1, 1)
1064-
or die errmsg("%s %s: %s\n", errmsg("lock"), $infile, $!);
1063+
if($options->{scalar_ref}){
1064+
open(IN, '+<', $infile)
1065+
or die errmsg("%s %s: %s\n", errmsg("open scalar reference"), *$infile, $!);
1066+
# locking of scalar reference filehandles in unsupported
1067+
}
1068+
else{
1069+
open(IN, "+<$infile")
1070+
or die errmsg("%s %s: %s\n", errmsg("open read/write"), $infile, $!);
1071+
lockfile(\*IN, 1, 1)
1072+
or die errmsg("%s %s: %s\n", errmsg("lock"), $infile, $!);
1073+
}
1074+
10651075
}
10661076
else {
10671077
open(IN, "<$infile")
@@ -1474,7 +1484,9 @@ EndOfRoutine
14741484
}
14751485
delete $out->[$CONFIG]{Clean_start};
14761486
delete $out->[$CONFIG]{_Dirty};
1477-
unlockfile(\*IN) or die "unlock\n";
1487+
unless($options->{scalar_ref}){
1488+
unlockfile(\*IN) or die "unlock\n";
1489+
}
14781490
close(IN);
14791491
my $dot = $out->[$CONFIG]{HIDE_AUTO_FILES} ? '.' : '';
14801492
if($numeric_guess) {

0 commit comments

Comments
 (0)