开发者

perl mktemp and echo

i am trying to put some word in tempfile via commandline temp file creat but word not past in tempfile

#!/usr/bin/perl -w
system ('clear');
$TMPFILE = "mktemp /tmp/myfile/devid.XXXXXXXXXX";
$ech开发者_开发问答o = "echo /"hello world/" >$TMPFILE";
system ("$TMPFILE");
system ("$echo");

Please Help to Solve This


To capture the name output by mktemp, do this instead:

chomp($TMPFILE = `mktemp /tmp/myfile/devid.XXXXXXXXXX`);

But Perl can do all the things you are doing without resorting to the shell.


Avoid using external commands from perl script as much as possible.

you can use: File::Temp module in this case, see this


Here's a specific demonstration of the advice that others have given you: where possible, use Perl directly rather than invoking system. Also, you should get in the habit of including use strict and use warnings in your Perl scripts.

use strict;
use warnings;

use File::Temp;
my $ft = File::Temp->new(
    UNLINK   => 0,
    TEMPLATE => '/tmp/myfile/devid.XXXXXXXXXX',
);

print "Writing to temp file: ", $ft->filename, "\n";

print $ft "Hello, world.\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