/ / Come dovrei usare Perl's File :: Temp? - perl, file temporanei

Come dovrei usare Perl's File :: Temp? - perl, file temporanei

Vorrei creare un file temporaneo, scrivere sul file-handle, quindi chiamare un programma esterno con il nome del file.

Il problema è che vorrei normalmente close il file dopo aver scritto su di esso e prima di chiamare il programma esterno, ma se ho capito bene close-ing a tempfile() causa la sua rimozione.

Quindi qual è la soluzione qui?

risposte:

7 per risposta № 1

Scrivi nel file temporaneo con il buffering disattivato. Chiama il programma esterno prima di chiudere il file nello script Perl e il programma esterno sarà in grado di leggere tutto ciò che hai scritto.

use File::Temp qw(tempfile);
use IO::Handle;

my ($fh, $filename) = tempfile( $template, ... );

... make some writes to $fh ...

# flush  but don"t  close  $fh  before launching external command
$fh->flush;
system("/path/to/the/externalCommand --input $filename");

close $fh;
# file is erased when $fh goes out of scope

5 per risposta № 2

A partire dal http://perldoc.perl.org/File/Temp.html:

unlink_on_destroy

Control whether the file is unlinked when the object goes out of scope. The file is removed if this value is true and $KEEP_ALL is not.

1. $fh->unlink_on_destroy( 1 );

Default is for the file to be removed.

Prova a impostarlo su 0.


0 per risposta № 3

con l'interfaccia OOP di File::Temp tu puoi fare:

my $cpp =  File::Temp->new;
print $cpp "SOME TEXT";
$cpp->flush;

`cat $cpp`;