Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have looked through the forums and I can't seem to find how to fix this. I have a very simple perl code that works fine on Windows, but when I try it on Linux and Solaris I get the following errors:

bash: ./test.pl: line 2: syntax error near unexpected token `('
bash: ./test.pl: line 2: `print '$_\n' for @Config{qw(myuname osname osvers)};'

This is my code:

use Config;
use strict;
print '$_\n' for @Config{qw(myuname osname osvers)};

Any thoughts on how I can get around this?

share|improve this question

closed as unclear what you're asking by slm, Gilles, jasonwryan, Anthon, Zelda Jan 14 at 6:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Are you missing a leading / on the shebang line, so it's #!/usr/bin/perl or is that just a typo? –  Joseph R. Jan 13 at 15:55
I have tried that but am still getting the same error. Not sure why it does not want to pick up the perl compiler –  user974873 Jan 13 at 15:58
Does your file by any chance have windows-ish linebreaks? cat -v test.pl will show you that. –  Jenny D Jan 13 at 16:22
Does not look like it. I got it working by entering /usr/bin/perl test.pl –  user974873 Jan 13 at 16:28
What operating system are you using? What is the exact script (newline convention, #!/usr/bin/perl or #!usr/bin/perl, etc.)? What does file -L /usr/bin/perl say? Whatever problem you're having, it's due to something you aren't telling us. –  Gilles Jan 13 at 23:03

3 Answers 3

up vote 1 down vote accepted

Insert the following into your script:

use strict;
use warnings;

Also, I didn't see the path of your Perl on the machine like /usr/bin/perl at the beginning of the code. You can see the correct form from the shell command prompt by using which perl.

share|improve this answer
I have updated my code above. I have tried to include which compiler to use but am still getting the same error :( when i type in ' whereis perl ' it shows perl: /usr/bin/perl /opt/perl5.8.4/bin/perl /usr/share/man/man1/perl.1.gz –  user974873 Jan 13 at 15:55
what is the output from which perl command ? –  klerk Jan 13 at 15:56
when using 'which perl' it shows /usr/bin/perl –  user974873 Jan 13 at 15:57
make test with only for example print blblblba and with #!usr/bin/perl in the beggining just to check if perl workaing at all –  klerk Jan 13 at 16:00
Looks like it works if I say /usr/bin/perl test.pl. Not sure why it does not want to work if I enter it in the top of the .pl file. Thanks! –  user974873 Jan 13 at 16:17

It seems that the shebang line (#!/usr/bin/perl) isn't interpreted on your system because bash itself is trying to interpret the script. Try invoking the script as:

[user@host]$ perl test.pl

Also, a much better "shebang" line courtesy of the Camel book:

eval "exec /usr/bin/perl -S $0 ${1+"$@"}"
    if $running_under_some_shell;

This allows you to invoke the script as ./test.pl regardless of whether your system understands the shebang line.

Additional note

This is not related to the actual issue, but it's an issue you'll eventually run into when you get Perl to interpret your script: '$_\n' is wrong, you need interpolative quotes (also called soft quotes and double quotes) for $_ to be interpolated and \n to be recognized as an escape sequence as opposed to a literal backslash preceding the letter n.

Several ways you can do this:

print "$_\n"   for @Config{...}


print qq{$_\n} for @Config{...}


#!/usr/bin/perl -l
print for @Config{...}

or (requires Perl 5.10 or higher)

use feature qw(say)
say for @Config{...}
share|improve this answer
I have tried that but am still getting the same error. Not sure why it does not want to pick up the perl compiler :( –  user974873 Jan 13 at 15:58
@user974873 What do you get with file /usr/bin/perl? –  Joseph R. Jan 13 at 16:10
Interesting. Seems to work this way but not when I reference it in the .pl file. Thanks! –  user974873 Jan 13 at 16:16

As others have said, bash is trying to execute your script instead of perl. You can also try using:

#!/usr/bin/env perl

Instead of your current shebang to see if perl picks it up.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.