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, |
Hi, welcome to LQ!
What shell are you working with? Cheers, Tink |
Quote:
I don't know about perl, but don't you have to call a script like Code:
./hello_world.pl |
Quote:
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>". |
Quote:
|
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. |
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).
|
type export PATH=$PATH:. or system real hard use
|
Firstly, I am overwhelmed by the number of responses here ...
Quote:
Quote:
Quote:
Quote:
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. |
You can see the non printing chars by using cat with the -v flag:
Code:
cat -v hello_world.pl Evo2. |
Quote:
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 % 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 |
Hi,
Your are missing -> / Change this #!usr/bin/perl to this #!/usr/bin/perl |
Quote:
The following appears along the bottom of my gvim editor: Quote:
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? |
I don't really feel like participating in a round of editor wars: ... but you should be using emacs!
/me runs away Evo2. |
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. |