简体   繁体   English

我可以从标准输入运行 Perl 脚本吗?

[英]Can I run a Perl script from stdin?

Suppose I have a Perl script, namely mytest.pl.假设我有一个 Perl 脚本,即 mytest.pl。 Can I run it by something like cat mytest.pl | perl -e我可以用cat mytest.pl | perl -e之类的东西运行它吗? cat mytest.pl | perl -e ? cat mytest.pl | perl -e ?

The reason I want to do this is that I have a encrypted perl script and I can decrypt it in my c program and I want to run it in my c program.我想这样做的原因是我有一个加密的 perl 脚本,我可以在我的 c 程序中解密它,我想在我的 c 程序中运行它。 I don't want to write the decrypted script back to harddisk due to secruity concerns, so I need to run this perl script on-the-fly, all in memory.由于安全问题,我不想将解密的脚本写回硬盘,所以我需要即时运行这个 perl 脚本,全部在内存中。

This question has nothing to do with the cat command, I just want to know how to feed perl script to stdin, and let perl interpreter to run it.这个问题与cat命令无关,我只想知道如何将perl脚本提供给stdin,并让perl解释器运行它。

perl < mytest.pl 

should do the trick in any shell.应该在任何外壳中都可以做到这一点。 It invokes perl and feeds the script in via the shell redirection operator < .它调用 perl 并通过 shell 重定向运算符<提供脚本。

As pointed out, though, it seems a little unnecessary.不过,正如所指出的,这似乎有点不必要。 Why not start the script with为什么不启动脚本

#!/usr/bin/perl

or perhaps也许

#!/usr/bin/env perl

? ? (modified to reflect your Perl and/or env path) (修改以反映您的Perl和/或env路径)

Note the Useless Use of Cat Award .请注意无用使用 Cat 奖 Whenever I use cat I stop and think whether the shell can provide this functionality for me instead.每当我使用cat我都会停下来思考 shell 是否可以为我提供此功能。

Sometimes one needs to execute a perl script and pass it an argument.有时需要执行一个 perl 脚本并向它传递一个参数。 The STDIN construction perl input_file.txt < script.pl won't work. STDIN 构造perl input_file.txt < script.pl将不起作用。 Using the tip from How to assign a heredoc value to a variable in Bash we overcome this by using a "here-script":使用How to assign a heredoc value to a variable in Bash 中的提示,我们通过使用“here-script”克服了这个问题:

#!/bin/bash
read -r -d '' SCRIPT <<'EOS'
$total = 0;

while (<>) {
    chomp;
    @line = split "\t";
    $total++;
}

print "Total: $total\n"; 
EOS

perl -e "$SCRIPT" input_file.txt
perl mytest.pl 

should be the correct way.应该是正确的方法。 Why are you doing the unnecessary?你为什么要做不必要的事?

cat mytest.pl | perl

…is all you need. …是你所需要的全部。 The -e switch expects the script as a command line argument. -e开关将脚本作为命令行参数。

perl will read the program from STDIN if you don't give it any arguments.如果你不给它任何参数, perl将从STDIN读取程序。

So you could theoretically read an encrypted file, decrypt it, and run it, without saving the file anywhere.因此,理论上您可以读取加密文件、解密并运行它,而无需将文件保存在任何地方。


Here is a sample program:这是一个示例程序:

#! /usr/bin/perl
use strict;
use warnings;
use 5.10.1;

use Crypt::CBC;

my $encrypted = do {
  open my $encrypted_file, '<', 'perl_program.encrypted';
  local $/ = undef;
  <$encrypted_file>;
};

my $key = pack("H16", "0123456789ABCDEF");
my $cipher = Crypt::CBC->new(
  '-key'    => $key,
  '-cipher' => 'Blowfish'
);
my $plaintext = $cipher->decrypt($encrypted);

use IPC::Run qw'run';
run [$^X], \$plaintext;

To test this program, I first ran this:为了测试这个程序,我首先运行了这个:

perl -MCrypt::CBC -e'
  my $a = qq[print "Hello World\n"];
  my $key = pack("H16", "0123456789ABCDEF");
  my $cipher = Crypt::CBC->new(-key=>$key,-cipher=>"Blowfish");
  my $encrypted = $cipher->encrypt($a);
  print $encrypted;
' > perl_program.encrypted

This still won't stop dedicated hackers, but it will prevent most users from looking at the unencrypted program.这仍然不会阻止专门的黑客,但它会阻止大多数用户查看未加密的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM