SlideShare uma empresa Scribd logo
1 de 7
Some Simple Perl Scripts<br />To get an idea of how Perl works, we'll finish off the first lesson with some simple Perl scripts. We'll build on the items you've learned earlier: scalars and arrays, the if-else, while and for constructs, and the print statement. <br />Since we're writing real perl programs here, there are a few more things you should know about. <br />Statements <br />Statements are complete thoughts in perl, just like sentences. In English, sentences end with a period. In Perl, statements must end with a semi-colon. This is for good reason — in perl the period is used for something else. <br />Comments <br />If you've ever had the nerve to scribble notes in the margin of a novel, you'll know what comments are for. <br />In perl, comments are words within the program that perl itself ignores. They are written by you for your own benefit. They help explain the program so when you look at it later you know what the heck is going on. In perl, comments are set off with the quot;
#quot;
 character. Anything following the # to the end of the line is a comment. For example: <br />#This illustrates a comment.<br /> <br />#Note there is a semi-colon on the next line<br />print quot;
The quick brown fox jumped over the lazy dogquot;
;<br />There is another form of comment which is very useful when you want to chop out large sections of code. Borrowing a technique from Perl's embedded documentation, I like to use it in this way: <br />=comment until cut<br />$variable = 1;<br />print quot;
The variable has the value of $variablequot;
;<br />...<br />...<br />=cut<br />Commenting out large sections like this can be extremely helpful. <br />The Newline Character <br />In the examples leading up to this section, I've used the print statement a lot. Each time, I've added the funny ending quot;
quot;
 onto the print statement. This odd combo of  is the newline character. Without it, the print statement would not go on to the next line. <br />Many people are surprised to learn you have to tell the program to go to a new line each time.  But if it did this all by itself every time, then you could never print out complete lines a bit at a time, like this: <br />$num_words = quot;
eightquot;
;<br />print quot;
There are quot;
;<br />print $num_words;<br />print quot;
 words altogether in this sentence.quot;
;<br />Instead the output would be: <br />There are<br />eight<br /> words altogether in this sentence.<br />Short Forms <br />In perl, some operations are so common they have a shortened form that saves time. These may be strange to the novice, so I'll be careful here. Where appropriate I'll make a comment near the statement to describe the short form. <br />Programming Errors <br />Programming errors? Already? Yes. Programming errors, also affectionately known as bugs, are a fact of programming life. You'll be encountering them soon enough. <br />If you have a mistake in your perl script that makes your meaning unclear, you'll get a message from the perl compiler when you try to run it. To check for these errors before running, you can run perl with the -c flag. And turn on the warnings flag, -w, while you're at it. This picks up hard to find errors too. As an example you'd type in: <br />perl -wc hello.pl<br />to check on the health of the perl script, hello.pl. If there are any syntax errors, you'll hear about them alright! <br />Running the example scripts <br />You can copy any of the following script examples into a file in Notepad and save it as, say, perltest.pl. Then check it for errors by typing <br />perl -wc perltest.pl<br />If it comes back saying quot;
syntax okquot;
, then go ahead and run it by typing <br />perl perltest.pl<br />If it doesn't say quot;
syntax okquot;
, then go and fix the reported error, and try again. <br />Script 1: Adding the numbers 1 to 100, Version 1 <br />$top_number = 100;<br />$x = 1;<br />$total = 0;<br />while ( $x <= $top_number ) {<br />$total = $total + $x;# short form: $total += $x;<br />$x += 1;# do you follow this short form?<br />}<br />print quot;
The total from 1 to $top_number is $totalquot;
;<br />                  <br />Script 2: Adding the numbers 1 to 100. Version 2 <br />This script uses a form of the for loop to go through the integers 1 through 100: <br />$total = 0;<br />#the for loop gives $x the value of all the <br />#numbers from 1 to 100; <br />for $x ( 1 .. 100 ) { <br />$total += $x;    # again, the short form            <br />}<br />print quot;
The total from 1 to 100 is $totalquot;
; <br />                  <br />Script 3: Printing a menu <br />This script uses an array to store flavors. It also uses a terrific form of the for loop to go through them.  <br />@flavors = ( quot;
vanillaquot;
, quot;
chocolatequot;
, quot;
strawberryquot;
 );<br />for $flavor ( @flavors )  {<br />print quot;
We have $flavor milkshakesquot;
;<br />}<br />print quot;
They are 2.95 eachquot;
;<br />print quot;
Please email your order for home deliveryquot;
;<br />Script 4: Going one way or the other: <br />This allows you to program in a word to make a decision. The quot;
nequot;
 in the if statement stands for quot;
not equalquot;
 and is used to compare text. The quot;
diequot;
 statement shows you a way to get out of a program when you're in trouble. <br />#You can program answer to be heads or tails<br />$answer = quot;
headsquot;
;<br />if ( $answer ne quot;
headsquot;
 and $answer ne quot;
tailsquot;
 ) {<br />die quot;
Answer has a bad value: $answer!quot;
;<br />}<br />print quot;
Answer is programmed to be $answer.quot;
;<br />if ( $answer eq quot;
headsquot;
 ) {<br />print quot;
HEADS! you WON!quot;
;<br />} else {<br />print quot;
TAILS?! you lost. Try your coding again!quot;
;<br />}<br />                  <br />Script 5: Going one way or the other, interactively: <br />This allows you to type in a word to make a decision. A shameless sneak peek at the next lesson on input and output. <STDIN> allows us to read a word from the keyboard, and quot;
chompquot;
 is a function to remove the newline that's attached to our answer after hitting the carriage return. <br />print quot;
Please type in either heads or tails: quot;
;<br />#The <STDIN> is the way to read keyboard input<br />$answer = <STDIN>;<br />chomp $answer;<br />while ( $answer ne quot;
headsquot;
 and $answer ne quot;
tailsquot;
 ) {<br />print quot;
I asked you to type heads or tails. Please do so: quot;
;<br />$answer = <STDIN>;<br />chomp $answer;<br />}<br />print quot;
Thanks. You chose $answer.quot;
;<br />print quot;
Hit enter key to continue: quot;
;<br />#This line is here to pause the script<br />#until you hit the carriage return<br />#but the input is never used for anything.<br />$_ = <STDIN>;<br />if ( $answer eq quot;
headsquot;
 ) {<br />print quot;
HEADS! you WON!quot;
;<br />} else {<br />print quot;
TAILS?! you lost. Try again!quot;
;<br />}<br />
Simple perl scripts
Simple perl scripts
Simple perl scripts
Simple perl scripts
Simple perl scripts
Simple perl scripts

