【问题标题】:executing perl script from python从 python 执行 perl 脚本
【发布时间】:2023-03-15 14:37:01
【问题描述】:

我正在尝试从 python 脚本执行 Perl 脚本,但似乎还没有执行! 在 python 中使用这个命令:

os.popen('dump_hash.pl {}'.format(scn_filepath))

这是 dump_hash.pl 脚本:

$path = <STDIN>;
require "$path";
open (OUTFILE, "+>dumpered_hash");
print OUTFILE Dumper("\%...");
close (OUTFILE);

当试图将“dumpered_hash”作为可读文件打开时,脚本卡住了。当我在 shell 中运行 ls 命令时,该目录没有 dumpered_hash 文件。

谢谢你。

【问题讨论】:

  • 脚本如何“未执行”但“尝试打开时卡住”?如果打开文件输出有问题,也许这些链接会帮助你:perldoc.perl.org/File/Path.html#ERROR-HANDLINGstackoverflow.com/questions/3233327/… - 否则,你能提供更详细的信息吗?什么有效,什么无效,如何调用哪个脚本等。还要粘贴确切的错误消息
  • 我的错误,它卡在 os.popen(..) 命令上。
  • 使用subprocess.call('perl dump_hash.pl') 使用subprocess python 模块怎么样?
  • subprocess.Popen('dump_hash.pl {}'.format(scn_filepath)),尝试了 subprocess.call 它没有工作,给出了同样的错误。
  • 如果您在 *nix 下运行,您可能需要在 .pl 文件的开头添加 #! 行,否则您的操作系统没有理由猜测它是 Perl。

标签: python perl unix


【解决方案1】:

你的 Perl 脚本是错误的:

  • 您从 STDIN 读取路径,而不是从命令行参数读取。
  • 读完一行后不要删除换行符。您将寻找 "foo\n" 而不是 "foo"
  • 您没有任何错误检查。
  • 您没有加载Data::Dumper 模块。

你确定你真的想将$path 的文件作为 Perl 代码执行吗?

清理干净,我将 Perl 脚本编写为

perl -MData::Dumper \
  -do $ARGV[0];' \
  -e'print Dumper \%some_global' \
  input-file.pl >output

或作为

use strict; use warnings;
use Data::Dumper
do $ARGV[0];
open my $fh, ">", "output" or die "Can't open output: $!";
print { $fh } Dumper \%some_global;

如果你真的想要来自 STDIN 的文件名:

 use strict; use warnings;
 use Data::Dumper;
 chomp(my $path = <STDIN>);
 do $path;
 ...

【讨论】:

  • 路径用于包含复杂哈希表名称 /../hash.pl 的文件。我已经装载了翻斗车,我有警告。我需要路径中的文件。
  • requiredo 在此用途上是等效的。
  • 仍然没有运行。脚本不会t creat any files. and if create files for it, it doesnt 给他们写任何东西。顺便说一句,该脚本已通过对具有完整路径的文件的要求进行检查。它奏效了。
猜你喜欢
  • 2014-01-06
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
相关资源
最近更新 更多