一行野郎(one-liner)はperlにおまかせ

序文


  dankogai   21使


perl
% cat hello.pl
print "Hello, world!\n";
% perl hello.pl
Hello, world!
% 

-e使
% perl -e'print "Hello, World!\n"'
Hello, world!
% 

perl
% perl
print 1+1, "\n";
[ctrl-D]
2
% 

ruby


PerlRubyputs-lprintputs
% perl -e'print "Hello, World!"'
Hello, World! %
% perl -le'print "Hello, World!"'
Hello, World!
% 

-lprintchomp

5.10.0 say使-e-E
% perl -e'say "Hello, World!"'
String found where operator expected at -e line 1, near "say "Hello, World!""
        (Do you need to predeclare say?)
syntax error at -e line 1, near "say "Hello, World!""
Execution of -e aborted due to compilation errors.
% perl -E'say "Hello, World!"'
Hello, World!

-MModule


使-e'use Module; ...'-MModule

http://www.dan.co.jp/(LWP)
% perl -MLWP::Simple -le'print get shift' http://www.dan.co.jp/

-MO=Deparse


-MO=Deparseperl使
% perl -MO=Deparse -le'print "Hello, World!"'
BEGIN { $/ = "\n"; $\ = "\n"; }
print 'Hello, World!';
-e syntax OK
% 

-n-pprint


使便-n-p-l使

: 
% perl -nle'print "$.:$_"' script.pl
1:while(<>){
2:    print "$.:$_"
3:}

findperl使


404 Blog Not Found:perl - the best friend of find(1)


ruby

-i


-i
% perl -i -ple's/\r\n/\n/g' *.txt

DOS(\r\n)Unix(\n)

-i.ext
% perl -i.bak -ple's/\r\n/\n/g' *.txt

.txt.bak-i.bak
% find . -type f -name \*.bak | perl -nle'$o=$_;s/\.bak$//;rename $o,$_'


-aawk


-n-p-aAWK$1, $2awkPerlregexp使@F

: PID
% ps aux | perl -anle'print $F[1]'

ruby(ruby$F)

Unicode使-CIO


Perl 5.8.1Perl 5.8UTF-8STDINSTDOUTbinmode STDIN, ':utf8';-CIO


% perl -MHTML::Entities -ple'$_=encode_entities($_)'
Dan Kogai (小飼弾)
Dan Kogai (&aring;&deg;&#143;&eacute;&pound;&frac14;&aring;&frac14;&frac34;)
% perl -CIO -MHTML::Entities -ple'$_=encode_entities($_)'
Dan Kogai (小飼弾)
Dan Kogai (&#x5C0F;&#x98FC;&#x5F3E;)

OS XTerminal.appUTF-8


perlshellhistoryone-liner
% echo $SHELL
/bin/tcsh
% echo histdup
erase
% echo $savehist
1024
% history | perl -nle'/perl/ and $p++;END{print $p}'
257

1/4

id:mala

Dan the One-Liner Monger

See Also:

Back