Hatena::ブログ(Diary)

satoru.netの自由帳 このページをアンテナに追加 RSSフィード

180921Friday

[]perlからphpのsession read

いまさらperlつかってるひとなんて化石なんだろうなあとおもいつつーperlだいすき


https://www.h-fj.com/blog/archives/2013/02/27-152952.php

use Data::Dumper

use CGI::Cookie;

use PHP::Serialization qw( unserialize );

my %ck = fetch CGI::Cookie;;

my $data = $memd->get($ck{PHPSESSID}->value);

$data =~ s/^[^\|]+\|//;

my $ref = PHP::Serialization::unserialize($data);

print Dumper($ref);

180626Tueday

when https disabled memo

yum -y install perl-Net-SSLeay perl-Crypt-SSLeay perl-IO-Socket-SSL
cpanm LWP::Protocol::https

180114Sunday

並列処理 Parallel::ForkManager @ perl

  • 時間のかかる処理を{}で括って$pm->start and next;と$pm->finish;で待機させる
  • count1は10秒で終わる、count2は5秒で終わる処理を並列処理するサンプル
#!/usr/bin/perl

use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(2);

## 処理1
{
	$pm->start and next;
	my $count1 = 0;
	while(1){
		if($count1++ == 10){
			print "count1 finish!\n";
			last;
		}
		print "count1 $count1\n";
		sleep 1;
	}
	$pm->finish;
}

## 処理2
{
	$pm->start and next;
	my $count2 = 0;
	while(1){
		if($count2++ == 5){
			print "count2 finish!\n";
			last;
		}

		print "count2 $count2\n";
		sleep 1;
	}
	$pm->finish;
}

$pm->wait_all_children;

print "ALL FINISH $count1 $count2\n";

結果

count1 1
count2 1
count1 2
count2 2
count1 3
count2 3
count1 4
count2 4
count1 5
count2 5
count1 6
count2 finish!
count1 7
count1 8
count1 9
count1 10
count1 finish!
ALL FINISH

180112Friday

coincheck-API perl

#!/usr/bin/perl

use HTTP::Tiny;
use Digest::SHA qw/hmac_sha256_hex/;
use strict;

my $ACCESS_KEY = ACCESS_KEY;
my $SECRET_KEY = SECRET_KEY;

my $dt = time*10000;
my $url = "https://coincheck.com/api/ticker";
my $signature = hmac_sha256_hex($dt . $url ,$SECRET_KEY);
my $tiny = new HTTP::Tiny(
	default_headers => {
		"ACCESS-KEY" => $ACCESS_KEY,
		"ACCESS-NONCE"=> $dt,
		"ACCESS-SIGNATURE"=> $signature,
	}
);

my $res = $tiny->get("$url");
print $res->{content};

160525Wedday

memo

#!/usr/bin/perl

use re 'eval';

my $key = "192.168.[0-100].*";
my $re = make_re($key);

print "[$re]\n";

my @lines = grep {$_} split /\n/,qq{
192.168.99.112 - - [10/Jul/2013:13:53:15 +0900] "GET /top.html HTTP/1.1" 404 1426 "http://facebook.com" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
192.168.81.20 - - [10/Jul/2013:15:06:33 +0900] "GET /hogehoge.html HTTP/1.1" 404 4374 "http://paiza.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
192.168.223.58 - - [10/Jul/2013:21:32:01 +0900] "GET /hoge.html HTTP/1.1" 304 6601 "http://paiza.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0"
};

for(@lines){
	if(m@^$re@){
		s@\[|\]@@gim;
		my($ip,$date,$file) = (split / /)[0,3,6];
		print "$ip $date $file\n";
	}
}

sub make_re{

	my $pattern = shift;

	my @re;
	my $count = 1;

	for my $n ($pattern =~ m@([^\.]+)@g){
		if($n =~ m@\[@){ # レンジ指定
			my @n = $n =~ m@(\d+)@gim;
			push @re,sprintf '(\d+)(?(?{range($%s,%s,%s)})|(?!))',$count,$n[0],$n[1];
			$count++;
		} elsif($n eq "*") { #アスタリスク
			push @re,'\d+';
		} else { #数値
			push @re,"$n";
		}
	}
	return join('\.',@re);
}

sub range{
	my ($n,$min,$max) = @_;
	return ($n >= $min && $n <= $max)
}

©satoru.net
Mail Twitter