目前我正在使用
system("echo $panel_login $panel_password $root_name $root_pass $port $panel_type >> /home/shared/ftp");
Run Code Online (Sandbox Code Playgroud)
使用Perl做同样事情的最简单方法是什么?IE:一个单行.
Tel*_*hus 13
为什么需要一行?你没有按线付款,是吗?这可能过于冗长,但总共需要两分钟才能输入.
#!/usr/bin/env perl
use strict;
use warnings;
my @values = qw/user secret-password ftp-address/;
open my $fh, '>>', 'ftp-stuff' # Three argument form of open; lexical filehandle
or die "Can't open [ftp-stuff]: $!"; # Always check that the open call worked
print $fh "@values\n"; # Quote the array and you get spaces between items for free
close $fh or die "Can't close [ftp-stuff]: $!";
Run Code Online (Sandbox Code Playgroud)
您可能会发现IO :: All有用:
use IO::All;
#stuff happens to set the variables
io("/home/shared/ftp")->write("$panel_login $panel_password $root_name $root_pass $port $panel_type");
Run Code Online (Sandbox Code Playgroud)
http://perldoc.perl.org/functions/open.html
在您的情况下,您必须:
#21st century perl.
my $handle;
open ($handle,'>>','/home/shared/ftp') or die("Cant open /home/shared/ftp");
print $handle "$panel_login $panel_password $root_name $root_pass $port $panel_type";
close ($handle) or die ("Unable to close /home/shared/ftp");
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用autodie pragma(如@Chas Owens 在评论中建议的那样)。这样,就不需要使用检查 (the or die(...)) 部分。
希望这次能做对。如果是这样,将清除此警告。
使用打印(虽然不是一个衬垫)。只需先打开您的文件并获取句柄。
open (MYFILE,'>>/home/shared/ftp');
print MYFILE "$panel_login $panel_password $root_name $root_pass $port $panel_type";
close (MYFILE);
Run Code Online (Sandbox Code Playgroud)
http://perl.about.com/od/perltutorials/a/readwritefiles_2.htm
(open my $FH, ">", "${filename}" and print $FH "Hello World" and close $FH)
or die ("Couldn't output to file: ${filename}: $!\n");
Run Code Online (Sandbox Code Playgroud)
当然,不可能在单行代码中进行正确的错误检查......这应该写得略有不同:
open my $FH, ">", "${filename}" or die("Can't open file: ${filename}: $!\n");
print $FH "Hello World";
close $FH;
Run Code Online (Sandbox Code Playgroud)
您可能想要使用简单的File :: Slurp模块:
use File::Slurp;
append_file("/home/shared/ftp",
"$panel_login $panel_password $root_name $root_pass ".
"$port $panel_type\n");
Run Code Online (Sandbox Code Playgroud)
它不是核心模块,所以你必须安装它.