Can I run a Perl script from stdin?

Suppose I have a Perl script, namely mytest.pl. Can I run it with something like cat mytest.pl | perl -e?

The reason I want to do this is because I have an encrypted perl script and I can decrypt it in my c program and I want to run it in my c program. I do not want to write the decrypted script back to the hard drive due to security problems, so I need to run this perl script on the fly, all in memory.

This question has nothing to do with the command cat, I just want to know how to pass the perl script to stdin, and let the perl interpreter run it.

+5
source share
5 answers
perl < mytest.pl 

. perl script <.

, . script

#!/usr/bin/perl

, ,

#!/usr/bin/env perl

? (, Perl / env)

Cat. , cat, , .

+5
perl mytest.pl 

. ?

+4

perl script . STDIN perl input_file.txt < script.pl . heredoc 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
+3
cat mytest.pl | perl

... , . -e script .

+2

perl STDIN, .

, , , .


:

#! /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;

, :

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 will not stop the highlighted hackers, but it will not allow most users to look at an unencrypted program.

+1
source

All Articles