user2131116 user2131116 - 1 year ago 46
Perl Question

Change the position of program lead to syntax error

I have a program called
. When I use
perl Input Shuffled
to execute , it success work and show no error .

I create a directory called
under my home directory , and I set this path to
. So I can execute the program without typing
to execute . ( This is my first time to do this , maybe some wrong in here)

But when I move the to ~/.tools and execute . it show I have error in line 5 . But if I use
perl ~/.tools/
it can work . So it means it should have no syntax error in my program ,But why it can't work after I put my program to ~/.tools

error message

.tools/ 5: Syntax error: "(" unexpected


set path = (. ~ ~/.tools /sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin )

enter image description here


here is my program

use strict;
use warnings;

use List::Util qw(first max maxstr min minstr reduce shuffle sum);

open(my $fh,"<","$ARGV[0]");
my @Lines = readline($fh);
my @Shuffled = shuffle(@Lines);
close $fh;

open(my $shuf,">","$ARGV[1]");
print $shuf @Shuffled;
close $shuf;

enter image description here

Answer Source

The shebang is used to tell which interpreter should be used for this script. For this to work, the magic number #! has to appear at the immediate beginning of the file. Otherwise, the default interpreter is used.

In this case, the shebang was preceded by a few empty lines. They have to be removed.

The shebang is not parsed when an explicit interpreter is used to execute the file, E.g. in $ perl
It is only important when launched as executable: ./ In that case, the kernel is left to figure out what to do with it: Load into memory as compiled program? Launch an interpreter? Which one? Magic numbers like #! resolve this.

In general, if the shebang doesn't work, the following possible errors can be checked:

  • An UTF byte order mark precedes the #!.
    Diagnosis: A hexdump shows FE FF at the beginning.
    Solution: configure your editor to store files without a BOM
  • The script is encoded in such a way that the beginning does not decode to #! as ASCII.
    Diagnosis: The file does not begin with #! when opened as ASCII or does not begin with 23 21 in a hexdump. Or your editor shows UTF-16 or UTF-32 as the encoding.
    Solution: Store the script in ASCII-compatible encoding. UTF-8 is an especially good choice.
  • Non-native line endings can be confused to be part of the executable name. E.g. with windows line endings, the shebang in

    print 1;

    could be taken as the interpreter name "/usr/bin/perl\r". Many filesystems allow line endings inside filenames.
    Diagnosis: A hexdump shows something other than a space (20) or newline (0A) after the executable name.
    Solution: Convert line endings to Unix.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download