LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-04-2008, 05:46 AM   #1
uhollins
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
syntax error near unexpected token `newline'


I am trying to learn perl but my basic <stdin> variable assignment will not work.

Im using Ubuntu 9 from the bash... Perl version 5.8.8

Here's the error...
syntax error near unexpected token `newline'

here's the code...

#!/usr/bin/perl -w

printf "\nwhat is your name? \n";
$name = <STDIN>;
 
Old 12-04-2008, 06:30 AM   #2
harry2006
Member
 
Registered: Aug 2006
Location: /hawaii/honolulu/downtown
Distribution: Fedora 10[Cambridge] and Ubuntu 9.04[Jaunty]
Posts: 201

Rep: Reputation: 30
copied the code from ur post and ran it, no probs at all!

run this code
Code:
#!/usr/bin/perl -w
printf "\nwhat is your name? \n";
$name = <STDIN>;
printf "your name is $name";
Just added an extra line to your old code

Last edited by harry2006; 12-04-2008 at 06:34 AM.
 
Old 12-04-2008, 07:02 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
As harry2006 said, it works ok. In general I'd suggest always using:

use strict;

as the 2nd line. It'll save you from problems later. Also, here's a couple of good Perl guides:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
Old 12-04-2008, 09:51 AM   #4
uhollins
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
This is the error that I am getting when I type the ';' after <STDIN>....
If I leave off the ';' after <STDIN>- then I get the 'newline' syntax error
-- This error is so basic could it be an environment issue... I pasted code in from the first reply...




uhollins@uhollins-laptop:~/Perl_Scripts$ . name2

what is your name?
bash: name2: line 3: syntax error near unexpected token `;'
bash: name2: line 3: `$name = <STDIN>;'


This is the name2 code....
#!/usr/bin/perl -w
printf "\nwhat is your name? \n";
$name = <STDIN>;
printf "your name is $name";
 
Old 12-04-2008, 10:24 AM   #5
uhollins
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry I didnt include what I've tried...

I tried the
use strict; - same error occurred.

I've been reading all of the perldocs for beginner mistakes...

I will keep reading til I find the answer...

any suggestion will be greatly appreciated...

thanks to you 2 guys for your post...

I'm using Ubuntu 8.04 and perl 5.8.8... Help please...
 
Old 12-04-2008, 11:30 AM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 241

Rep: Reputation: 45
To run your program you should be typing:
Code:
./name2
By typing ". name" (google: bash source) you are running the file in the current shell and the first line (shebang) is ignored. So it is trying to run as a bash, not perl, script, hence the error.

Last edited by norobro; 12-04-2008 at 11:32 AM.
 
Old 12-04-2008, 01:18 PM   #7
uhollins
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks norobro...

that was it... I was running it as bash not perl... running the script like ./name2

fixed my issue...
thanks everyone for helping I learned a lot from all of the posts....

Last edited by uhollins; 12-05-2008 at 03:51 PM.
 
  


Reply

Tags
perlscript


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
error: unexpected token `newline' CarlosV Programming 4 05-17-2011 03:47 AM
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error near unexpected token deskjockey39 Linux - General 2 01-16-2007 08:47 AM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM


All times are GMT -5. The time now is 08:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration