File::Temp and external editor

  • Thread starter Binand Sethumadhavan
  • Start date
B

Binand Sethumadhavan

Hi All,

I am writing a program which:

- creates a temporary file using File::Temp
- opens the file in an external editor
- processes the data the user enters in the file

(sort of like "cvs commit" - it opens a /bin/vi buffer where you can
enter the commit log).

I cannot figure out how to get the text typed in the buffer, back in my
perl code. Here is what I'm doing at the moment:

$prefix = "/tmp";
$cmd = "/bin/vi";
use File::Temp qw/tempfile/;
($fh, $filename) = tempfile(DIR => $prefix, UNLINK => 1);
print $fh <<EOF;
[some pre-filled information here]
EOF
$mtime1 = (stat($filename))[9];
system ("$cmd $filename");
$mtime2 = (stat($filename))[9];
if ($mtime2 - $mtime1 > 0) {
# Process the contents of $filename here
}

What I am doing at the moment is @cmdbuf = `cat $filename`; so that I
can continue with the rest of my code, but I'd like to know how to do it
better.

I hope this is not a faq.

Binand
 
X

xhoster

Binand Sethumadhavan said:
Hi All,

I am writing a program which:

- creates a temporary file using File::Temp
- opens the file in an external editor
- processes the data the user enters in the file

(sort of like "cvs commit" - it opens a /bin/vi buffer where you can
enter the commit log).

I cannot figure out how to get the text typed in the buffer, back in my
perl code.

What exactly is the problem?
$prefix = "/tmp";
$cmd = "/bin/vi";
use File::Temp qw/tempfile/;
($fh, $filename) = tempfile(DIR => $prefix, UNLINK => 1);
print $fh <<EOF;
[some pre-filled information here]
EOF
$mtime1 = (stat($filename))[9];
system ("$cmd $filename");
$mtime2 = (stat($filename))[9];
if ($mtime2 - $mtime1 > 0) {
# Process the contents of $filename here
}

I see no reason you need the $mtime stuff. What if the person edits
and saves the file really quickly?

What I am doing at the moment is @cmdbuf = `cat $filename`; so that I
can continue with the rest of my code, but I'd like to know how to do it
better.

What is suboptimal about the way you are doing it now? We can't tell you
how to do it better if you don't tell us what the criteria are. I guess
for you could change @cmdbuf=`cat $filename` to something from File::Slurp,
for portability.

Xho
 
B

Brian McCauley

Hi All,

I am writing a program which:

- creates a temporary file using File::Temp
- opens the file in an external editor
- processes the data the user enters in the file

(sort of like "cvs commit" - it opens a /bin/vi buffer where you can
enter the commit log).

I cannot figure out how to get the text typed in the buffer, back in my
perl code. Here is what I'm doing at the moment:

$prefix = "/tmp";
$cmd = "/bin/vi";
use File::Temp qw/tempfile/;
($fh, $filename) = tempfile(DIR => $prefix, UNLINK => 1);
print $fh <<EOF;
[some pre-filled information here]
EOF
$mtime1 = (stat($filename))[9];
system ("$cmd $filename");
$mtime2 = (stat($filename))[9];
if ($mtime2 - $mtime1 > 0) {
# Process the contents of $filename here

}
What I am doing at the moment is @cmdbuf = `cat $filename`; so that I
can continue with the rest of my code, but I'd like to know how to do it
better.

I think you are looking for

seek($fh,0,0);
@cmdbuf = <$fh>;
 
J

Joe Smith

Brian said:
I think you are looking for

seek($fh,0,0);
@cmdbuf = <$fh>;

If you're using an editor that creates a backup file, that may not work.
Better to re-open the file after the editor is done with it.
-Joe
 
P

Peter J. Holzer

Binand Sethumadhavan said:
$mtime1 = (stat($filename))[9];
system ("$cmd $filename");
$mtime2 = (stat($filename))[9];
if ($mtime2 - $mtime1 > 0) {
# Process the contents of $filename here
}

I see no reason you need the $mtime stuff. What if the person edits
and saves the file really quickly?

Some programs use that technique to decide whether the user saved the
file from the editor (thereby confirming that he wanted to proceed) or
exited the editor without saving (and thus canceled the whole
operation). As you notice, this can cause trouble, if the user manages
to save the file within the same second (rarely a problem with humans,
but quite possible if used within a script). An alternative is to check
whether the file really changed (either via a checksum or by saving the
original contents of the file), but that causes problems if saving the
file unchanged is a valid response. I don't see a general solution to
the problem.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top