20 modules i haven't yet talked about

78
20 modules I haven’t yet talked about Tatsuhiko Miyagawa Shibuya Perl Mongers / Six Apart

description

 

Transcript of 20 modules i haven't yet talked about

Page 1: 20 modules i haven't yet talked about

20 modules I haven’t yet talked about

Tatsuhiko MiyagawaShibuya Perl Mongers / Six Apart

Page 2: 20 modules i haven't yet talked about

20 modules I haven’t yet talked about

Tatsuhiko MiyagawaShibuya Perl Mongers / Six Apart

10

Page 3: 20 modules i haven't yet talked about

#1

Page 4: 20 modules i haven't yet talked about

YAPC and WiFi

Page 5: 20 modules i haven't yet talked about

HTTP::ProxyPAC use HTTP::ProxyPAC; use LWP::UserAgent;

my $ua = LWP::UserAgent->new; my $uri = URI->new("http://portal.titech.ac.jp/proxy.pac"); my $pac = HTTP::ProxyPAC->new($uri);

my $res = $pac->find_proxy("http://www.google.com/");

if ($res->direct) { # ... } elsif ($res->proxy) { $ua->proxy('http' => $res->proxy); }

Page 6: 20 modules i haven't yet talked about

use JavaScript::Runtime;

sub init { my($class, $code) = @_;

my $runtime = JavaScript::Runtime->new; my $context = $runtime->create_context();

for my $func (@HTTP::ProxyPAC::Functions::PACFunctions) { no strict 'refs'; $context->bind_function( name => $func, func => sub { &{"HTTP::ProxyPAC::Functions::$func"}(@_) }); }

$context->eval($code);

bless { context => $context }, $class;}

Page 7: 20 modules i haven't yet talked about

package HTTP::ProxyPAC::Functions;# stolen from HTTP::ProxyAutoConfig

sub isPlainHostName { my ($host) = @_;

return (($host =~ /\./) ? 0 : 1);}

sub dnsResolve { my ($host) = @_; return unless isResolvable($host); return inet_ntoa(inet_aton($host));}

sub myIpAddress { return inet_ntoa(inet_aton(hostname()));}

Page 8: 20 modules i haven't yet talked about

ENODEMOSpindermonkey (libjs) and OS X

hate each other :<

Page 9: 20 modules i haven't yet talked about

#2(speaking of JavaScript...)

Page 10: 20 modules i haven't yet talked about

pQueryWeb::Scraper’s friend

Page 11: 20 modules i haven't yet talked about

use pQuery::DOM;

my $v = pQuery::DOM->fromHTML('<div id="foo">bar</div>') ->getElementById('foo')->innerHTML;

Page 12: 20 modules i haven't yet talked about

use pQuery::DOM;no capitalization ‘pQuery::DOM’;

my $v = pQuery::DOM->fromHTML('<div id="foo">bar</div>') ->get_element_by_id('foo')->innerHTML;

Page 13: 20 modules i haven't yet talked about

#3(speaking of P...)

Page 14: 20 modules i haven't yet talked about

MigratingPHP apps to Perl

Page 15: 20 modules i haven't yet talked about

6 years ago...• I’ve got a call from our sales guy

• “We’re closing a deal to migrate PHP apps to Perl”

• “... while keeping PHP apps writing data and Perl for more views”

• and I was drunken

Page 16: 20 modules i haven't yet talked about

Crazy Idea:share PHP session with Perl

Page 17: 20 modules i haven't yet talked about

PHP sessions

• PHP data serialization

• /tmp/{session_id}

Page 18: 20 modules i haven't yet talked about

PHP::Session use PHP::Session;

my $session = PHP::Session->new($id);

# session id my $id = $session->id;

# get/set session data my $foo = $session->get('foo'); $session->set(bar => $bar);

Page 19: 20 modules i haven't yet talked about

I’ve never used it.

Page 20: 20 modules i haven't yet talked about
Page 21: 20 modules i haven't yet talked about

#4(speaking of “many people use it”)

Page 22: 20 modules i haven't yet talked about

Ruby on RailsActiveSupport

Page 23: 20 modules i haven't yet talked about

when = 60.days.agodatasize = 200.megabytes

Page 24: 20 modules i haven't yet talked about

autobox

Page 25: 20 modules i haven't yet talked about

use autobox; use autobox::DateTime::Duration;

# equivalent to DateTime::Duration->new(months => 1, days => 2); $duration = 1->month + 2->days;

# equivalent to DateTime->now->add(years => 2); $datetime = 2->years->from_now;

# equivalent to DateTime->now->add(months => 4, years => 5); $datetime = (4->months + 5->years)->from_now;

# equivalent to DateTime->now->subtract(days => 3); $datetime = 3->days->ago;

Page 26: 20 modules i haven't yet talked about

See also:autobox::Numeric::(Bytes|Time)

on CodeRepos (hirose31)

Page 27: 20 modules i haven't yet talked about

#5(speaking of numbers)

Page 28: 20 modules i haven't yet talked about

my $ttl = 608400; # 7 days

Page 29: 20 modules i haven't yet talked about

my $ttl = 608400; # 7 days604800

Page 30: 20 modules i haven't yet talked about

my $ttl = 24 * 60 * 60 * 7; # 7 days

Page 31: 20 modules i haven't yet talked about

use Time::Duration::Parse;

my $ttl = parse_duration “7 days”;

Page 32: 20 modules i haven't yet talked about

Useful to set values in .conf filesbecause it’s a string and readable

Page 33: 20 modules i haven't yet talked about

#6(speaking of strings...)

Page 34: 20 modules i haven't yet talked about

UnicodeDo you understandPerl & Unicode?

Page 35: 20 modules i haven't yet talked about

UnicodeDo you really understand

Perl & Unicode?

Page 36: 20 modules i haven't yet talked about

I guess not.

Page 37: 20 modules i haven't yet talked about

$x = “\x{5bae}”; # Unicode$y = “\xe5\xae\xae”; # UTF-8

$z = $x . $y;

Page 38: 20 modules i haven't yet talked about

$x = “\x{5bae}”; # Unicode$y = “\xe5\xae\xae”; # UTF-8

$z = $x . $y; \x{5bae}\x{e5}\x{ae}\x{ae}

Page 39: 20 modules i haven't yet talked about

Perl DTRTbut not DWIM

Page 40: 20 modules i haven't yet talked about

Fix It (correctly)use Encode;

$x = “\x{5bae}”; # Unicode$y = “\xe5\xae\xae”; # UTF-8

$z = $x . decode_utf8($y);

Page 41: 20 modules i haven't yet talked about

Fix It (evilly)use Encode::DoubleEncodedUTF8;

$x = “\x{5bae}”; # Unicode$y = “\xe5\xae\xae”; # UTF-8

$z = $x . $y;$z = decode(“utf-8-de”, $z);

Page 42: 20 modules i haven't yet talked about

my $latin1_as_utf8 = "[\xC2\xC3][\x80-\xBF]";

my $valid_utf8_regexp = <<'.' ; [\x{00}-\x{7f}] | [\x{c2}-\x{df}][\x{80}-\x{bf}] | \x{e0} [\x{a0}-\x{bf}][\x{80}-\x{bf}] | [\x{e1}-\x{ec}][\x{80}-\x{bf}][\x{80}-\x{bf}] | \x{ed} [\x{80}-\x{9f}][\x{80}-\x{bf}] | [\x{ee}-\x{ef}][\x{80}-\x{bf}][\x{80}-\x{bf}] | \x{f0} [\x{90}-\x{bf}][\x{80}-\x{bf}] | [\x{f1}-\x{f3}][\x{80}-\x{bf}][\x{80}-\x{bf}][\x{80}-\x{bf}] | \x{f4} [\x{80}-\x{8f}][\x{80}-\x{bf}][\x{80}-\x{bf}].

sub decode { my($obj, $buf, $chk) = @_;

$buf =~ s{((?:$latin1_as_utf8){2,3})}{ _check_utf8_bytes($1) }ego; $_[1] = '' if $chk; # this is what in-place edit means

Encode::decode_utf8($buf);}

sub _check_utf8_bytes { my $bytes = shift; my $copy = $bytes;

my $possible_utf8 = ''; while ($copy =~ s/^(.)(.)//) { $possible_utf8 .= chr( (ord($1) << 6 & 0xff) | ord($2) ) }

$possible_utf8 =~ /$valid_utf8_regexp/xo ? $possible_utf8 : $bytes;}

Page 43: 20 modules i haven't yet talked about

#7(speaking of UTF-8)

Page 44: 20 modules i haven't yet talked about
Page 46: 20 modules i haven't yet talked about

use utf8;use URI::Find;

my $text = <<TEXT;Japanese Wikipedia home page URL ishttp://ja.wikipedia.org/wiki/メインページTEXT

my $finder = URI::Find->new(\&cb);$finder->find(\$text);

sub cb { warn shift }

# http://ja.wikipedia.org/wiki/

Page 47: 20 modules i haven't yet talked about

use utf8;use URI::Find::UTF8;

my $text = <<TEXT;Japanese Wikipedia home page URL ishttp://ja.wikipedia.org/wiki/メインページTEXT

my $finder = URI::Find::UTF8->new(\&cb);$finder->find(\$text);

sub cb { my($uri, $orig) = @_;}# http://ja.wikipedia.org/wiki/メインページ

Page 48: 20 modules i haven't yet talked about

#8(speaking of Japanese...)

Page 49: 20 modules i haven't yet talked about

How to spell your namesin Japanese Passport

Page 50: 20 modules i haven't yet talked about

Hepburn Romanization

Page 51: 20 modules i haven't yet talked about

ミヤガワ タツヒコMIYAGAWA TATSUHIKO

Page 52: 20 modules i haven't yet talked about

カトウ シュウヘイKATOU SHUUHEIKATOH SYUHEIKATO SHUHEIKATOH SHUHEI

Page 53: 20 modules i haven't yet talked about
Page 54: 20 modules i haven't yet talked about

Katakana <-> Alphabet

• Lingua::JA::Romanize::Japanese

• Lingua::JA::Kana

• Text::Kakasi

• Lingua::JA::Romaji

Page 55: 20 modules i haven't yet talked about

Lingua::JA::Hepburn::Passport

use Lingua::JA::Hepburn::Passport;

my $hepburn = Lingua::JA::Hepburn::Passport->new;$hepburn->romanize("かとう しゅうへい");

# KATO SHUHEI

Page 56: 20 modules i haven't yet talked about

#9(speaking of Passport/ID ...)

Page 57: 20 modules i haven't yet talked about

Online ID/PW management

Page 58: 20 modules i haven't yet talked about

How to manage

• Password manager (IE/Firefox)?

• SSH Agent?

• Keychain?

• 1Password or KeePassX?

Page 59: 20 modules i haven't yet talked about

What about Perl?my $user = “miyagawa”;my $pass = “blahblah”;

$api->login($user, $pass);

Page 60: 20 modules i haven't yet talked about

LWP::UserAgent with Keychain

use LWP::UserAgent::Keychain;

my $ua = LWP::UserAgent::Keychain->new; $ua->get("http://proteceted.example.com/");

Page 61: 20 modules i haven't yet talked about

DEMO

Page 62: 20 modules i haven't yet talked about

#10(speaking of the Web...)

Page 63: 20 modules i haven't yet talked about

XML

Page 64: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><heroes>Larry & Schwern</heroes>

Page 65: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><heroes>Larry &amp; Schwern</heroes>

Page 66: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><shout>I &hearts; Perl</shout>

Page 67: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><shout>I &#x2665; Perl</shout>

Page 68: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><div><a href=”/search?q=YAPC&Asia”>link</a></div>

Page 69: 20 modules i haven't yet talked about

What’s wrong?

<?xml version=”1.0”?><div><a href=”/search?q=YAPC&amp;Asia”>link</a></div>

Page 70: 20 modules i haven't yet talked about

What’s wrong?<?xml version=”1.0”?><rss><channel><item><summary>YAPC rocks</summary><xhtml:body>YAPC <xhtml:strong>really</xhtml:strong> rocks.</xhtml:body></item></channel></rss>

Page 71: 20 modules i haven't yet talked about

What’s wrong?<?xml version=”1.0”?><rss xmlns:xhtml=”http://www.w3.org/1999/xhtml”><channel><item><summary>YAPC rocks</summary><xhtml:body>YAPC <xhtml:strong>really</xhtml:strong> rocks.</xhtml:body></item></channel></rss>

Page 72: 20 modules i haven't yet talked about

People are stupidCloud is full of crapSoftware can fix it

Page 73: 20 modules i haven't yet talked about

XML::Liberal

• Uses XML::LibXML diagnostics to automatically fixed the errors

• Badly named (“That’s NOT an XML you’re talking about”)

Page 74: 20 modules i haven't yet talked about

use XML::LibXML;my $parser = XML::LibXML->new;my $doc = $parse->parse_string($xml);

Page 75: 20 modules i haven't yet talked about

use XML::Liberal;my $parser = XML::Liberal->new(‘LibXML’);my $doc = $parse->parse_string($xml);

Page 76: 20 modules i haven't yet talked about

Bonus

•No performance penalty if the given XML is valid

Page 77: 20 modules i haven't yet talked about

That’s it!

• Go to search.cpan.org/~miyagawa for more!

• I want to hear your version of this talk if you have > 10 modules on CPAN

Page 78: 20 modules i haven't yet talked about

Thank you