Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perl #2 Looking at Examples
#1
Learning Perl Tutorial #2


Today we will just look at some code, then ill explain the lines. All of my tutorials usually are just me explaining lines of code to you. With this series i will try to tell you more about Perl, instead of just the variables.

Ok first example :
Code:
#!/usr/bin/perl -w
#This code wil print the input onto the screen

print 'Echo? ';
$input = <STDIN>;
print $input;

First line is our shebang line with the warning

Next line is a comment, Perl only supports single line comments. So you will have to keep your comments on one line or create another line with the #
Perl will ignore everything on the line that has # at the begining

Next line is print, this just prints the word Echo? onto the screen. Then you see the semicolon, you need to add this to the end of almost every line

Now you see the $input = <STDIN> What this does is takes what you type and creates it into the variable $input. So it can be called on later

Last line is just calling the input variable to be printed to the screen. So now whatever you typed will be printed onto the screen after the words Echo?

Now onto Example 2 :
Code:
#!/usr/bin/perl -w

$cookie = "";

while ( $cookie ne cookie ) {
   print 'Give me a cookie';
   chomp($cookie = <STDIN>;
}

print "Mmmmmm good cookie!\n";

Ok so First line is our normal shebang line, i will stop mentioning this in the explanation from now on

Ok now you see the cookie variable. Variables are stated with the $ symbol. You can see cookie has nothing stated as its string. That is because it will be determined by the users input. Meaning whatever the person types in will be the string.

Next is the while line, This starts the while loop. This says while cookie = "" the whole ne thing is basically saying if they dont type cookie, then dont end the script. So if you type cookie in it will take to the print "mmmmm good cookie!" line. Which would end the script.

If you dont type in cookie it will take you to the print 'Give me a cookie' line.

This other line is something i will go into later on. But basically chomp just strips the newline character off the end of whatever you typed.

Ok now i will take some work out of my book and tell you to do it. Then later i will post the answers!
QUIZ

1. What does Perl Stand for? What does it mean?
3. Whats the most recent version of Perl.
4. What are the basic differences between compiled and interpreted languages. Which one is Perl? Why is it useful?
5. What does the shebang line in Perl do?
6. How do you create comments in Perl?
7. What are Perl Warnings? How do you turn them on in your platform? Why are they useful?

EXERCISES

1. Create a Hello World script and make it print the greeting twice.
2. Create a Hello Worl script and make it print the greeting twice on the same line.
3. Whats wrong with this?
Code:
!/usr/bin/perl -w
  print "Hello World\n";
4.Whats wrong with this? (2 errors)
Code:
#!/usr/bin/perl -w
  print 'Enter your name: '
  # save the data $inputeline = <STDIN>;
  print $inputline;
5.I want you to create a hello world program that calls on a variable. (Be creative!)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compile Error in Perl liveproject101 0 1,304 04-12-2013, 03:08 AM
Last Post: liveproject101
  [Perl] IRC Bot wchar_t 26 14,636 08-20-2012, 01:40 PM
Last Post: Trump
  Perl Ebook ven0m 2 1,834 05-04-2012, 08:14 AM
Last Post: ven0m
  Help with a program written in perl Rodman42866 1 1,689 03-09-2012, 03:16 AM
Last Post: AceInfinity
  HTML perl script AceInfinity 5 2,743 12-03-2011, 11:35 PM
Last Post: Closed Account

Forum Jump:


Users browsing this thread: 2 Guest(s)