Exercises and Resources
- Write a loop that counts from 1 to 100, and
- Prints out all even numbers
- Prints out all odd numbers
- Prints out all the numbers that end in 1, 2, or 7
-
Write a loop that counts down from 100 to 1
-
Write a program that reads standard input and prints out any lines that begin with "hello" in any case (uppercase, lowercase, or mixed case). Hint: look at the "perldoc perlrun" manual page, and try to use Perl's built-in switches instead of writing your own loops.
-
Write a logical condition that will check if a scalar is defined, non-zero, a palindrome (the same backwards and forward), or the number 234.98. Test with and without the warnings "-w" flag to the Perl interpreter and the "use strict" compiler pragma. Do you understand the warnings that were generated, if any?
-
Write the following in normal (non-postfix) notation, and simplify them if you can:
- print if $debug;
- $i++ unless $i > 10;
- unless ($j && !$i) { $j += $i while <> };
- next while <>;
Resources
- Read the previous chapters of The road to better programming.
- For related study, take a look at these perldoc pages: perlrun, perlsyn, perlfaq, perlmodlib, strict, and perlop.
- A good tutorial on logic and Boolean algebra is the "Combinational Logic & Systems Tutorial Guide", by David Belton, April 1998.
- For the Perl FAQ, refer to http://www.perl.com/pub/v/faqs or http://perlfaq.cpan.org.
-
"Programming Perl Third Edition"
by Larry Wall, Tom Christiansen, and Jon Orwant (O'Reilly &
Associates, 2000) is the best guide to Perl today, with up-to-date
coverage of 5.005 and 5.6.0.
- Read Teodor's other Perl articles in the developerWorks "Cultured Perl" series:
- A programmer's Linux-oriented setup
- Application configuration with Perl
- Automating UNIX system administration with Perl
- Debugging Perl with ease
- The elegance of JAPH
- Genetic algorithms applied with Perl
- One-liners 101
- Parsing with Perl modules
- Perl 5.6 for C and Java programmers
- Reading and writing Excel files with Perl
- Review of Programming Perl, Third Edition
- Small observations about the big picture
- Writing Perl programs that speak English
- Browse more Linux resources on developerWorks.
- Browse more Open source resources on developerWorks.
View Road to better programming: Chapter 3. Loops, clean code, and the Perl idioms Discussion
Page: 1 2 3 4 5 Next Page: Perl loops