10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

334APPENDIX ■ PERL BASICSdoneAll done!$You can use a last in any looping construct (while, until, for, and foreach). However, thelast does not work with the do { } while or do { } until loops.Note that last1.pl could have been written using an expression modifier. It can be arguedthat this code is a bit more readable:#!/usr/bin/perl -w# last2.pluse strict;while () {last if $_ eq "done\ n";print "You entered: $_";}print "All done!\ n";Going On <strong>to</strong> the NextIf you want <strong>to</strong> skip the rest of the processing of the body, but don’t want <strong>to</strong> exit the loop, youcan use next <strong>to</strong> immediately go execute the next iteration of the loop by testing the expression.Here is an example of a program that reads input from the user, and if the line of input is notblank, the line is printed. It the line is blank, then we immediately go back <strong>to</strong> read the next line:#!/usr/bin/perl -w# next1.pluse strict;print "Please enter some text:\ n";while () {if ($_ eq "\ n") {next;}chomp;print "You entered: [$_]\ n";}Here is an example of running this program in Windows:$ perl next1.plPlease enter some text:testingYou entered: [testing]oneYou entered: [one]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!