
Database Programming with Perl #!/usr/bin/perl use 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/[a−z]/\*/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 Richardson Paul Fenwick Database Programming with Perl by Jacinta Richardson and Paul Fenwick Copyright © 2005-2008 Jacinta Richardson Copyright © 2005-2008 Paul Fenwick Copyright © 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 Australia’s courses, manuals and course extracts can be found at http://www.perltraining.com.au/. This is revision 1.8 of the Perl Training Australia’s "Database Programming with Perl" training manual. Table of Contents 1. About Perl Training Australia....................................................................................................... 1 Training....................................................................................................................................... 1 Consulting ................................................................................................................................... 1 Contact us.................................................................................................................................... 1 2. Introduction..................................................................................................................................... 3 Course outline ............................................................................................................................. 3 Assumed knowledge ................................................................................................................... 3 Module objectives ....................................................................................................................... 3 Platform and version details........................................................................................................ 3 The course notes.......................................................................................................................... 4 Other materials............................................................................................................................ 4 3. Our database................................................................................................................................... 7 Database schema......................................................................................................................... 7 4. Accessing the database ................................................................................................................... 9 In this chapter.............................................................................................................................. 9 Our training database .................................................................................................................. 9 The MySQL shell........................................................................................................................ 9 MySQL commands..........................................................................................................10 Exercises ...................................................................................................................................10 Accessing the database from Perl .............................................................................................11 Using the database shell directly .....................................................................................11 Using a database specific module....................................................................................11 SQL injection attacks.............................................................................................12 Group Exercises.....................................................................................................13 Using DBI and derivatives...............................................................................................13 Introduction to DBI...................................................................................................................13 What is DBI? ...................................................................................................................14 Supported databases...............................................................................................14 Why use DBI? .................................................................................................................15 Chapter summary ......................................................................................................................15 5. Programming with the DBI .........................................................................................................17 In this chapter............................................................................................................................17 Establishing a database connection...........................................................................................17 Testing that the connection worked.................................................................................18 Exercise..................................................................................................................19 Testing the connection later.............................................................................................19 Simple connection flags...................................................................................................19 DELETE, INSERT, UPDATE ............................................................................................................20 Exercises..........................................................................................................................21 Inserting, updating, deleting based on external input......................................................21 Placeholders and bind values .................................................................................22 Prepare and execute...................................................................................................................22 prepare.............................................................................................................................23 execute.............................................................................................................................23 dump_results....................................................................................................................24 Exercise ...........................................................................................................................24 do vs prepare ..................................................................................................................24 Statement handles............................................................................................................24 Perl Training Australia (http://perltraining.com.au/) iii Fetching data.............................................................................................................................25 How many rows? .............................................................................................................25
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages140 Page
-
File Size-