Mais conteúdo relacionado

Mais procurados

Mais procurados (7)

Syntax errors
Syntax errorsSyntax errors
Syntax errors
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
 
Introduction to php exception and error management
Introduction to php  exception and error managementIntroduction to php  exception and error management
Introduction to php exception and error management
 
PHP tutorial | ptutorial
PHP tutorial | ptutorialPHP tutorial | ptutorial
PHP tutorial | ptutorial
 
PERL Unit 6 regular expression
PERL Unit 6 regular expressionPERL Unit 6 regular expression
PERL Unit 6 regular expression
 
Php
PhpPhp
Php
 
Handling error & exception in php
Handling error & exception in phpHandling error & exception in php
Handling error & exception in php
 

Destaque

Dfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreDfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreNitesh Jain
 
Dfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreDfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreNitesh Jain
 
Authors metadatarightssocialmediafv
Authors metadatarightssocialmediafvAuthors metadatarightssocialmediafv
Authors metadatarightssocialmediafvpeterclifton
 
99469 633604361858906250
99469 63360436185890625099469 633604361858906250
99469 633604361858906250Nitesh Jain
 
Risk management standard_030820
Risk management standard_030820Risk management standard_030820
Risk management standard_030820Vijay Kejriwal
 

Destaque (8)

Dfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreDfasggfdsnodjfoejoenuretjerfhrejmtklre
Dfasggfdsnodjfoejoenuretjerfhrejmtklre
 
Capm time mgmt fas track as
Capm time mgmt fas track asCapm time mgmt fas track as
Capm time mgmt fas track as
 
Fdasfasfasd
FdasfasfasdFdasfasfasd
Fdasfasfasd
 
Dfasggfdsnodjfoejoenuretjerfhrejmtklre
DfasggfdsnodjfoejoenuretjerfhrejmtklreDfasggfdsnodjfoejoenuretjerfhrejmtklre
Dfasggfdsnodjfoejoenuretjerfhrejmtklre
 
Authors metadatarightssocialmediafv
Authors metadatarightssocialmediafvAuthors metadatarightssocialmediafv
Authors metadatarightssocialmediafv
 
99469 633604361858906250
99469 63360436185890625099469 633604361858906250
99469 633604361858906250
 
