(\d+\.\d+\.\d+\.\d+)<\/div>/;
my $ip = $1;
say $ip;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit
/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36');
$d = $ua->get("https://www.esolutions.se/whatsmyinfo");
$d->decoded_content =~ /
(\d+\.\d+\.\d+\.\d+)<\/div>/;
$ip = $1;
say $ip;
```
---
youtube.pl
==========
```perl
#!/usr/bin/perl
use 5.014;
no warnings;
use WWW::Mechanize;
use Getopt::Std;
our $opt_n;
getopts('n:');
my $keywords = join("+", @ARGV);
my $limit = $opt_n // 6;
if ($keywords eq "") {
say <new();
# youtube query URL
my $url = "http://www.youtube.com/results?hl=en&search_query=$keywords";
say "try to search for $limit results ...\n" . $url . "\n\n";
$mech->get( $url );
# http://www.youtube.com/watch?v=XXXXXXXXXXX
my $ref = $mech->find_all_links( url_regex => qr/watch\?v=/i );
# for all valid video links
for (@$ref) {
if ($_->url() =~ /watch\?v=.{11}/ and $_->text() !~ /Watch Later/) {
say $_->url_abs();
say $_->text();
$limit--;
}
last if not $limit;
}
```
---
xferlog parser
==============
```perl
#!/usr/bin/perl
#
#
#Dec 21 17:07:08 nat235 pure-ftpd: (?@192.168.0.15) [INFO] ioi32 is now logged in
use 5.014;
system('sudo cat /var/log/xferlog | grep "logged" | grep "Dec 22" > log1');
my %table = ();
# record src IP
open F, "<", "log1";
while () {
my @line = split;
$table{$line[7]} //= [];
if (not $line[5] ~~ @{$table{$line[7]}}) {
push $table{$line[7]}, $line[5];
}
}
close F;
for (sort keys %table) {
say "$_ @{$table{$_}}";
}
```
---
socket programming
==================
- server
```perl
#!/usr/bin/perl
use 5.014;
use IO::Socket;
my $server = "127.0.0.1";
my $sock = new IO::Socket::INET ( LocalHost => $server, LocalPort => 6667,
Proto => 'tcp', Listen => 5, Reuse => 1)
or die "ERROR in Socket Creation : $!\n";
# accept a connection from client
while (my $client = $sock->accept()) {
$client->autoflush(1);
say "accept a connetion!";
while (<$client>) {
print $client "--> $_";
print "--> $_";
}
$client->close;
}
$sock->close;
```
socket programming
==================
- client
```perl
#!/usr/bin/perl
use 5.014;
use IO::Socket;
my $server = "127.0.0.1";
# connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp',
Blocking => 1) or die "Can't connect\n";
print $sock "blah\n";
while (<$sock>) {
print $sock "c --> $_";
print "c --> $_";
}
$sock->close;
```
---
class: center, middle, inverse
Any questions?
--------------
---
name: inverse
class: center, middle, inverse
Thanks
======
---
Reference
=========
- [perldoc](http://perldoc.perl.org/)
- [Perl Maven](http://perlmaven.com/)
---
Reading
======
- [Learning Perl](http://shop.oreilly.com/product/0636920018452.do)
- [Intermediate Perl](http://shop.oreilly.com/product/0636920012689.do)
- [Perl Best Practices](http://shop.oreilly.com/product/9780596001735.do)
- [Programming Perl](http://shop.oreilly.com/product/9780596004927.do)
- [Advanced Perl Programming](http://shop.oreilly.com/product/9780596004569.do)
- [Mastering Perl](http://shop.oreilly.com/product/0636920012702.do)
- [Perl Hacks](http://shop.oreilly.com/product/9780596526740.do)
- [Perl Cookbook](http://shop.oreilly.com/product/9780596003135.do)