Download presentation
Presentation is loading. Please wait.
1
Advanced Topics Web Programming
2
Reference/Pointer A reference Syntax
a pointer to something (e.g. array, hash) a variable that refer to other variables contain a memory address of data good for working with large data structures creating complex data structures Syntax to create a reference $ptr1= \$scalar; $ptr2= $ptr3= \%hash; $ptr4= [1, 2, 3]; $ptr5= { k1=>v1, k2=>v2} to get the value Variable (i.e. scalar, array, hash) $$ptr1; @$ptr2; %$ptr3; Element of array/hash $$ptr2[0]; $$ptr3{0}; $ptr2->[0]; $ptr3->{0}; Web Programming
3
Multi-dimensional Arrays
Using anonymous arrays $array = [ [id1,pw1], [id2,pw2], [id3,pw3] ]; for ($i=0; $i<=2; $i++) { for ($j=0; $j<=1; $j++) { print “$array -> [i][j]\n”; } Using array references @a1= (id3,pw3); @array = # array of array pointers for ($i=0; $i<=2; $i++) { $ptr= $array[$i]; print Examples: #1, #2, #3, #4, #5 Web Programming
4
Fetching Web pages with LWP
use LWP::Simple; $url= $body= get($url); print $body; Example script use LWP::UserAgent; $ua = LWP::UserAgent->new(); $resp= $ua->get($url); $content=$resp->content(); print $content; Example script Form CGI Web Programming
5
WWW-Mechanize Module a subclass of LWP::UserAgent Basics Create a mech
use WWW::Mechanize; my $mech = WWW::Mechanize->new( autocheck => 1 ); Fetch a page $mech->get( " ); print $mech->content; Fetch a page into a file $mech->get( " ":content_file" => “hello.htm" ); Get Links @links= $mech->links(); array of link object foreach { my($url,$text,$absurl)= ($link->url(), $link->text, $link->url_abs()); print “$url, $text, $absurl\n”; } Find Links $link= $mech->find_link(url_abs_regex=>qr|^ $page= $mech->follow_link(url_abs_regex=>qr|^ @pages= $mech->follow_all_links(url_abs_regex=>qr|^ Mechanized Spider Example: #1, #2, #3 Web Programming
6
Perl DBI Module Basics Connect to a database Execute SQL Retrieve data
use DBI; my $dbh = DBI->connect( DBI:Pg:dbname=$dbname; host=$host; $user, $pw ) || die “Can’t connect to DB: $DBI::errstr\n”; Execute SQL my $sql = “SELECT * FROM $table; my $sth = $dbh->prepare($sql); $sth->execute(); Retrieve data while $sth->fetchrow_array()) {; print join (“, } Disconnect from the database $dbh->disconnect(); Example Script: #1, #2 Perl DBI Manual Web Programming
7
Miscellaneous Features
require “mylib.pl” store and use common subroutines in mylib.pl looks for mylib.pl directory add “return 1” at the end of “mylib.pl” example: mylib.pl, calling script #1 perlcc prog.pl –o prog.out generates a compiled binary named “prog.out” Web Programming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.