Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I'm trying to run my basic Perl script from the file. It has something to do with the syntax, I can't figure it out what it is. This is the command: 

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

This works fine and outputs, Hello World on the next line. However, when I insert this into a vi and save it as the perlOne, the same and run the command:

$ perl perlOne

I get the error:

 "syntax error at perlOne line 1, near "perl -e -- Execution of perlOne aborted due to compilation errors."

It's this same line, but it does not work in my file.

Help me.

1 Answer

0 votes
by (36.8k points)
edited by

The command-line switch -e allows you to run the code from your command line, you need to write your program to the file and then execute it.

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

Output:

Hello World

If you want to run it from your file you will need to write it differently:

#!/usr/bin/perl

use strict;

use warnings;

my $message = 'Hello World';

print $message . "\n";

# You can also make it directly

print "Hello World\n";

It can also be written as:

print "Hello World\n";

To know about Linux join Linux training

Also check out the video below

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
...