Docstoc

Perl Lecture _1

Document Sample
Perl Lecture _1 Powered By Docstoc
					Perl Lecture #1

Scripting Languages
      Fall 2004
                     Perl
• Practical Extraction and Report Language

• -created by Larry Wall -- mid – 1980’s
  – needed a quick language
  – didn’t want to resort to C
  – derivative of sed or awk (interpreted language
    used on Unix / sed – stream editor.
            Perl Intro Cont’d
• fills the gap between C and awk
• very powerful language / easy to learn
• used to write small scripting programs as well as
  larger applications
• for the web has been used –cgi scripts – run
  forms etc.
• also web apps – shopping cart applications
• makes use of regular expressions – powerful
  sequence of characters
              Why Use?
• Perl is free
• many Perl ide’s are free – works great
  integrated in a Unix environment as most
  version come with Perl / Mod-Perl and
  Emb Perl
• Works well in a Windows environment as
  well
• CPAN – comprehensive Perl Archive
  Network
#!/usr/bin/perl
#Author: Lori N
#Description: First program
#Date: Today’s Date
$string="Top 10";
$number=10.0;
print "Number is 10.0 and string is 'Top 10'\n\n";
$add = $number + $string;
print "Adding a number and a string: $add\n";
$concatenate = $number . $string;
print "Concatenating a number and a string: $concatenate \n";
$add2 = $concatenate + $add;
print "Adding the previous two results: $add2 \n\n";
$undefAdd = 10 + $undefNumber;
print "Adding 10 to an undefined variable: $undefAdd\n";
print "Printing an undefined variable: $undefVariable(end)\n";
                   $Scalar
• When we have just one of something we
  have a scalar
  – simplest kind of data that Perl Manipulates.
  – either a number or a string of characters
  – Perl uses them interchangeably
  – no need to declare a variable
  – Perl will figure it out by its usage
                     Numbers
• int and floating pt numbers
• Perl computes with double-precision fp values
• Literal
  – is not a result of calculation or I/O op – data written
    directly into the source code
  –0
  – 2001
  – -4
  – also use Octal ( base 8 ) , hexadecimal ( base 16)
                  Strings
• seq of characters
• they have a literal representation – ‘single
  quoted’ and “double quoted”
• Single quoted Literals
     - ‘string’
     - ‘string\’s’
     - ‘hello\n’ – no newline
      Double Quoted Strings
- Double quoted Literals

 --“string”
 --“string\n” – newline
 --“string \””
            String operators:
• “hello” . “world” = helloworld
• “hello” . ‘ ‘ . “world” = hello world
String repetition operator
  – x – takes its left operand ( a string ) and
    makes as many concatenated copies as you
    specify
  – “string” x 3 -- stringstringstring
  – 5 x 4 -- 5555
      Automatic Conversions
• Perl automatically performs conversions
  between Numbers and Strings.

  – by the operator used or they way you attempt
    to use them in your script
  -- be careful this might not work out
 logically like you’d like it to
                 Warnings
• Perl’s Built in Warnings.
  – Command line – perl –w myfile.pl
  – Or add it to your code #!/usr/bin/perl –w

  – use man perldiag to see more useful
    troubleshooting flags
  – also see man perllexwarn man page for
    warnings that can be turned on and off.
           Scalar Variables
• variable – all should be familiar – they hold
  values
• a scalar variable holds a single scalar
  value
• they all begin with $Perl_identifier
• can’t start with a digit
• they are also referenced with the leading $
          Scalar Assignment
• --assignment 
•    --$income = ‘tolittle’;
•    --$tax_amount = 1000;
•    --$miles = 100;
•    --$distance = $miles * 5;
• Similar binary operators as C
• --+= , *= , .= (string concatenator)
                     Output
•   --print “Hello World\n”;
•   --print ( )
•   --in a series separated by comma’s
•   --print “My income is “, 0 * 10000 , “.or
    null\n”;
  Interpolation of Scalar variables
             into Strings
• $income = “not much”;
• $expenses = “quite a bit”;
• $lifesavings = “My income is $income but
  my expenditures are $expenses”;
• or $lifesavings = ‘My income is ‘ . $income
  . ‘but my expenditures are ‘ . $expenses;
• Book has table on page 32 – Operator
  Precedence and Associativity
       Comparison Operators
• < <= == >= > !=
• Strings – eq , ne , lt , gt , le , ge
         if Control Structure
• if ( $variable <= $anothervariable) {
•      Print this;
• }
• Curly braces are required
            No Boolean Data Type
• No Boolean data type – used simple rules:
• --the undef value
•      --what if you use a scalar value before you give it a value?
•      --Perl gives it a undef value – neither a string or a number
•      --acts like zero – or an empty string
•
• --for Boolean process uses simple rules
•      --undef is false
•      --Zero is false – all else true
•      --empty string ‘ ‘ is false – all else true
•      --The one exception – since numbers and strings are
  equivalent, the string form of zero, ‘0’ has the same value as its
  numeric form – false
                     User Input
• --line-input operator <STDIN>
• --Perl reads the next complete line of text from standard
  input ( up to the first newline)
• --uses it as the value of <STDIN>
• --its string value has a newline character on the end of it
  :
•       $line = <STDIN>
•       if ($line eq “\n”) {
•                print “That was just a blank line!\n”;
•       }else {
•                print “That line of input was : $line”;
•       }
              Chomp Operator
•   --works on a variable
•   --variable has to hold a string
•   --if the string ends in a newline – it removes it
•   --take input from <STDIN> -- chomp removes \n and
•   provides us with just the string.
•   One step:
•         chomp($variable = <STDIN>)
•   --chomps return value is the number of characters
    removed – 1
     while Control Structure
• --same as C++
               Tutorial:
         Simple Perl program:
#!/usr/bin/perl –w
#
#Name: Add Name
#Date: Today’s Date
#Description: first.pl Ask and Display name
print “Please enter you name “;
$name = <STDIN>;
chomp ($name);
print “Your name is $name”;
         Execute Your Code
• Has to be executable:

• chmod 755 first.pl

• To run two ways:
• perl first.pl
• or ./first.pl

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:2
posted:7/28/2012
language:
pages:24