ä½ãã®æåã«ãã®è¨äºãè¦ããã¦ãã¾ããè¡æãåããã
2006-11-07 - 兼雑記
Perlã£ã¦ãããªå¤æ
ããã°ã©ã ãæ¸ãã¦ãã¾ãã®ãâ¦ï¼ï¼ã¨ã
è¨å·ã ãã§ããã°ã©ã ãæ¸ãã¦ãã¾ãã¨ã¯å
¨ç¶ç¥ããªãã£ãã
ä½åº¦ãèªã¿è¿ãã¦ããããç解ããç¯å²ã§ã¯ã
- æ¡å¼µæ£è¦è¡¨ç¾ã®æ§æãå©ç¨ãã¦ä»»æã®ã³ã¼ããå®è¡ããã
- æååã¯æååå士ã®æä»çè«çåãã¨ããã¨ã§çæã§ãã
ã¨ãããã¨ãããã
ãªã®ã§ããã©ã®æåã¨ã©ã®æåã®æä»çè«çåãã©ã®æåã«ãªããããææ¡ãã¦ããã°ãããããæååãçæãããã¨ãã§ããããã§ãèªåã§ãããããè¨å·ããã°ã©ã ãæ¸ããã®ã§ã¯ãªããï¼ã¨æã£ã¦ãä½ã£ã¦ã¿ãã
ä»»æã®ã¯ã³ã©ã¤ãã¼ãè¨å·ã§è¡¨ç¾ããªããããã®ã¹ã¯ãªããã(2/8 ã¡ãã£ã¨ä¿®æ£)
#!/usr/bin/perl use strict; use warnings; # å¤æè¾æ¸ my $dict = &create_dictionary(); # ã¯ã³ã©ã¤ãã¼æåå my $str = shift; # ä½æä¸å¯è½ãªæå my $impossibles = q|()*+,-./0-9:|; # ã¯ã³ã©ã¤ãã¼ãè¨å·åå¯è½ãªæåããå§ã¾ããã©ããå¤å® my $flg = $str =~ /^[$impossibles]/ ? 1 : 0; my $output = '.'; while ($str) { my $pattern; # æååãæãåºã if ($flg) { $str =~ /[$impossibles]+/; $pattern = $&; # ä½æä¸å¯è½ãªæååã¯""ã§å²ãã ã $output .= qq|"$&".|; } else { $str =~ /[^$impossibles]+/; $pattern = $&; # è¨å·åå¯è½ãªæååã¯è¨å·åãã $output .= &symbolization($&) . '.'; } # ãã©ã°ã®åãæ¿ã $flg = !$flg; # åãåºãããã¿ã¼ã³ãæ¶å» $str = substr($str, length($pattern)); } # æçµåºå print qq|perl -e '""!~("(?{"$output"})")'\n|; # è¾æ¸ã®ä½æ sub create_dictionary { # ã¯ã³ã©ã¤ãã¼ã«ä½¿ãæåã®ã¿ãåãåºã my @char = grep { /[^\da-zA-Z\\'"]/ } map { chr } (0x21..0x7E); my $dict = {}; for my $lhs (@char) { for my $rhs (@char) { my $xor = ($lhs ^ $rhs); my $ord = ord $xor; # 表示å¯è½ãªæåã®å ´åã®ã¿ if ($ord >= 0x20 and $ord <= 0x7E) { my $array = $dict->{$xor}; if (defined $array) { push @$array, [$lhs, $rhs]; } else { $dict->{$xor} = [[$lhs, $rhs]]; } } } } return $dict; } # æååã®è¨å·å sub symbolization { my $arg = shift or die $!; my ($lhs, $rhs) = ('', ''); # å¼æ°ãä¸æåãã¤åãåºãã¦å¤æ for (0..length($arg) - 1) { my $char = substr($arg, $_, 1); my $next = substr($arg, $_ + 1, 1); if ($dict->{$char}) { my $array = $dict->{$char}; my ($lappend, $rappend); LOOP: while (1) { my $set = $array->[rand @$array]; $lappend = $set->[0]; $rappend = $set->[1]; # '@_', '$_', '@$' ãªã©ãå«ã¾ãã¦ãã㨠# ""å ã§å±éãããã¨ãã¦ãã¾ãã®ã§ããç´ã # '$'ã¯å¾ã§ã¾ã¨ãã¦ã¨ã¹ã±ã¼ããã for my $word (qw/@_ @+ @- @: @$ @{/) { next LOOP if index($lhs.$lappend, $word) + 1; next LOOP if index($rhs.$rappend, $word) + 1; } # '$'ã®å¤æ㯠("_"^"{") ãããªãã®ã§ # '@'ã®å¾ã«ããã¨ç¡éã«ã¼ãã«ãªã£ã¦ãã¾ã if (($lappend eq '@' || $rappend eq '@') and $next eq '$') { next LOOP; } last; } $lhs .= $lappend; $rhs .= $rappend; } } # '$'ã®ã¨ã¹ã±ã¼ã $lhs =~ s|\$|\\\$|g; $rhs =~ s|\$|\\\$|g; return qq|("$lhs"^"$rhs")|; }
æ£è¦è¡¨ç¾ãã¤ãã¤ã使ãããªãããããªãè¦å´ãã¦ããâ¦orz
ãã¡ãããããã使ãã°ãã¯ã³ã©ã¤ãã¼ãè¨å·åã§ãããã¯ãã
ä¾ï¼ï¼Hello world
$ ./symbolize.pl 'print "Hello world!\n"' perl -e '""!~("(?{".("+,@.)]}(>,,/^_@/@?|!@^"^"[^)@]}_`[@@@~(/],[]}.|")."})")'
â ã³ãã
$ perl -e '""!~("(?{".("+,@.)]}(>,,/^_@/@?|!@^"^"[^)@]}_`[@@@~(/],[]}.|")."})")' Hello world!
ä¾ï¼ï¼ã¢ã¸ã¥ã¼ã«ã®ãã¼ãã ã£ã¦ã§ããã
$ ./symbolize.pl 'use Data::Dumper; print Dumper { hoge => foo, fuga => bar }' perl -e '""!~("(?{".("+_>@~>]<"^"^,[`:_)]")."::".("?_--%[[`-_@@*~?[--:]^_{@@<%}}~}&@@"^"{*@]@)`@]-).^^{.@]_/~\$[(/[@]@@]@//").",".("@=.<:`}@[]^,@#"^"`[[[[@@~{??^`^")."})")'
â ã³ãã
$ perl -e '""!~("(?{".("+_>@~>]<"^"^,[`:_)]")."::".("?_--%[[`-_@@*~?[--:]^_{@@<%}}~}&@@"^"{*@]@)`@]-).^^{.@]_/~\$[(/[@]@@]@//").",".("@=.<:`}@[]^,@#"^"`[[[[@@~{??^`^")."})")' $VAR1 = { 'fuga' => 'bar', 'hoge' => 'foo' };
ã¨ã
ãã ãããã®ã¹ã¯ãªããã§ä½æãã¦ããè¾æ¸ãDumpãã¦ã¿ãã°åããéãã'()*+,-./0123456789:'ã®æåã¯è¨å·å士ã®æä»çè«çåã§ã¯ä½æãããã¨ãåºæ¥ãªãã
ã¾ã'+-*/'ã¨ãã¯ãã®ã¾ã¾åºåããã°ãããã ãã©ãæ°åãä½ããªãã®ã«ã¯å°ã£ã¦ãã¾ãã
æä»çè«çåãããªãã¦è«çç©ã使ãã°8ã¨9ã ãã¯ä½ããããã«ãªããã©ãããã¯ããã§ä»ã®ã¢ã«ãã¡ããããªã©ãä½ããªããªã£ã¦ãã¾ããã
ãã¾ã®ã¨ãããã®ã¹ã¯ãªããã§ã¯ããã¯æªè§£æ±ºãªã®ã§æ°åã¯ãã®ã¾ã¾ä½¿ããã¦ãã¾ãã
ä¾ï¼ããããã¯ã³ã©ã¤ãã¼
$ perl ./symbolize.pl 'print int(rand 6) + 1, "\n"' perl -e '""!~("(?{".("],@@]^@.^"^"-^).)~)@*")."(".("/!@@^"^"]@.\$~")."6)".("^"^"~")."+".("@"^"`")."1,".("}^}@}"^"]|!._")."})")'
6ã¨1ããã®ã¾ã¾ä½¿ããã¦ãã¾ãâ¦
ãã®å ´åã©ãããã°ãããã ããã
ã¨ããããå
¥åã®ã¯ã³ã©ã¤ãã¼å´ã§æ°åã使ããªãããã«ããããã
$ perl ./symbolize.pl 'print int(rand length $~) + !$[, "\n"' perl -e '""!~("(?{".("-^@@)~@.]"^"],).]^)@)")."(".("__@?@@>.<]({_#"^"->.[`,[@[)@[{]").")".("{"^"[")."+".("}~_;"^"]_{`").",".("[^!.|"^"{|}@^")."})")'
ããã©ã«ãå¤ã$~ = 'STDOUT', $[ = 0 ãªã®ãå©ç¨ãã¦æååã®é·ããè«çå¦å®ã§æ°åãä½ãèæããã
0 = $[ 1 = !$[ 2 = !$[<<!$[
ã¨ããã«ã³ã¸ã§ç½®ãæãã¦ãããã¨ããã®ãä»ã®ã¨ããæãã¤ãæ¹æ³ã
ä»ã«ä½ãè¯ãæã¯ãªãããªãã