0

I have written a very simple perl script for Linux to determine the current user logged on. However I keep getting the following error when trying to run it:

bash: use: command not found
bash: my: command not found
bash: ./test.pl: line 9: syntax error near unexpected token `else'
bash: ./test.pl: line 9: `} else {'

This is my code:

#!/usr/bin/perl
use strict;

my $loginName = '';

if ($^O =~ /MSWin/i)
{
$loginName = getlogin;
} else {
#else it is unix
$loginName = getpwuid($<);
}

print $loginName;

I have tried to google this but I dont see what I am doing wrong with my if statement? It works fine on Windows.

Thank you

4
  • 1
    Looks like you are running it with bash. Try perl test.pl
    – John C
    Commented May 13, 2014 at 14:03
  • Show us how you are running your script. Commented May 13, 2014 at 14:05
  • Also, make sure #!/usr/bin/perl is the very first line in your script.
    – John C
    Commented May 13, 2014 at 14:07
  • Ahh your right. I was invoking it wrong. I was doing . ./test.pl, should be perl test.pl. Thank you John
    – user974873
    Commented May 13, 2014 at 14:11

2 Answers 2

2

You are invoking the script incorrectly: these errors are clearly from bash, while perl should be running the script instead.

I don't know how you're running it now, but (assuming its filename is mywhoami) you can always invoke perl explicitly:

perl mywhoami

It should also work to make it executable

chmod a+x mywhoami

and then execute it:

./mywhoami
0

I think it is something it the way you run the script. Please try to run it as follow:

$perl <script.pl>
2
  • 1
    py is not the best extension for a Perl script, is it?
    – choroba
    Commented May 13, 2014 at 14:07
  • @choroba, you are right, I'm used to run python scripts (which have the same behavior), it should be .pl, thx.
    – tal.tzf
    Commented May 13, 2014 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.