使用 Perl 写入文件的最简单方法是什么? [英] What's the easiest way to write to a file using Perl?

查看:81
本文介绍了使用 Perl 写入文件的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用

system("echo $panel_login $panel_password $root_name $root_pass $port $panel_type >> /home/shared/ftp");

使用 Perl 做同样事情的最简单方法是什么?IE:单线.

What is the easiest way to do the same thing using Perl? IE: a one-liner.

推荐答案

EDIT(根据流行和可编辑的需求)

http://perldoc.perl.org/functions/open.html

在您的情况下,您必须:

In your case you would have to :

  #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");

或者,您可以使用 autodie pragma(正如@Chas Owens 在评论中所建议的那样).这样,就不需要使用检查 (the or die(...)) 部分.

Alternatively, you could use the autodie pragma (as @Chas Owens suggested in comments). This way, no check (the or die(...)) part needs to be used.

希望这次能做对.如果是这样,将清除此警告.

Hope to get it right this time. If so, will erase this Warning.

使用打印(虽然不是一个衬垫).只需先打开您的文件并获取句柄.

Use print (not one liner though). Just open your file before and get a handle.

open (MYFILE,'>>/home/shared/ftp');
print MYFILE "$panel_login $panel_password $root_name $root_pass $port $panel_type";
close (MYFILE);

http://perl.about.com/od/perltutorials/a/readwritefiles_2.htm

这篇关于使用 Perl 写入文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