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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

314APPENDIX ■ PERL BASICSmy $name = "fred";print 'My name is $name\ n';Here we get$ perl varint2.plMy name is $name\ n$Notice that the system prompt is printed at the end of that line because \ n is not a newlinecharacter within the single quotes. This doesn’t just happen in things we print, it happens everytime we construct a string:#!/usr/bin/perl -w# varint3.pluse strict;my $name = "fred";my $salutation = "Dear $name,";print $salutation, "\ n";This gives us$ perl varint3.plDear fred,$This has exactly the same effect asmy $salutation = "Dear " . $name . ",";but is more concise and easier <strong>to</strong> understand.If you need <strong>to</strong> place text immediately after the variable, you can use curly braces <strong>to</strong>delimit the name of the variable. Take this example:#!/usr/bin/perl -w# varint4.pluse strict;my $times = 8;print "This is the $timesth time.\ n";This is syntactically incorrect, because <strong>Perl</strong> looks for a variable $timesth, which hasn’tbeen declared. In this case, we have <strong>to</strong> change the last line by wrapping the variable name incurly braces <strong>to</strong> this:print "This is the ${ times} th time.\ n";

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

Saved successfully!

Ooh no, something went wrong!