-3

I've just started learning perl. I wrote a Hello World program - hello.pl and made it executable using '+x'.

I can execute it using perl hello.pl, but when I try ./hello.pl, an error is coming: Error: no such file "Hello World"

What is the reason?

Edit:

My program

use warnings;
use strict;
use 5.010;

print "Hello World";

My errors:

./hello.pl: line 1: use: command not found ./hello.pl: line 2: use: command not found ./hello.pl: line 3: use: command not found Error: no such file "Hello World"

grovile
  • 45
  • 1
  • 8
  • 6
    Please post the code. From the error message - I'm guessing you've got some backticks in there, or are otherwise doing something strange with your script. – Sobrique Aug 03 '16 at 10:24
  • 1
    That you say it works fine by telling perl to run it, and it doesn't run by itself, should tell you that, called by itself, perl isn't running it. That you specify that you gave it the extension ".pl" and expected it to run under perl, tells me that you're thinking along the lines of win32, where extensions matter. However, they don't matter to unix/linux. Which is why you need the "shbang line" to tell the shell what should execute the executable. – Axeman Aug 03 '16 at 14:29

2 Answers2

10

Use shebang line in the top of the program. It tells the kernel or Apache to interpret the file as a Perl script. and should put the use warnings; and use strict; in the top of the program

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

print "hello world";

And more about shebang

Does the shebang determine the shell which runs the script?

Why should the shebang line always be the first line?

mkHun
  • 5,891
  • 8
  • 38
  • 85
2

Without seeing your code, this is just a guess (why do people think we can debug their programs without seeing them?) but do you have the right kind of quote characters in your program?

You program should look like this:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

say 'Hello World';

Or (with older versions of Perl) like this:

#!/usr/bin/perl

use strict;
use warnings;

print "Hello World\n";

My first version uses single quote characters around the string and my second version uses double quote characters. If you're getting an error saying "no such file" then it seems likely that you're using backticks - which are used to execute an external program. Does your print line look like this:

print `Hello World\n`; # Warning! Wrong kind of quotes!

Update: No, this isn't the problem. If this was the case, you wouldn't be able to run the program with perl hello.pl. This is very likely to be some confusion over the shebang line as mkHun says. But, once again, we can't really help you without seeing your code.

Community
  • 1
  • 1
Dave Cross
  • 68,119
  • 3
  • 51
  • 97