ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.