Zoskctr hints tips 20090723doc
Zoskctr hints tips 20090723docZoskctr hints tips 20090723doc
Zoskctr hints tips 20090723doc
 
Risk management standard_030820
Risk management standard_030820Risk management standard_030820
Risk management standard_030820
 

Semelhante a Simple perl scripts

Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottO'Reilly Media
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning PerlDave Cross
 
Php Loop
Php LoopPhp Loop
Php Looplotlot
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl coursepartiernlow
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng verQrembiezs Intruder
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaMarko Gargenta
 
Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.PVS-Studio
 
WTFin Perl
WTFin PerlWTFin Perl
WTFin Perllechupl
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command LineMarcos Rebelo
 

Semelhante a Simple perl scripts (20)

Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Php Loop
Php LoopPhp Loop
Php Loop
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl courseparti
 
Bioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-filesBioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-files
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
 
Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Php
PhpPhp
Php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Algorithm and psuedocode
Algorithm and psuedocodeAlgorithm and psuedocode
Algorithm and psuedocode
 
WTFin Perl
WTFin PerlWTFin Perl
WTFin Perl
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command Line
 

Mais de University High School - Fresno (11)

Dah time mgmt matching exercise answers
Dah time mgmt matching exercise answersDah time mgmt matching exercise answers
Dah time mgmt matching exercise answers
 
Capm ccvc capm time 4th edition pmbok review
Capm ccvc capm time   4th edition pmbok reviewCapm ccvc capm time   4th edition pmbok review
Capm ccvc capm time 4th edition pmbok review
 
Capm test taking preparation hints
Capm test taking preparation hintsCapm test taking preparation hints
Capm test taking preparation hints
 
Capm process cheat sheet
Capm process cheat sheetCapm process cheat sheet
Capm process cheat sheet
 
Capm time 150 question final key
Capm time 150 question final keyCapm time 150 question final key
Capm time 150 question final key
 
Capm scenario identifying the critical path answers
Capm scenario identifying the critical path   answersCapm scenario identifying the critical path   answers
Capm scenario identifying the critical path answers
 
Capm scenario identifying the critical path answers
Capm scenario identifying the critical path   answersCapm scenario identifying the critical path   answers
Capm scenario identifying the critical path answers
 
Capm time mgmt matching exercise
Capm time mgmt matching exerciseCapm time mgmt matching exercise
Capm time mgmt matching exercise
 
Source code examples
Source code examplesSource code examples
Source code examples
 
Business computing survey
Business computing surveyBusiness computing survey
Business computing survey
 
