【问题标题】:Where does pp (PAR) unpack add (-a) files?pp (PAR) 解压添加 (-a) 文件在哪里?
【发布时间】:2009-07-14 14:18:27
【问题描述】:

这是我试图解决"Why don’t my system calls work in the Perl program I wrap with pp?" 提出的无关问题的尝试,我在 linux 系统上创建了一个简单的 Perl 脚本:

new-net:~/scripts # cat ls_test.pl
@ls_out = `ls -l`;

map { print "$_\n" } @ls_out;

$out = `sh out_test.sh`;

print "$out\n";

这个脚本调用一个简单的shell文件:

new-net:~/scripts # cat out_test.sh
echo "I'm here"

我使用 pp 将 Perl 脚本与 shell 脚本一起打包到 ls_test 中:

new-net:~/test # 解压 -l ls_test 存档:ls_test 长度 日期 时间 名称 -------- ---- ---- ---- 0 07-13-09 16:41 脚本/ 436 07-13-09 16:41 清单 214 07-13-09 16:41 META.yml 93 07-13-09 16:41 脚本/ls_test.pl 538 07-13-09 16:41 脚本/main.pl 16 07-13-09 16:20 out_test.sh -------- -------- 1297 6 档

如果我在空目录中运行打包文件,则找不到 shell 脚本:

新网:~/test # ./ls_test 总计 3391 -rwxr-xr-x 1 root root 3466177 Jul 13 16:41 ls_test sh: out_test.sh: 没有这样的文件或目录

如果我将 shell 脚本复制到目录中,打包的脚本会按预期运行:

新网:~/test # ./ls_test
总计 3395 -rwxr-xr-x 1 root root 3466177 Jul 13 16:41 ls_test -rw-r--r-- 1 根 16 Jul 13 16:20 out_test.sh 我在这

那么,pp 打包脚本期望在哪里找到包含的文件?以及如何在原始 Perl 脚本中配置对该包含文件的调用?

【问题讨论】:

    标签: linux perl par perl-packager


    【解决方案1】:

    打包的可执行文件中的文件被提取到一个临时目录(通常是 /tmp/par-USERNAME/cache-XXXXXXX)。 要访问这些文件,请执行以下操作:

    #!/usr/bin/perl
    
    # Reads a data file from the archive (added with -a)
    print PAR::read_file("data");
    
    # Will execute "script2" in the archive and exit. Will not return to this script.
    require PAR;
    PAR->import( { file => $0, run => 'script2' } );
    

    您还可以创建与您要运行的脚本同名的可执行文件的符号链接,然后运行这些链接。

    实际上,重新阅读您的问题,简单地访问 PAR_TEMP 环境变量可能更有用:

    #!/usr/bin/perl
    use File::Slurp qw(slurp);
    
    $data_dir = "$ENV{PAR_TEMP}/inc";
    $script_dir = "$data_dir/script";
    
    print slurp("$data_dir/datafile");
    
    # file access permissions are not preserved as far as I can tell,
    # so you'll have to invoke the interpreter explicitly.
    system 'perl', "$script_dir/script2", @args;
    

    【讨论】:

    • 来自 perldoc PAR; run 要在 par 中运行的脚本的名称。完成后退出。这是否意味着对添加文件的任何调用都会退出打包脚本?我最初试图解决的问题是一个 POE/TK 脚本,它从小部件命令中调用各种外部文件。
    【解决方案2】:

    感谢大家为这个答案做出贡献。我添加这个答案是为了提炼出每个人的宝贵意见,我曾经提出在我的特定应用程序中对我有用的解决方案。

    该应用程序使用 POE 和 Tk 用 ActiveState Perl 编写,并使用 pp 打包分发。它使用了几个外部文件;一些用于程序的输入(来自 DNS 的修改数据),一些用于用户执行的操作(创建和删除 DNS 别名记录)。

    PAR 打包程序 (pp) 使用 -a 参数包含外部文件。这些文件被解压到包创建的“temp”路径下的 \inc 目录中,并通过

    提供给脚本
    $ENV{PAR_TEMP}

    解决方案的第一步是将此信息添加到 POE“$heap”。下面的行处于内联状态“_start”;

    $heap->{t_path} = "$ENV{PAR_TEMP}\\inc\\";

    当我在 Win32 环境中工作时,我使用转义的反斜杠将 \inc 目录附加到临时路径中。

    当调用外部文件输入应用程序时,我使用了一个变量(@zone)来返回数据;

    @zone = `$heap->{t_path}dnscmd aus-dc1 /enumrecords company.pvt @`;

    对于完成外部操作的调用,调用文件而不保存输出;

    `$heap->{t_path}cpau -dec -file $heap->{t_path}del_event.job -nowarn -wait`;

    再次感谢大家的贡献,并感谢 stackoverflow 提供了这个伟大的环境。

    【讨论】:

    • 使用File::Spec->catfile( $ENV{PAR_TEMP}, 'inc') 而不是使用双反斜杠。顺便说一句,我有点惊讶我是唯一一个在这个帖子中对任何答案都投赞成票的人:你对 SO 表示赞赏的方式就是对答案投赞成票。
    【解决方案3】:

    这是在我的系统上运行的东西:

    C:\tmp> cat build.bat @echo 关闭 mkdir 输出 调用 pp -o runner.exe runner.pl -a sayhello.bat 移动 runner.exe 输出\runner.exe
    C:\tmp> cat sayhello.bat
    @echo 我在打招呼...
    
    
    C:\tmp> cat runner.pl
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use File::Spec::Functions qw( catfile );
    
    my $prog_path = catfile $ENV{PAR_TEMP}, inc => 'sayhello.bat';
    
    my $output = `$prog_path`;
    
    print "The output was: >>> $output <<< ";
    
    __END__
    

    输出:

    C:\tmp\output> runner.exe
    The output was: >>> I am saying hello ...
    <<<
    

    不过,这感觉有点脏。

    【讨论】:

    • 看起来在 MSWindows 上,PAR 的默认缓存/临时目录是当前目录。
    • 不,C:\DOCUME~1\username\LOCALS~1\Temp\par-username\cache-07b6bc6c42c824d8fdd5abd08eb8b67b2bf7ecab ... 但是,您的评论让我意识到我忘记修复 @ 987654323@在帖子中。
    • 使用 PAR_TEMP 很脏?我还没有找到任何其他方法来引用提取目录。您当然可以自己提取存档,然后您就知道它在哪里。
    • 不是 ... 感觉 ... 有点 ...无论如何,鉴于我对fsutil hardlink create 没有任何运气,这似乎是唯一的选择。
    • 我可以看到使用 PAR_TEMP 可能有点脏,因为它在 PAR 或 pp 的联机帮助页中都没有提到。但我怀疑它会消失,因为它很方便.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多