On Tue, Mar 19, 2013 at 12:21:10AM -0300, Brian Fraser wrote: > You can just pipe a program into perl and it'll DWIM: > > $ echo 'print "Hello World, Perl $^V\n"' | perl
You can also be more explict about the program being on STDIN: bash$ echo 'print "Hello , World!\n"' | perl - The - tells it to read the program from STDIN. This allows you to pass arguments to the program if necessary. bash$ echo 'print "$_\n" for @ARGV' | perl - foo bar baz From within Perl you can open a pipe to perl. For example: #!/usr/bin/perl use strict; use warnings; my $program = <<'EOF'; use strict; use warnings; print "$_\n" for @ARGV; EOF my @args = qw(foo bar baz); open my $fh, '|-', qw(perl -), @args or die "open pipe: $!"; print $fh $program or die "write to pipe: $!"; close $fh or $! == 0 or warn "close pipe: $!"; __END__ See perldoc -f open and perldoc perlrun. Of course, there's always eval() too, but that has certain drawbacks (in particular it executes within the scope and context of your program and can therefore tamper with it). #!/usr/bin/perl use strict; use warnings; hello(); # Prints "Hello, World\n!" my $program = <<'EOF'; use strict; use warnings; hello(); # Prints "Hello, Satan!\n" sub hello { print "Hello, Satan!\n"; } EOF eval $program; die $@ if $@; hello(); # Oops, prints "Hello, Satan!\n" sub hello { print "Hello, World!\n"; } __END__ Here is an evil example where the evaled program tampered with the outer program in ways that may be undesirable. If you control the inner program then you can do things to make sure that doesn't happen accidentally (and avoid doing it intentionally). Thanks to the warnings pragma we do get a warning about it, but that can be surpressed, and regardless the damage may be done already. See perldoc -f eval and perldoc warnings. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bamccaig.com/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
signature.asc
Description: Digital signature