LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perl Setup on Linux (https://www.linuxquestions.org/questions/linux-newbie-8/perl-setup-on-linux-795375/)

sentez_mon_fromage 03-14-2010 03:34 PM

Perl Setup on Linux
 
Hi,

I'm a Linux newbie and I have a bit of an annoying problem with running Perl scripts in Linux.

My system is Ubuntu (Karmic Koala distro) and the problem I have is that Perl scripts I have written do not seem like they are being associated with the perl executable when I try to run them in a terminal.

Basically I want to run a Perl script like the following command:

% hello_world.pl

But I am forced to run it as the following:

% perl hello_world.pl

The problem would seem at first to be with my shebang line. However, I am fairly confident that this is set up correctly. If I run the following, perl -e 'print "$^X\n;"', I can see that the path to the interpreter is 'usr/bin/perl'. Thus my shebang is #!/usr/bin/perl.

Yet running 'hello_world.pl' gives the following response : hello_world.pl: command not found

I am stuck. Do people reckon the problem is due to me not setting up my .bashrc file correctly? Why doesn't Linux recognise, or associate, my shebang with the perl executable?

Many thanks in advance,

Tinkster 03-14-2010 05:14 PM

Hi, welcome to LQ!

What shell are you working with?


Cheers,
Tink

crts 03-14-2010 05:33 PM

Quote:

Originally Posted by sentez_mon_fromage (Post 3898097)
Hi,

I'm a Linux newbie and I have a bit of an annoying problem with running Perl scripts in Linux.

My system is Ubuntu (Karmic Koala distro) and the problem I have is that Perl scripts I have written do not seem like they are being associated with the perl executable when I try to run them in a terminal.

Basically I want to run a Perl script like the following command:

% hello_world.pl

But I am forced to run it as the following:

% perl hello_world.pl

The problem would seem at first to be with my shebang line. However, I am fairly confident that this is set up correctly. If I run the following, perl -e 'print "$^X\n;"', I can see that the path to the interpreter is 'usr/bin/perl'. Thus my shebang is #!/usr/bin/perl.

Yet running 'hello_world.pl' gives the following response : hello_world.pl: command not found

I am stuck. Do people reckon the problem is due to me not setting up my .bashrc file correctly? Why doesn't Linux recognise, or associate, my shebang with the perl executable?

Many thanks in advance,

Hi,

I don't know about perl, but don't you have to call a script like
Code:

./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.

TB0ne 03-14-2010 05:51 PM

Quote:

Originally Posted by crts (Post 3898207)
Hi,
I don't know about perl, but don't you have to call a script like
Code:

./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.

It's not a security measure, but a matter of the shell not being able to find the script.

The PATH environment variable is what the shell looks at. If you were to copy your script into any of the directories that your path contains, it'll run just fine. The "./" merely states the explicit path, being where you are now. If your script was in /software/mytest/directory/myscript.pl, you could either type that entire thing in, or cd into that directory, and type in ./myscript.pl

With a Perl program, *MOST* of the time you put in a "#!/usr/bin/perl" (or wherever your Perl program is located), as the first line of the script. If you do a "chmod 755" on the .pl file, you can just run it by typing in the script name. If you don't have that as the first line, or need some explicit Perl run-time things, you can run it as "perl <script name>".

crts 03-14-2010 06:06 PM

Quote:

It's not a security measure, but a matter of the shell not being able to find the script.
I know opinions differ wether one should include the current directory in your PATH or not, e.g., malicious code that 'disguises' itself as a normal command. I for myself decided not to include it in my PATH and it is a good thing that the shell does not look in the current directory by default. This would be the intuitive behaviour.

MTK358 03-14-2010 07:13 PM

The problem is that the shell does not look into your current working dir for commands.

You must explicitly tell it to look in this dir by prepending "./" to the script name.

chrism01 03-14-2010 07:55 PM

You definitely have to provide the path (whether relative or absolute) to any cmd/script if its location is not in your $PATH variable. By convention, the curent dir (.) and your home dir are not in $PATH (for security reasons).

rockfall 03-15-2010 01:27 AM

type export PATH=$PATH:. or system real hard use

sentez_mon_fromage 03-15-2010 01:42 AM

Firstly, I am overwhelmed by the number of responses here ...

Quote:

Originally Posted by Tinkster (Post 3898190)
Hi, welcome to LQ!

What shell are you working with?

Cheers,
Tink

I am working with a bash shell (/bin/bash)

Quote:

Originally Posted by crts (Post 3898207)
Hi,

I don't know about perl, but don't you have to call a script like
Code:

./hello_world.pl
It is a security measure in linux that you have to explicitly tell your shell to execute a program in your current directory. That is what './' is for. I assume execution permission is set correctly.

Hi, I have tried running './hello_world.pl' but the same error returns (hello_world.pl: command not found). The script has been 'chmod +x'ed for executable so the permission should be set correctly.

Quote:

Originally Posted by TB0ne (Post 3898222)
It's not a security measure, but a matter of the shell not being able to find the script.

The PATH environment variable is what the shell looks at. If you were to copy your script into any of the directories that your path contains, it'll run just fine. The "./" merely states the explicit path, being where you are now. If your script was in /software/mytest/directory/myscript.pl, you could either type that entire thing in, or cd into that directory, and type in ./myscript.pl

With a Perl program, *MOST* of the time you put in a "#!/usr/bin/perl" (or wherever your Perl program is located), as the first line of the script. If you do a "chmod 755" on the .pl file, you can just run it by typing in the script name. If you don't have that as the first line, or need some explicit Perl run-time things, you can run it as "perl <script name>".

The following directory is in my $PATH variable /home/<user>/bin, so I have copied my script to here and ran the script from this directory. I now get a different error:
Quote:

/usr/bin/perl^M: bad interpreter: No such file or directory
So I think that the terminal is not liking my shebang (#!/usr/bin/perl)?

Also from experience a ^M anchor problem is some compatibility issue between Windows and Linux? My machine is dual boot so I wrote this script in Windows originally. However, I am using gvim as a text editor and cannot see any ^M anchors in my code when I open it up in Linux. This is very strange.

evo2 03-15-2010 01:47 AM

You can see the non printing chars by using cat with the -v flag:
Code:

cat -v hello_world.pl
Even if you don't see any, just to be sure you can run dos2unix or "flip -u" on the file.

Evo2.

sentez_mon_fromage 03-15-2010 01:55 AM

Quote:

Originally Posted by rockfall (Post 3898601)
type export PATH=$PATH:. or system real hard use

Tried putting % export PATH=$PATH:. in the terminal and re-running. I still have the same problem.

OK, I have tried writing a brand new script, inside my /home/<user>/bin area. So I am writing this script on my Linux partition. It goes as follows:

Code:

#!usr/bin/perl
print"hello world\n";

I have saved it as "hello_world_2.pl". I have then run the following commands in my terminal:

% chmod 755 hello_world_2.pl
% ./hello_world_2.pl

I then get the following back ...

% bash: ./hello_world_2.pl: usr/bin/perl: bad interpreter: No such file or directory

If I re-run it with 'perl' fixed infront of it ...
% perl ./hello_world_2.pl
I get the expected 'hello world' response.

So there is no longer a ^M anchor being seen on the shebang but there is still something wrong with my bash set-up (I think) and the area of bash etc is still a very grey area for me.

Thanks

mario.almeida 03-15-2010 01:57 AM

Hi,

Your are missing -> /

Change this
#!usr/bin/perl

to this
#!/usr/bin/perl

sentez_mon_fromage 03-15-2010 02:10 AM

Quote:

Originally Posted by evo2 (Post 3898620)
You can see the non printing chars by using cat with the -v flag:
Code:

cat -v hello_world.pl
Even if you don't see any, just to be sure you can run dos2unix or "flip -u" on the file.

Evo2.

OK. Maybe my problems have something to do with my .vimrc set-up for my text editor?

The following appears along the bottom of my gvim editor:
Quote:

"hello_world.pl" [CR missing][dos] 8L, 182C
If I do your suggestion of "cat -v hello_world.pl" I see that every line of my script ends in ^M.

Once I run "dos2unix" on the file the script suddenly works! Thanks.

However, if I open up the file again in gvim, change it again and re-save it the file will not work. If I do a "cat -v" on it the ^Ms have re-appeared.

Re-running dos2unix solves this but there must be some reason as to why my text editor is adding the ^M anchors in the first place?

evo2 03-15-2010 02:14 AM

I don't really feel like participating in a round of editor wars: ... but you should be using emacs!

/me runs away

Evo2.

chrism01 03-15-2010 03:10 AM

Try just using the cmd line vim instead of gvim (after getting rid of the ^M ie dos2unix).


All times are GMT -5. The time now is 07:53 AM.