LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2008, 12:22 PM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Rep: Reputation: 46
The basic Perl programming


I am new to Perl programming.

I created a folder called 'Perl1'.

I wrote the following program using the 'vi' editor.
The name of the program is 'Hello1'.
I used the command 'chmod 755 Hello1' so this will be an executable one.
I copied this simple basic program from an online tutorial.

http://www.comp.leeds.ac.uk/Perl/basic.html

--------------------------------------
Ni@linux-3vxw:~/Perl1> cat Hello1


#!/usr/local/bin/perl
#
# Program to do the obvious
#
print 'Hello world.'; # Print a message

Ni@linux-3vxw:~/Perl1>
---------------------------------------

I got the following error message:

Ni@linux-3vxw:~/Perl1> ./Hello1
./Hello1: line 7: print: command not found
Ni@linux-3vxw:~/Perl1>

What is the problem?
 
Old 07-21-2008, 12:38 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
A few possibilities.
  • The shebang line (#!/usr/local/bin/perl) must be the very first line of the file - no blank lines may exist before it, and there must be be spaces or other characters before the #! - these must be the first two characters in the file.
  • The script you have assumes the perl interpreter is installed in /usr/local/bin. It might not be on your system. Do find out where perl is installed on your system, do this:
    Code:
    which perl
    If it is not in /usr/local/bin, you will need to adjust your program file accordingly. For example, if which tells you that perl is found at /usr/bin/perl, you should alter the shebang line to look like this:
    Code:
    #!/usr/bin/perl
 
Old 07-21-2008, 12:51 PM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
Thanks Matthew for the reply.

I have perl installed.

Ni@linux-3vxw:~> which perl
/usr/bin/perl
Ni@linux-3vxw:~>


-------------------------------------
I changed the program now. The following are the lines.

#!/usr/bin/perl
#
# Program to do the obvious
#
print " Hello world.\n"; # Print a message
----------------------------------------

I am getting the same old error message.
What is the problem?


---------------------------
Ni@linux-3vxw:~/Perl1> ./Hello1
./Hello1: line 7: print Hello world.\n: command not found
Ni@linux-3vxw:~/Perl1>
------------------------------
I just got the above error message again.

Last edited by Gins; 07-21-2008 at 01:14 PM.
 
Old 07-21-2008, 01:10 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
What happens when you run it with this command:
Code:
perl Hello1
 
Old 07-21-2008, 01:16 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
Thanks Matthew.

It worked fine.

Ni@linux-3vxw:~/Perl1> perl Hello1
Hello world.
Ni@linux-3vxw:~/Perl1>
 
Old 07-21-2008, 02:01 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I think then you must have some problem on the shebang line of the file. Can you post the output of this command for me (please post it in [code] tags to aid readability):
Code:
od -tc Hello1
 
Old 07-21-2008, 03:03 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
For me the following output is gibberish:

Code:
  Ni@linux-3vxw:~> cd Perl1
Ni@linux-3vxw:~/Perl1> od -tc Hello1
0000000  \n  \n   #   !   /   u   s   r   /   b   i   n   /   p   e   r
0000020   l  \n   #  \n   #       P   r   o   g   r   a   m       t   o
0000040       d   o       t   h   e       o   b   v   i   o   u   s  \n
0000060   #  \n   p   r   i   n   t   "       H   e   l   l   o       w
0000100   o   r   l   d   .   \   n   "   ;  \t  \t   #       P   r   i
0000120   n   t       a       m   e   s   s   a   g   e  \n  \n  \n  \n
0000140
Ni@linux-3vxw:~/Perl1>
 
Old 07-21-2008, 03:26 PM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Gins,

The output is saying that the first two lines are empty.

This means that the shebang is not on the first line of the file, but on the third line, therefore the shebang is being interpreted as a comment.

If you remove the two empty lines from the front of your program, you should find that it works.
 
Old 07-21-2008, 03:29 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
As I said:
Quote:
The shebang line (#!/usr/local/bin/perl) must be the very first line of the file - no blank lines may exist before it, and there must be be spaces or other characters before the #! - these must be the first two characters in the file.
The od output I requested shows that there are two blank lines at the start of the file. That is why it does not work as you expect.
 
Old 07-21-2008, 03:30 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by Gins View Post
For me the following output is gibberish:
hehe, yeah it looks a bit weird eh? I just wanted it so I could see if there were any blank lines at the start of the file. It's a useful tool mind you.
 
Old 07-21-2008, 04:51 PM   #11
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
Those blank files were the root cause. If you are a newbie in these things, basic problems are always there.

Now I got it working. I thank both of you.

By the way, the command 'od -tc' is a strange one. This is for the first time I have learnt about it.
How did you find it?
Where can I read more about it?
Please tell me.

--------------------------------------------------------

I created a folder called Java1 and for the first time in my life I ran a java program unsuccessfully.

The following is the program:
-------------------------------------------------

class Hello {
public static void main(Strings args[]){
system.out.println("Hello, world");

}
}
--------------------------------------------------
The name of the program is 'Hello.java'

I copied the program from one of my UNIX books.
The book suggests to run 'javac Hello.java'.

I got the following output:

Ni@linux-3vxw:~/Java1> javac Hello.java
bash: javac: command not found
Ni@linux-3vxw:~/Java1>

The reason to get the above output is that the directory with 'javac' is not in the path.
I maybe wrong here. I am not good at guessing.

The following is my path:
---------------------------------------------------------------

Ni@linux-3vxw:~> $PATH
bash: /usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib64/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/lib/qt3/bin: No such file or directory
Ni@linux-3vxw:~>
---------------------------------------------------------------

I looked whether java is on my system. I just used the command 'find'. It is on the system.
Code:
Ni@linux-3vxw:~> su root
Password:
linux-3vxw:/home/Ni # find / -name java
/var/tmp/dirinstall/usr/share/java
/var/tmp/dirinstall/etc/java
/var/lib/rpm/alternatives/java
/usr/share/java
/usr/share/opera/java
/usr/share/swig/1.3.29/java
/usr/share/doc/packages/db/ref/java
/usr/share/doc/packages/libidn-devel/doc/java
/usr/lib64/java
/usr/lib64/jvm/java-1.5.0-sun-1.5.0_update14/jre/bin/java
/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java
/usr/lib/ooo-2.0/share/Scripts/java
/usr/include/c++/4.1.2/java
/usr/include/c++/4.1.2/gnu/java
/usr/bin/java
/etc/java
/etc/alternatives/java
linux-3vxw:/home/Ni #

However, it didn't find 'javac'.
Code:
linux-3vxw:/home/Ni# find / -name javac
linux-3vxw:/home/Ni #
Your thoughts are welcome.
 
Old 07-21-2008, 05:54 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by Gins
By the way, the command 'od -tc' is a strange one. This is for the first time I have learnt about it.
How did you find it?
Where can I read more about it?
Almost all commands you will use in the shell are either programs which will have their owb manual page, or shell internals.

If you want to know what a program does, like "od" type this in a terminal:
Code:
man od
When you're done reading a manual page, hit 'q' to quit back to the command line.

If doing "man command" doesn't find and documentation, a command might be a shell internal, in which base it will probably be documented in the bash manual page:
Code:
man bash
This is a HUGE lump of documentation, so it's helpful to know that to find a word which is the first thing on a line apart from spaces (there's a big list of commands like this in the bash manual page), you can search by typing:
Code:
/ *thing
/ is the command for search the space and then the * mean "any number of spaces", and then you put your search word. You can then hit 'n' to skip to the next match.

If you have konqueror installed (which is part of KDE), you can browse all the manual pages by entering this into the location/url bar:
Code:
man:/
Failing an existant manual page try:
Code:
command --help
Many programs implement this --help option, and will at least show some basic usage info if you do this.

Quote:
Originally Posted by Gins
[stuff about java]
I'm not a Java coder, but it looks like you don't have the Java development packages installed. I'm also not a Fedora user, so I can't really know what the Java development packages are called. You'd probably do well to post in the Fedora forum.
 
Old 07-21-2008, 05:57 PM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Oh, by the way, find is very slow (searching though directories is a slow process with unix-like filesystems). For this reason, and the usefulness o being able to search the whole filesystem, most modern unix-like OSes have a filesystem indexing program installed.

Instead of
Code:
find / -name javac
try:
Code:
locate javac
The one drawback is that the locate database is only updated every so often (usually once a day or once a week), so it will not help to locate files which have been recently created or moved.
 
Old 07-21-2008, 07:29 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Here's the Perl docs, inc tutorials/examples: http://perldoc.perl.org/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
The very basic Perl problem Gins Linux - General 2 05-01-2007 06:59 AM
[SOLVED] basic programming angel115 Programming 2 10-03-2006 05:58 PM
basic perl module trscookie Programming 10 11-21-2005 01:46 AM
programming like visual basic? toastermaker General 14 11-16-2003 01:23 AM
I'm a BASIC chap, looking for some info on BASIC programming CragStar Programming 2 01-21-2001 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration