23

I have looked through a number of tutorials, but I still can't figure out what I am doing wrong... I am trying the code below (in a .pl Perl file, as an executable):

#!/usr/bin/perl

perl -e 'print "Hello";' 

I run this script and get:

Execution of /home/user1/Desktop/file_backups.pl aborted due to compilation errors.

(I'm new to using Perl to call the Linux command line.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rick
  • 16,612
  • 34
  • 110
  • 163
  • 1
    Do you mean to have Perl call out to the linux (shell) command line, or have the shell call Perl? – Ether Jul 10 '10 at 18:06
  • This question is tagged with Linux, but this is related: *[Why doesn't my Perl one-liner work on Windows?](https://stackoverflow.com/questions/660624)* – Peter Mortensen May 25 '20 at 16:16

7 Answers7

17

Try:

#!/usr/bin/perl
# This is a comment ~~~
# This script will be run as a Perl script
# since 'perl' isn't a keyword or function in Perl
# something like this must fail:
#
# perl -e 'print "Hello";' 
#
# The following should work.

print "Hello"; print " World\n";

Or, if you want your shell script to execute Perl code:

#!/bin/sh
# That's a Bash script ~~~
# It's just a command line in a file ...    

perl -e 'print "Hello World";' 

Background: #! is an interpreter directive.

When the command is executed, it is converted to an execution of the interpreter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
miku
  • 181,842
  • 47
  • 306
  • 310
15

Just type on the command line (not in a file):

perl -e 'print "Hello World\n";'

This is only really good for one-liners. Longer scripts need their own file.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Henno Brandsma
  • 2,116
  • 11
  • 12
14

perl is not a valid command inside a Perl script. If you had named that file as a .sh script, and used #!/bin/bash on the shebang line, it would have worked, but it doesn't really make a lot of sense to write a bash file just to invoke Perl (why not invoke Perl directly?)

Since you mentioned you want to interact with the command line, I'll mention here that you can get at the command line options within Perl via the @ARGV array. (See perldoc perlvar.)

Ether
  • 53,118
  • 13
  • 86
  • 159
8

This should work. Double quotes outside single quotes inside ;)

perl -e "print 'Hello'"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Slak
  • 578
  • 10
  • 13
  • I'm sorry to revive this old post, but where can I look at the meaning of the options? (i.e. your "-e") Thanks – Sos Oct 30 '14 at 10:49
  • @Sos: That is a good question. One would think it is on the [man page for Perl](https://linux.die.net/man/1/perl), but it is ***not*** (or is very, very terse in the case of `-e`). The commonly used options `-l` (l as the last letter in "Perl") and `-n` are not documented at all on the man page... The keyword is [perlrun](https://perldoc.perl.org/perlrun.html) (in the man system: [perlrun](https://linux.die.net/man/1/perlrun)), but it does not work out of the box as a man page. – Peter Mortensen May 25 '20 at 15:44
  • Stack Overflow question *[Is there a man page for `perl` itself?](https://stackoverflow.com/questions/12879858)* – Peter Mortensen May 25 '20 at 16:05
  • $ perldoc perlrun (actually man perlrun works on my Debian derived Bunsen install) – AAAfarmclub Aug 28 '20 at 01:53
0

Say you have the following inputs:

$ for i in a b c ; do echo "$i is $i" > $i; done

$ cat a b c
a is a
b is b
c is c

Everybody knows capital letters are much better!

perl -i.bak -pe '$_ = uc' a b c

So now

$ cat a b c
A IS A
B IS B
C IS C

But we'd really like to can this in a command called upcase, and that's easy to do!

#! /usr/bin/perl -pi.bak
$_ = uc

See it at work:

$ for i in a b c ; do echo "$i is $i" > $i; done

$ cat a b c
a is a
b is b
c is c

$ ./upcase a b c

$ !cat
cat a b c
A IS A
B IS B
C IS C

More tips from an answer to a similar question:

The -e option introduces Perl code to be executed—which you might think of as a script on the command line—so drop it and stick the code in the body. Leave -p in the shebang (#!) line.

In general, it's safest to stick to at most one "clump" of options in the shebang line. If you need more, you could always throw their equivalents inside a BEGIN {} block.

Don't forget to turn on the execute bit!

chmod +x script-name
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
-1

You might be trying to do this:

system("/usr/bin/perl -e 'print \"Hello!\\\\n\";'");
eruciform
  • 7,680
  • 1
  • 35
  • 47
  • Thanks for the responses.. well what I'm trying to do is write perl scripts that interact with the command line, I guess I had it backwards as what I was looking at are bash / shell scripts to call perl – Rick Jul 10 '10 at 17:47
  • 3
    This is just bad advice. OP wants to print hello inside a Perl script. So just use print. – ghostdog74 Jul 10 '10 at 17:49
  • @ghostdog: i'm not trying to advise this, it's just an answer to a direct question. i'm trying not to judge. it looks like he's looking for the opposite, anyways, so we're both off. :-) – eruciform Jul 10 '10 at 17:55
-1

Since you said you're looking for the opposite, this may be what you mean:

# vi stdin.pl
# cat stdin.pl
  #!/usr/bin/perl
  while(<STDIN>)
  {
      if( /^hello/ ){ print "Hello back to ya!\n"; }
  }
# chmod 0777 stdin.pl
# ./stdin.pl
  foo
  hello
  Hello back to ya!
#
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eruciform
  • 7,680
  • 1
  • 35
  • 47