LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-05-2011, 12:29 PM   #1
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
Perl Problem


I copied a perl program and execute it out of the book and I get nothing. I doubled checked and trippled check made sure everything was type correctly and the program doesn't execute how the book said it should.

Code:
#!/usr/bin/perl -w  
 
print "Your weight:";  
$_=<STDIN>;  
chomp;  
s/^\s+//;   # Remove leading spaces, if any.  
if (m/(lbs?|kgs?|kilograms?|pounds?)/i) {  
        if (s/\s*(kgs?|kilograms?).*//) {  
               $_*=2.2;  
        } else {  
               s/\s*(lbs?|pounds?).*//;  
        }  
}  
print "Your weight on the moon: ", $_*.16667, " lbs\n";
it suppose to print something like
Quote:
./moon.pl
Your weight: 150: lbs
Your weight on the moon: 25.00005lbs

or

./moon.pl
Your weight: 90kg
Your weight on the moon: 90.9090 lbs
And if your wondering the lesson example is from a lesson on expression, pattern finding and manipulation from book "Sams Teach Yourself Perl in 24 Hours".

The book is based on perl 5.6 and I use v5.10.1 so maybe it was a updated that changed how the code worked?

Last edited by jmc1987; 11-05-2011 at 12:41 PM.
 
Old 11-05-2011, 12:33 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jmc1987 View Post
I copied a perl program and execute it out of the book and I get nothing. I doubled checked and trippled check made sure everything was type correctly and the program doesn't execute how the book said it should.

Code:
#!/usr/bin/perl -w  
 
print "Your weight:";  
$_=<STDIN>;  
chomp;  
s/^\s+//;   # Remove leading spaces, if any.  
if (m/(lbs?|kgs?|kilograms?|pounds?)/i) {  
        if (s/\s*(kgs?|kilograms?).*//) {  
               $_*=2.2;  
        } else {  
               s/\s*(lbs?|pounds?).*//;  
        }  
}  
print "Your weight on the moon: ", $_*.16667, " lbs\n";
The book is based on perl 5.6 and I use v5.10.1 so maybe it was a updated that changed how the code worked?
What is "nothing" ? E.g. is any message printed ?

Anyway, start from here: http://perldoc.perl.org/ - I've seen too many wrong examples in this forum copied from all kinds of strange sites/books.
 
0 members found this post helpful.
Old 11-05-2011, 12:47 PM   #3
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
The script should work, at least it does on my system (slackware 13.37, perl v.5.12.3)
 
Old 11-05-2011, 12:53 PM   #4
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by Cedrik View Post
The script should work, at least it does on my system (slackware 13.37, perl v.5.12.3)
Are you able to interact with it by typing in a weight and getting output. when I run it just does nothing.
 
Old 11-05-2011, 01:32 PM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Yes, I type weight values and get output...
 
Old 11-05-2011, 01:34 PM   #6
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Okay weird story I'm editing with gedit and the output would be

Quote:
@james@xion:~/Documents/Perl$./moon.pl
@james@xion:~/Documents/Perl$
I recreated the file using cli editor nano and it got the output I should

Quote:
james@xion:~/Documents/Perl$ ./moon.pl
Your weight:100 lbs
Your weight on the moon: 16.667 lbs
james@xion:~/Documents/Perl$ ./moon.pl
Your weight: 90 kg
Your weight on the moon: 33.00066 lbs
james@xion:~/Documents/Perl$
So why it wouldn't work the first go around who knows?
 
Old 11-05-2011, 01:36 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jmc1987 View Post
Okay weird story I'm editing with gedit and the output would be



I recreated the file using cli editor nano and it got the output I should



So why it wouldn't work the first go around who knows?
Create it again with 'gedit', save it with a different name, make sure it doesn't work, 'diff' the two files.
 
Old 11-05-2011, 02:02 PM   #8
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Okay I uploaded to files moon.pl.txt works for me and moont.pl.txt doesn't do nothing and shows no errors.

They both look the same to me
Attached Files
File Type: txt moon.pl.txt (339 Bytes, 14 views)
File Type: txt moont.pl.txt (339 Bytes, 13 views)

Last edited by jmc1987; 11-05-2011 at 02:04 PM.
 
Old 11-05-2011, 02:43 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jmc1987 View Post
Okay I uploaded to files moon.pl.txt works for me and moont.pl.txt doesn't do nothing and shows no errors.

They both look the same to me
No, they are different:

Code:
sergei@amdam2:~> diff moon.pl.txt moont.pl.txt
1,14c1
< #!/usr/bin/perl -w
<
< print "Your weight:";
< $_=<STDIN>;
< chomp;
< s/^\s+//;   # Remove leading spaces, if any.
< if (m/(lbs?|kgs?|kilograms?|pounds?)/i) {
<         if (s/\s*(kgs?|kilograms?).*//) {
<                $_*=2.2;
<         } else {
<                s/\s*(lbs?|pounds?).*//;
<         }
< }
< print "Your weight on the moon: ", $_*.16667, " lbs\n";
---
print "Your weight on the moon: ", $_*.16667, " lbs\n";
\ No newline at end of file
sergei@amdam2:~>
- it may be one of them has DOS line endings:

Code:
sergei@amdam2:~> file moon.pl.txt
moon.pl.txt: a /usr/bin/perl -w script text
sergei@amdam2:~> file moont.pl.txt
moont.pl.txt: a /usr/bin/perl -w\015\015print "Your w script text
sergei@amdam2:~>
.


Read 'man hexdump' and start further investigation.
 
  


Reply



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
problem with perl modules declaration and perl/cgi script shifter Programming 9 02-24-2010 09:09 AM
need perl help...perl module problem john83reuben Programming 2 03-22-2008 09:08 PM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

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

All times are GMT -5. The time now is 06:30 PM.

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
Open Source Consulting | Domain Registration