Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

$perl <script.pl>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top