Watson
WatsonWatson
Watson
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Simple perl scripts

  • 1. Some Simple Perl Scripts<br />To get an idea of how Perl works, we'll finish off the first lesson with some simple Perl scripts. We'll build on the items you've learned earlier: scalars and arrays, the if-else, while and for constructs, and the print statement. <br />Since we're writing real perl programs here, there are a few more things you should know about. <br />Statements <br />Statements are complete thoughts in perl, just like sentences. In English, sentences end with a period. In Perl, statements must end with a semi-colon. This is for good reason — in perl the period is used for something else. <br />Comments <br />If you've ever had the nerve to scribble notes in the margin of a novel, you'll know what comments are for. <br />In perl, comments are words within the program that perl itself ignores. They are written by you for your own benefit. They help explain the program so when you look at it later you know what the heck is going on. In perl, comments are set off with the quot; #quot; character. Anything following the # to the end of the line is a comment. For example: <br />#This illustrates a comment.<br /> <br />#Note there is a semi-colon on the next line<br />print quot; The quick brown fox jumped over the lazy dogquot; ;<br />There is another form of comment which is very useful when you want to chop out large sections of code. Borrowing a technique from Perl's embedded documentation, I like to use it in this way: <br />=comment until cut<br />$variable = 1;<br />print quot; The variable has the value of $variablequot; ;<br />...<br />...<br />=cut<br />Commenting out large sections like this can be extremely helpful. <br />The Newline Character <br />In the examples leading up to this section, I've used the print statement a lot. Each time, I've added the funny ending quot; quot; onto the print statement. This odd combo of is the newline character. Without it, the print statement would not go on to the next line. <br />Many people are surprised to learn you have to tell the program to go to a new line each time.  But if it did this all by itself every time, then you could never print out complete lines a bit at a time, like this: <br />$num_words = quot; eightquot; ;<br />print quot; There are quot; ;<br />print $num_words;<br />print quot; words altogether in this sentence.quot; ;<br />Instead the output would be: <br />There are<br />eight<br /> words altogether in this sentence.<br />Short Forms <br />In perl, some operations are so common they have a shortened form that saves time. These may be strange to the novice, so I'll be careful here. Where appropriate I'll make a comment near the statement to describe the short form. <br />Programming Errors <br />Programming errors? Already? Yes. Programming errors, also affectionately known as bugs, are a fact of programming life. You'll be encountering them soon enough. <br />If you have a mistake in your perl script that makes your meaning unclear, you'll get a message from the perl compiler when you try to run it. To check for these errors before running, you can run perl with the -c flag. And turn on the warnings flag, -w, while you're at it. This picks up hard to find errors too. As an example you'd type in: <br />perl -wc hello.pl<br />to check on the health of the perl script, hello.pl. If there are any syntax errors, you'll hear about them alright! <br />Running the example scripts <br />You can copy any of the following script examples into a file in Notepad and save it as, say, perltest.pl. Then check it for errors by typing <br />perl -wc perltest.pl<br />If it comes back saying quot; syntax okquot; , then go ahead and run it by typing <br />perl perltest.pl<br />If it doesn't say quot; syntax okquot; , then go and fix the reported error, and try again. <br />Script 1: Adding the numbers 1 to 100, Version 1 <br />$top_number = 100;<br />$x = 1;<br />$total = 0;<br />while ( $x <= $top_number ) {<br />$total = $total + $x;# short form: $total += $x;<br />$x += 1;# do you follow this short form?<br />}<br />print quot; The total from 1 to $top_number is $totalquot; ;<br /> <br />Script 2: Adding the numbers 1 to 100. Version 2 <br />This script uses a form of the for loop to go through the integers 1 through 100: <br />$total = 0;<br />#the for loop gives $x the value of all the <br />#numbers from 1 to 100; <br />for $x ( 1 .. 100 ) { <br />$total += $x; # again, the short form <br />}<br />print quot; The total from 1 to 100 is $totalquot; ; <br /> <br />Script 3: Printing a menu <br />This script uses an array to store flavors. It also uses a terrific form of the for loop to go through them.  <br />@flavors = ( quot; vanillaquot; , quot; chocolatequot; , quot; strawberryquot; );<br />for $flavor ( @flavors ) {<br />print quot; We have $flavor milkshakesquot; ;<br />}<br />print quot; They are 2.95 eachquot; ;<br />print quot; Please email your order for home deliveryquot; ;<br />Script 4: Going one way or the other: <br />This allows you to program in a word to make a decision. The quot; nequot; in the if statement stands for quot; not equalquot; and is used to compare text. The quot; diequot; statement shows you a way to get out of a program when you're in trouble. <br />#You can program answer to be heads or tails<br />$answer = quot; headsquot; ;<br />if ( $answer ne quot; headsquot; and $answer ne quot; tailsquot; ) {<br />die quot; Answer has a bad value: $answer!quot; ;<br />}<br />print quot; Answer is programmed to be $answer.quot; ;<br />if ( $answer eq quot; headsquot; ) {<br />print quot; HEADS! you WON!quot; ;<br />} else {<br />print quot; TAILS?! you lost. Try your coding again!quot; ;<br />}<br /> <br />Script 5: Going one way or the other, interactively: <br />This allows you to type in a word to make a decision. A shameless sneak peek at the next lesson on input and output. <STDIN> allows us to read a word from the keyboard, and quot; chompquot; is a function to remove the newline that's attached to our answer after hitting the carriage return. <br />print quot; Please type in either heads or tails: quot; ;<br />#The <STDIN> is the way to read keyboard input<br />$answer = <STDIN>;<br />chomp $answer;<br />while ( $answer ne quot; headsquot; and $answer ne quot; tailsquot; ) {<br />print quot; I asked you to type heads or tails. Please do so: quot; ;<br />$answer = <STDIN>;<br />chomp $answer;<br />}<br />print quot; Thanks. You chose $answer.quot; ;<br />print quot; Hit enter key to continue: quot; ;<br />#This line is here to pause the script<br />#until you hit the carriage return<br />#but the input is never used for anything.<br />$_ = <STDIN>;<br />if ( $answer eq quot; headsquot; ) {<br />print quot; HEADS! you WON!quot; ;<br />} else {<br />print quot; TAILS?! you lost. Try again!quot; ;<br />}<br />