|
Perl µð¹ö°Å
> perl -d Sample.pl
½ÇÇàÈÄ¿¡
s <-- script¸¦ ÇÑÁÙ¾¿ ½ÇÇà
p $a <-- $aÀÇ °ªÀ» Ç¥½ÃÇØÁØ´Ù.
q <-- µð¹ö°Å Á¾·á
h <--- help
#!/usr/bin/perl -w
-w Warning
################### ¿¬»êÀÚ ###################
.. : ¹üÀ§ ¿¬»êÀÚ
@a = (); # ArrayÀ» ºñ¿ò
$b = $#a ; # ÷ÀÚÀÇ ÃÖ´ë°ª
$b = @a ; ArrayÀÇ ¼ö
delete $a{"name"}; # Å° name¸¦ %a¿¡¼ »èÁ¦
\$a : $aÀÇ ¸Þ¸ð¸® ½ÇÁ¦ Address
\@a
\%a
3°¡ÁöÀÇ ½ÇÁ¦ Address´Â ´Ù ´Ù¸§
$a = 10;
$b = \$a;
$c = $$b; #c´Â a¿Í °°À½
$b = \@a;
$d = $b->[0];
*a : º¯¼öÀ̸§ÀÌ aÀÎ ¸ðµç °ÍÀ» ³ªÅ¸³¿
$a = [1,2,3]; #¹«¸íArray
$a = {"one" => "ÀÏ"}; #¹«¸í ÇؽÃ
@a = ([10,20],[30,40]); #$a[1][1]Àº 40ÀÓ
foreach $a(1,2,3) {..} # $a¿¡ ¸Å iteration ¸¶´Ù 1,2,3, ¼øÂ÷ÀûÀ¸·Î ÀúÀåµÊ
################### ·çÇÁÀÇ Áß´Ü ###################
last ; #ÇöÀç loop¸¦ Á¾·á
last LABEL ; #¶óº§ÀÌ ºÙÀº BlockÀ» Á¾·á
next ; #ÇöÀç loopÀÇ ½ÃÀÛÀ§Ä¡·Î Á¡ÇÁ
next LABEL ; #¶óº§ÀÌ ºÙÀº BlockÀÇ ½ÃÀÛ À§Ä¡·Î Á¡ÇÁ
redo ; #ÇöÀç loop BlockÀÇ Ã³À½À¸·Î µ¹¾Æ°¨
redo LABEL ; #¶óº§ÀÌ ºÙÀº Block¾ÈÀÇ Ã³À½À¸·Î Á¡ÇÁ
LABEL : while($a < 5) {
:
redo LABEL ;
:
} continue {...}
################### ¸ÞŸ¹®ÀÚ ###################
. : ÀÓÀÇÀÇ Çѹ®ÀÚ(Çà ¹Ù²ÞÀº Á¦¿Ü)
* : 0ȸ ÀÌ»ó ¹Ýº¹
+ : 1ȸ ÀÌ»ó ¹Ýº¹
? : 0ȸ ¶Ç´Â 1ȸ ¹Ýº¹
^ : ¼±µÎ
$ : ¸»¹Ì
| : ¼±ÅÃ
() : Á¤±ÔÇ¥Çö ±×·ì
[] : ¹®ÀÚ Å¬·¡½º
{n} : nȸ ¹Ýº¹
{n, } : nȸ ÀÌ»ó ¹Ýº¹
{n,m} : nȸ ÀÌ»ó mȸ ÀÌ»ó ¹Ýº¹
\ : ¸ÞŸ¹®ÀÚ¸¦ ¹®ÀÚ·Î Ãë±Þ
\w : ¿µ¹®ÀÚ, ¼ýÀÚ¿Í _
\W : ¿µ¹®ÀÚ, ¼ýÀÚ¿Í _ ÀÌ¿ÜÀÇ ¹®ÀÚ
\s : °ø¹é(¹Ý°¢ Space, ÅÇ, °³Çà)
\S : °ø¹éÀÌ¿Ü
\d : ¼ýÀÚ
\D : ¼ýÀÚ ÀÌ¿Ü
ÃÖÀå ¸ÅÄ¡, ÃÖ´Ü ¸ÅÄ¡
"abcabc" =~ /a.*c/ ; #abcabc ¸ÅÄ¡µÊ
"abcabc" =~ /a.*?c/ ; #abc ¸ÅÄ¡µÊ
ÆÐÅϸÅĪÀÇ °á°ú¸¦ ³ªÅ¸³»´Â Ư¼ö º¯¼ö
$& : ¸ÅÄ¡ÇÏ´Â String Àüü
$` : ¸ÅÄ¡ÇÏ´Â ºÎºÐº¸´Ù ¾Õ¿¡ ÀÖ´Â String
$' : ¸ÅÄ¡ÇÏ´Â ºÎºÐº¸´Ù µÚ¿¡ ÀÖ´Â String
$1, $2, ... : ù¹ø°, µÎ¹ø°, .. ±×·ì¿¡ ¸ÅÄ¡ÇÏ´Â String
$+ : ¸¶Áö¸· ±×·ì¿¡ ¸ÅÄ¡ÇÏ´Â String
¸ÅÄ¡¿¬»êÀÚ ¿É¼Ç $str =~ m/../../
g : ¸ÅÄ¡ÇÏ´Â °ÍÀ» ¸ðµÎ ã´Â´Ù.
i : ´ë¹®ÀÚ¿Í ¼Ò¹®ÀÚ¸¦ ±¸º°ÇÏÁö ¾Ê´Â´Ù.
m : StringÀ» º¹¼öÇàÀ¸·Î Ãë±ÞÇÑ´Ù.
s : StringÀ» ´ÜÀÏÇàÀ¸·Î Ãë±ÞÇÑ´Ù.
x : ÆÐÅÏ ³»¿¡ Æ÷ÇԵǴ ¹Ý°¢ Space¸¦ ¹«½ÃÇÑ´Ù.
ġȯ¿¬»êÀÚ ¿É¼Ç : $str =~ s/../../
e : ġȯ ÈÄÀÇ StringÀ» ½ÄÀ¸·Î °£ÁÖÇÑ´Ù.
g : ¸ÅÄ¡ÇÏ´Â °ÍÀ» ¸ðµÎ ã´Â´Ù.
i : ´ë¹®ÀÚ¿Í ¼Ò¹®ÀÚ¸¦ ±¸º°ÇÏÁö ¾Ê´Â´Ù.
m : StringÀ» º¹¼öÇàÀ¸·Î Ãë±ÞÇÑ´Ù.
s : StringÀ» ´ÜÀÏÇàÀ¸·Î Ãë±ÞÇÑ´Ù.
x : ÆÐÅÏ ³»¿¡ Æ÷ÇԵǴ ¹Ý°¢ Space¸¦ ¹«½ÃÇÑ´Ù.
º¯È¯¿¬»êÀÚ ¿É¼Ç : $str =~ tr/.../.../
c : º¯È¯ ´ë»óÀÎ String¿¡ Æ÷ÇÔµÇÁö ¾Ê´Â ¹®ÀÚ¸¦ º¯È¯ÇÑ´Ù.
d : º¯È¯ ÈÄÀÇ String¿¡ Æ÷ÇÔµÇÁö ¾Ê´Â ¹®ÀÚ¸¦ »èÁ¦ÇÑ´Ù.
s : Áߺ¹µÈ ¿¬¼Ó ¹®ÀÚ¸¦ 1¹®ÀÚ·Î º¯È¯ÇÑ´Ù.
s/(^.)/\U\1\E/ ; # ù¹ø° ¹®ÀÚ¸¦ ´ë¹®ÀÚ·Î ¹Ù²ã¶ó, \U~\E»çÀÌ¿¡ µé¾î°£ ¹®ÀÚ¸¦ ´ë¹®ÀÚ·Î ¹Ù²ã¶ó~
################### ¼ºê·çƾ È£Ãâ ##################
sub ¼ºê·çƾÀ̸§ {
...
return ;
}
$a = &¼ºê·çƾÀ̸§(Àμö);
################### ÆÐÅ°Áö È£Ãâ ###################
package ÆÐÅ°Áö¸í1 ;
:
package ÆÐÅ°Áö¸í2;
:
EX)
$mypack = "main ÆÐÅ°Áö" ;
:
package pack1 ;
$mypack = "pack1 ÆÐÅ°Áö";
:
sub myprint{
...
}
package pack2 ;
:
print "$pack1::mypack\n";
&pack1::myprint();
################### º¯¼öÀÇ ¹üÀ§ ###################
my($a, @b, ...);
local($a, @b, ...); #¼ºê·çƾ±îÁö ¿µÇâÀ» ¹Ìħ
################### Library¿Í Module ###################
1. Library
FileÀÇ È®ÀåÀÚ´Â .pl
File³¡¿¡ 1;¸¦ ±â¼úÇؾßÇÔ.
ex)
package packl ;
$mypack = "Library" ;
:
1;
Library ÀоîµéÀ̱â
require "packl.pl" ;
print "$packl::mypack\n";
2. Module
FileÀÇ È®ÀåÀÚ´Â .pm
File³¡¿¡ 1;¸¦ ±â¼úÇؾßÇÔ.
ex)
package packm ;
$mypack = "Module" ;
:
1;
Module ÀоîµéÀ̱â
use packm ;
print "$packm::mypack\n";
################### File Á¶ÀÛ ###################
binmode(FILE1) ; #File ÇÚµéÀ» ¹ÙÀ̳ʸ® Mode·Î ¼³Á¤
File Å×½ºÆ® ¿¬»êÀÚ
-e : FileÀÌ Á¸ÀçÇϸé Âü
-r : Àб⠰¡´ÉÀ̸é Âü
-w : ¾²±â °¡´ÉÀ̸é Âü
-z: File Å©±â°¡ 0À̸é Âü
-s : File Å©±â(FileÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é °ÅÁþ)
-T : ÅؽºÆ® FileÀ̸é Âü
-B : ¹ÙÀ̳ʸ® FileÀ̸é Âü
rename("file.txt", "newfile.txt"); #file.txt¸¦ newfile.txt·Î File¸í º¯°æ
unlink("file.txt"); #File »èÁ¦, »èÁ¦ÇÑ FileÀÇ ¼ö¸¦ Return"
################### ¹èŸÁ¦¾î ###################
flock(FLIE,2); #FILE FileÇڵ鿡 ¶ô°É±â(2)
flock(FLIE,8); #FILE FileÇڵ鿡 ¶ô ÇØÁ¦(8)
CGI(Common Gateway Interface) : À¥ºê¶ó¿ìÀú°¡ ¿äûÇϸé À¥¼¹ö°¡ ProgramÀ» ½ÇÇàÇÏ´Â ±¸Á¶¸¦ ¸»ÇÔ
chomp($_); # ÁöÁ¤ÇÑ String ³¡ÀÇ °³Çà ¹®ÀÚ¸¦ »èÁ¦ÇÔ.
################### ȯ°æº¯¼ö ###################
foreach $key (keys %ENV) { print " $key : $ENV{$key}\n";}
################### ½Ã°£ ###################
$ct = time;
@now = localtime($ct);
################### ¼öÇÐ °ü·Ã ÇÔ¼ö ###################
abs($x);
sqrt($x); # Æò¹æ±Ù
exp($x); #eÁö¼ö
log($x);
sin($x); #´ÜÀ§´Â ¶óµð¾È
cos($x);
atan2($x);
$a = 2.58;
$b = int($a); # $b = 2
$a = rand(10); #0~10¹Ì¸¸ÀÇ ¹üÀ§ÀÇ ³¼ö¸¦ ½Ç¼ö·Î Return
$a = rand(); #0~1¹Ì¸¸ÀÇ ¹üÀ§ÀÇ ³¼ö¸¦ ½Ç¼ö·Î Return
srand(ÀÓÀÇÀÇ Á¤¼ö); #´ÙÀ½¿¡ ½ÇÇàÇÏ´Â randÇÔ¼öÀÇ ½Ãµå¸¦ ¼³Á¤ÇÔ.
################### StringÀ» Á¶ÀÛÇÏ´Â ÇÔ¼ö ###################
StringÀÇ ±æÀÌ
$str = length($a);
StringÀ» ÃßÃâÇϰųª ġȯ
$strl1= "ABCEF";
$strl2 = substr($strl1, 2,3); #2¹ø° ¹®ÀڷκÎÅÍ 3°³ÀÇ ¹®ÀÚ¸¦ Return
$strl1= "ABCEF";
$strl2 = substr($strl1, 2,3,"GGGG"); 2¹ø° ¹®ÀÚ·Î ºÎÅÍ 3°³ÀÇ ¹®ÀÚ¸¦ GGGG·Î ġȯ
StringÀÇ Ã¹ À§Ä¡ ±¸Çϱâ
$strl1= "ABCEF";
$strl2 = index($strl1, "E"); #E°¡ ÃÖÃÊ·Î ³ªÅ¸³»´Â À§Ä¡ 4¸¦ ReturnÇÑ´Ù. ¾øÀ» ¶§´Â -1¸¦ Return
StringÀ» ºÐÇÒÇÏ¿© ¸®½ºÆ®·Î ¸¸µé±â
@arr = split(/,/, "I,Love,Cat"); #±¸ºÐ ¹®ÀÚ ÆÐÅÏ ' À» ±âÁØÀ¸·Î ºÐÇÒÇÏ¿© ¸®½ºÆ®¿¡ ÀúÀåÇÑ´Ù
#$arr[0] = "I",$arr[1] = "Love",$arr[2] = "Cat"
################### Array¿¡ °üÇÑ ÇÔ¼ö ###################
$str = join(" ", ("I","Love","Cat"));
#$str = "I Love Cat"
@a = sort @b;
@a = reverse @b;
$x = pop @a;
$x = shift @a;
push @a, $y; #@a¿¡ $y¸¦ Ãß°¡. Arrayµµ Ãß°¡ÇÒ ¼ö ÀÖ´Ù.
unshift @a, $y; #@aÀÇ ¸Ç ¾Õ¿¡ $y¸¦ Ãß°¡. Arrayµµ Ãß°¡ÇÒ ¼ö ÀÖ´Ù.
@a = grep /^a.*/, @b ; # @bArray¿¡¼ Á¤±ÔÇ¥Çö¿¡ ¸ÅÄ¡ÇÏ´Â ¿ä¼Ò¸¦ ÃßÃâÇÏ¿© @aArray¿¡ ÀúÀå
################### ¿¬»óArray¿¡ °üÇÑ ÇÔ¼ö ###################
@b = keys %a;
@b = value %a;
while(($k, $v) = each %a) { print "$k $v";}
$key = "name";
$b = exist $a{$key}; # %a¿¡ $key¶ó´Â key°¡ ÀÖ´ÂÁö Á¶»çÇÏ¿© 1 or 0À» ReturnÇÑ´Ù.
################### Directory °ü·Ã ÇÔ¼ö ###################
opendir(DIR, $dirname) || die "opendir Error!\n"; #DIRÀº Directory ÇÚµé¸í, $dirnameÀº Directory¸í
@alldir = readdir DIR; #Directory¿¡ Á¸ÀçÇÏ´Â FileÀÇ À̸§°ú DirectoryÀÇ À̸§À» Array¿¡ ÀúÀåÇÑ´Ù.ÇöÀçDirectory¿Í »óÀ§ Directoryµµ Æ÷ÇÔ
closedir(DIR);
$a = mkdir $dirname; #Directory »ý¼ºÀÌ ¼º°øÇϸé 1, ½ÇÆÐÇϸé 0À» º¯¼ö¿¡ ÀúÀåÇÑ´Ù.
$a = rmdir $dirname;
chdir $dirname || die "chdir Error!\n";
################### Program Á¦¾î¿¡ °üÇÑ ÇÔ¼ö ###################
$p = "1 + 1";
$a= eval($p); # $a´Â 1+1= 2ÀÌ´Ù
exit(0) ; # ProgramÀÌ Á¾·áÇÒ ¶§ System¿¡ Return °ªÀ» ÁöÁ¤ 0: Á¤»óÁ¾·á, ÀÌ¿Ü °ªÀº ÀÌ»óÁ¾·á.
die !; #Error°¡ ¹ß»ýÇϸé Error Message¸¦ Ãâ·ÂÇÏ°í ProgramÀ» Á¾·áÇÑ´Ù.
################### Data º¯°æ¿¡ °üÇÑ ÇÔ¼ö ###################
$a = hex("10"); # hexa:10 -> decimal:16
$a = chr(65); #$a='A' |
|