Database Programming with Perl - Perl Training A · PDF file · 2008-06-16Exercises...

Click here to load reader

  • date post

    09-Mar-2018
  • Category

    Documents

  • view

    229
  • download

    4

Embed Size (px)

Transcript of Database Programming with Perl - Perl Training A · PDF file · 2008-06-16Exercises...

  • DatabaseProgramming with

    Perl

    #!/usr/bin/perluse strict; print "\nSaid the Llama to the Lama:\n";

    my (@Llama, %answers, @llama, @questions) = map{ print;$_ }();

    die "seeking a llama to share pipe!\n" unless $Llama[0]=~/Find/;

    print "\nSaid the Lama to the Llama:\n";

    my$karma=0 ;for ( @Llama ){chomp; ; chomp; s/Fi/Fou/;s///; ;/ /; my ( $lama, $llama ) = ($,$); s/ //g for( $lama , $llama); $|=0;push @questions,$llama unless $llama=~ /^$/ ; push @llama,# $lama unless $lama=~ /^$/} %answers = map { $_ , 0, scalar reverse( split // ) ,0 }@questions;my @karma=llama( @llama);my @dharma = llama(map { ( ) x $_ . $llama[$_]. ( ) x (1* $#llama $_)}(0..$#llama)); my @lama=map{$a = $_;;;;; s/( *)(\w+)( *)/$3$2$1/; $a}@dharma; my %find = (lama => \@llama,dharma =>\@karma,karma =>\@dharma ,llama=> \@lama); sub llama{map{$|++; my $grazing= $_ ;join( , map{ ( split //,$_[ $_ ]) [$grazing] } (0..$#_ ))}(0.. ( length($_[0])1))}sub dharma{my @llama = @{$_[0]} ;[map{s/[az]/\*/g;; ; $_}@llama ] };; my %llama= map{$_,dharma($find{$_}) } ( keys %find );; sub karma { my $reasons=shift ;; ; ; my @answers = @{ $find{ "$reasons" } };; for my $questions (keys%answers){next if $answers{ $questions }; study $questions;for (0.. $#answers ) { if ( $answers[ $_]=~/$questions/){my($soul ,$seeks , $to_know )=($,$ ,$&) ;s/\w/\*/g for ($seeks, $to_know, $soul);$llama{ $reasons} >[$_]=$soul . $questions .$seeks;$karma ++;$answers{ $questions} ++ ; $answers{ reverse( split//,$to_know)}++ }}}};for(keys %find){ karma($_ )};my@dali =map{$a= $_;$a=~s/./\*/g;$a} (@llama); for my $karma(keys %llama){ $llama {$karma}=[ llama( @{$llama{$karma}})] if $karma=~/.{5}/ ;for(0 .. $#{$llama{$karma}}){ $llama{$karma}>[$_] =~s/ //g;my@Lama =split//, $dali[$_];my @lAma= split//,$llama{$karma}>[$_];$dali[$_]= join(,map{($lAma[$_] ne *) ? $lAma[$_]:$Lama[$_]}(0..$#lAma));}}my @answer=map { s/(.)/$1 /g ;s/\*/ /g; ($_, x(length($_)))}(@dali);my($llama,$lama)=sort($#questions, $#answer) ; for (0..$lama) {print $answer[$_]," ",$questions[$_],"\n" }

    Jacinta RichardsonPaul Fenwick

  • Database Programming with Perlby Jacinta Richardson and Paul Fenwick

    Copyright 2005-2008 Jacinta RichardsonCopyright 2005-2008 Paul FenwickCopyright 2005-2008 Perl Training Australia

    Cover artwork Copyright(c) 2001 by Frank Booth. Used with permission.

    Conventions used throughout this text are based upon the conventions used in the Netizen training manuals by Kirrily Robert, and found at

    http://sourceforge.net/projects/spork

    Distribution of this work is prohibited unless prior permission is obtained from the copyright holder.

    This training manual is maintained by Perl Training Australia. Information about Perl Training Australias courses, manuals and course

    extracts can be found at http://www.perltraining.com.au/.

    This is revision 1.8 of the Perl Training Australias "Database Programming with Perl" training manual.

  • Table of Contents1. About Perl Training Australia ....................................................................................................... 1

    Training ....................................................................................................................................... 1Consulting ................................................................................................................................... 1Contact us.................................................................................................................................... 1

    2. Introduction..................................................................................................................................... 3

    Course outline ............................................................................................................................. 3Assumed knowledge ................................................................................................................... 3Module objectives ....................................................................................................................... 3Platform and version details........................................................................................................ 3The course notes.......................................................................................................................... 4Other materials ............................................................................................................................ 4

    3. Our database ................................................................................................................................... 7

    Database schema ......................................................................................................................... 7

    4. Accessing the database ................................................................................................................... 9

    In this chapter... ........................................................................................................................... 9Our training database .................................................................................................................. 9The MySQL shell........................................................................................................................ 9

    MySQL commands.......................................................................................................... 10Exercises ................................................................................................................................... 10Accessing the database from Perl ............................................................................................. 11

    Using the database shell directly ..................................................................................... 11Using a database specific module.................................................................................... 11

    SQL injection attacks............................................................................................. 12Group Exercises ..................................................................................................... 13

    Using DBI and derivatives............................................................................................... 13Introduction to DBI ................................................................................................................... 13

    What is DBI? ................................................................................................................... 14Supported databases............................................................................................... 14

    Why use DBI? ................................................................................................................. 15Chapter summary ...................................................................................................................... 15

    5. Programming with the DBI ......................................................................................................... 17

    In this chapter... ......................................................................................................................... 17Establishing a database connection........................................................................................... 17

    Testing that the connection worked ................................................................................. 18Exercise.................................................................................................................. 19

    Testing the connection later............................................................................................. 19Simple connection flags................................................................................................... 19

    DELETE, INSERT, UPDATE ............................................................................................................ 20Exercises.......................................................................................................................... 21Inserting, updating, deleting based on external input...................................................... 21

    Placeholders and bind values ................................................................................. 22Prepare and execute................................................................................................................... 22

    prepare ............................................................................................................................. 23execute.........................................