|
Perl Grammer |
# 71 |
JMJS 06.9.4 14:34 |
|
PerlÀÇ ¹®¹ý ¼³¸í
Perl ¹®¹ý¼³¸í
´ÙÀ½ÀÇ ±â»ç´Â ¿ù°£ ÇÁ·Î±×·¥ ¼¼°è 1997³â 6¿ùÈ£ ƯÁý±â»ç·Î ½Ç·È´ø °ÍÀÔ´Ï´Ù. ƯÁý±â»ç´Â ÃÑ 3ºÎ·Î ÀÌ·ç¾îÁ® Àִµ¥, 1ºÎ´Â
PerlÀÇ ¼Ò°³, 2ºÎ´Â PerlÀÇ ¹®¹ý, 3ºÎ´Â PerlÀ» ÀÌ¿ëÇÑ CGI ¿¹Á¦ ÇÁ·Î±×·¥ ÀÛ¼ºÀ¸·Î ±¸¼ºµÇ¾î ÀÖ½À´Ï´Ù. Á¦°¡ ¸ÃÀº
ºÎºÐÀº 2ºÎ¿´°í ÃÑ ºÐ·®ÀÇ Àý¹Ý¿¡ ÇØ´çÇÕ´Ï´Ù. 1ºÎ¿Í 3ºÎ´Â ±è´ë½Å(À¥µ¥ÀÌŸ¹ðÅ©)¾¾°¡ ¸ÃÀ¸¼Ì½À´Ï´Ù. ´ÙÀ½ÀÇ ±ÛÀº Á¦°¡ ÃʾÈÀ¸·Î
ÀÛ¼ºÇÑ ÅؽºÆ® ÆÄÀÏÀ» HTML¹®¼·Î ¾à°£ ¼ÕÀ» º» °ÍÀÔ´Ï´Ù.
Â÷·Ê
- ¼Ò°³
- PerlÀÇ ¹®¹ý
- º¯¼ö(Variable)
- ¼öÄ¡(number)
- ¹®ÀÚ¿(string)
- scalar variable
- vector variable
- Ư¼öÇÑ º¯¼ö(Special variable)
- ·¹ÆÛ·±½º(reference)
- ¹è¿ÀÇ ¹è¿, ÇؽÃÀÇ ¹è¿, ¹è¿ÀÇ ÇؽÃ
- ½Ä(Expression)
- ±âº» ¿¬»êÀÚ
- Ãß°¡ ¿¬»êÀÚ
- ±âº» ÀÔÃâ·Â ¿¬»êÀÚ
- ºñ±³ ¿¬»êÀÚ
- Control Structure(Á¦¾î ±¸Á¶)
- if
- unless
- while/until
- for
- foreach
- do/while, do/until
- goto
- next, last, redo
- ¼ºê·çƾ(Subroutine)
- Á¤ÀÇ¿Í »ç¿ë
- my¿Í local
- ÆÐÅÏ ¸ÅĪ(Pattern Matching)
- Á¤±Ô Ç¥Çö½Ä(regular expression)
- match, substitute, translate
- ¹®ÀÚ¿ pattern Á¶ÀÛ
¼Ò°³
ÆÞÀº ÀÓÀÇÀÇ ÅؽºÆ® ÆÄÀϵéÀ» °Ë»öÇÏ°í ±× ÆÄÀÏ¿¡¼ Á¤º¸¸¦ ÃßÃâÇϸç, ±× Á¤º¸¿¡
µû¶ó º¸°í¼¸¦ ÀÛ¼ºÇϴµ¥ ÃÖÀûÈµÈ ¾ð¾îÀÌ´Ù. ¶ÇÇÑ ¸¹Àº ½Ã½ºÅÛ °ü¸® ÀÛ¾÷¿¡ ÁÁÀº
¾ð¾îÀÌ´Ù. ÀÌ ¾ð¾î´Â ½Ç¿ëÀûÀÏ ¼ö ÀÖµµ·Ï ¸¸µé¾îÁ³±â ¶§¹®¿¡ Å©±â°¡ ¾ÆÁÖ À۰ųª
¾Æ¸§´ä°Ô ¸¸µé¾îÁöÁöÁö ´Â ¾Ê¾ÒÁö¸¸ »ç¿ëÇϱ⠽±°í È¿À²ÀûÀÌ¸ç ¿Ïº®ÇÏ´Ù°í ¸»ÇÒ
¼ö ÀÖ´Ù. ÆÞÀº C¿Í sed¿Í awk¿Í sh µîÀÇ »ç¶÷µéÀÌ Ä£¼÷ÇÏ°Ô »ý°¢ÇÏ´Â ¾ð¾îµéÀÇ
ÀåÁ¡À» ¸ð¾Æ¼ ¸¸µç °ÍÀ̱⠶§¹®¿¡ ¹è¿ì±â ¾î·ÆÁö ¾Ê´Ù. Ç¥Çö½ÄÀº °ÅÀÇ C¿Í
ºñ½ÁÇϸç, ´Ù¸¥ À¯´Ð½º À¯Æ¿¸®Æ¼¿Í´Â ´Þ¸® ¿ë·® µîÀÇ Á¦ÇÑÀÌ ¾ø´Ù. ÁÖ·Î text¸¦
ó¸®Çϴµ¥ ¶Ù¾î³ ´É·ÂÀ» º¸¿©ÁÖÁö¸¸, ¹ÙÀ̳ʸ® ÀڷḦ ´Ù·ç´Âµ¥ À־µ
µ¥ÀÌÅͺ£À̽º¸¦ ¸¸µå´Â ±â´ÉÀÌ ÀÖÀ» Á¤µµÀÌ´Ù. sed³ª awk¸¦ »ç¿ëÇϴµ¥ ¹®Á¦°¡
¹ß»ýÇÑ´Ù¸é C·Î Â¥´Â ´ë½Å¿¡ ÆÞ·Î ÀÛ¼ºÇÏ¸é ½ºÅ©¸³Æ®¸¦ °ÅÀÇ ¹Ù²ÙÁö ¾Ê¾Æµµ µÈ´Ù.
2ºÎ. ÆÞÀÇ ¹®¹ý
PerlÀÇ ÀúÀÚ, Larry WallÀÌ ¸»Çß´ø °Íó·³ PerlÀº ÇÑ °¡Áö ÀÏÀ» Çϱâ À§ÇØ 10°¡Áö ¹æ¹ýÀ» Á¦½ÃÇÏ´Â
¾ð¾îÀÌ´Ù. ¾î¶»°Ô(how) ÇÒ °ÍÀÎÁö°¡ Áß¿äÇÑ °Ô ¾Æ´Ï¶ó ¹«¾ùÀ»(what) ÇÒ °ÍÀÎÁö¸¦ ¸ÕÀú »ý°¢ÇÏ°í ÇÁ·Î±×·¡¹ÖÀ» ÇØ¾ß ÇÒ
°ÍÀÌ´Ù. ±×·¯³ª ´Ù¾çÇÑ Ç¥Çö ¹æ½ÄµéÀ» ¸¹ÀÌ ÀÍÈ÷¸é ÀÍÈú¼ö·Ï ÇÁ·Î±×·¡¸Ó¿¡°Ô ´õ ¸¹Àº °¡´É¼ºÀÌ ¿¸®´Â ¼ÀÀÌ´Ù. ´ÙÀ½ÀÇ ±ÛÀº µ¶ÀÚµéÀÌ
±âº»ÀûÀÎ CÇÁ·Î±×·¡¹ÖÀ» ÀÌÇØÇÒ ¼ö ÀÖ°í À¯´Ð½ºÀÇ shell±â¹ÝÀÇ ÀÛ¾÷µé¿¡ ¹«ÁöÇÏÁö ¾ÊÀ½À» °¡Á¤ÇÏ°í ¾²¿©Áø °ÍÀÌ´Ù. ±×·¯³ª
°¡´ÉÇϸé Ãʺ¸Àڵ鵵 ÀÌÇØÇÒ ¼ö ÀÖµµ·Ï ½¬¿î Ç¥Çö°ú ¿¹Á¦¸¦ ÀÌ¿ëÇÏ¿´´Ù. ´ÙÀ½ÀÇ ¼³¸íÀº Perl 5¸¦ ±âÁØÀ¸·Î ¾²¿©Á³À¸³ª, Perl
4¿¡¼µµ ¹«³ÇÏ°Ô »ç¿ë °¡´ÉÇÒ °ÍÀÌ´Ù.
1. º¯¼ö(Variable)
±âº»ÀûÀ¸·Î PerlÀº º¯¼ö¿¡ ´ëÇØ, C¿Í °°ÀÌ °·ÂÇÑ type checkingÀ» ÇÏÁö ¾Ê´Â´Ù. ´Ù½Ã ¸»Çؼ '°ªÀÌ ¾î¶²
¹æ½ÄÀ¸·Î ÀúÀåµÇ¾îÀִ°¡'´Â '¾î¶»°Ô »ç¿ëÇÒ °ÍÀΰ¡'¿Í´Â º°°³ÀÇ ¹®Á¦¶ó´Â °ÍÀÌ´Ù. CÇÁ·Î±×·¡¹Ö¿¡ Àͼ÷ÇÑ »ç¿ëÀÚ¶ó¸é int c =
'x'; À¸·Î Á¤ÀÇµÈ º¯¼ö c°¡ ¹®ÀÚÇüÀ¸·Îµµ Á¤¼öÇüÀ¸·Îµµ »ç¿ë °¡´ÉÇÑ °ÍÀ» ¶°¿Ã¸± ¼ö ÀÖÀ» °ÍÀÌ´Ù. ±×·± °ÍÀ» È®ÀåÇؼ
»ý°¢Çغ¸¸é PerlÀÇ º¯¼öµéÀº ¿©·¯ ÇüÅ·Π»ç¿ëÇÒ ¼ö ÀÖ´Ù. ±×°ÍÀº Perl ÀÎÅÍÇÁ¸®ÅÍ(interpreter;Çؼ®±â)°¡
³»ºÎÀûÀ¸·Î ÀÚµ¿ º¯È¯ÇØ Áֱ⠶§¹®¿¡ °¡´ÉÇÑ °ÍÀÌ´Ù.
1) ¼öÄ¡(number)
Perl¿¡´Â Á¤¼öÇü ¼öÄ¡´Â Á¸ÀçÇÏÁö ¾Ê´Â´Ù. ¸ðµç ¼öÄ¡´Â double-precision floating pointÇü ¼öÄ¡·Î Á¸ÀçÇÑ´Ù. ´ÙÀ½°ú °°Àº ÇüÅÂÀÇ ¼öÄ¡´Â float literal(¼Ò¼öÇü »ó¼ö)ÀÌ´Ù.
111.25
23.25e91
-12.5e29
-12e-34
3.2E-23
´ÙÀ½°ú °°Àº ÇüÅÂÀÇ ¼öÄ¡´Â integer-literal(Á¤¼öÇü »ó¼ö)ÀÌ´Ù. ¼ýÀÚ ¾Õ¿¡ 0À̳ª 0x¸¦ ºÙ¿©¾²°Ô µÇ¸é °¢°¢ 8Áø¼ö¿Í 16Áø¼ö¸¦ ÀǹÌÇÏ°Ô µÈ´Ù.
342
-2423
0377
-0xf0
2) ¹®ÀÚ¿(string)
¹®ÀÚ¿Àº À̸§ÀÌ ¶æÇϵíÀÌ ¿¬¼ÓµÈ ¹®ÀÚµéÀÇ ¼ø¿ÀÌ´Ù. ÀϹÝÀûÀÎ ASCII ¹®ÀÚ´Â 128°³·Î Ç¥ÇöÀÌ µÇÁö¸¸, Perl¿¡¼´Â
Ư¼ö¹®ÀÚ°¡ Ç¥ÇöµÇ¾î¾ß Çϱ⠶§¹®¿¡ extended ASCII¸¦ Áö¿øÇÏ¿© ¹®ÀÚ¿ ³»ºÎÀÇ °¢°¢ÀÇ ±ÛÀÚ´Â 0¿¡¼ 255±îÁöÀÇ
ASCII°ªÀ» °¡Áö°Ô µÈ´Ù. ¹®ÀÚ¿Àº ¿©·¯ ÇüÅÂÀÇ µÎ quotation mark·Î °¨½Î¼ Ç¥ÇöÇÒ ¼ö Àִµ¥, ´ëÇ¥ÀûÀÎ ÄÉÀ̽º´Â
''(single-quote)¿Í ""(double-quote), ``(back-quote)ÀÌ´Ù. single-quote»çÀÌ¿¡
µé¾î°¡´Â ¹®ÀÚ¿Àº interpolationÀ» ÇÏÁö ¾Ê°í, double-quote»çÀÌ¿¡ µé¾î°¡´Â ¹®ÀÚ¿Àº
interpolationÀ» ÇÏ°Ô µÇ´Â Â÷ÀÌÁ¡ÀÌ ÀÖ´Ù. ±×·¯¸é interpolationÀ̶õ ¹«¾ùÀΰ¡ ¿¹¸¦ º¸¸é¼ ÀÌÇØÇϵµ·Ï ÇÏÀÚ.
$str1 = 'hello\n\neveryone';
$str2 = "hello\n\neveryone";
print $str1 . "\n";
print $str2 . "\n";
µÎ ¹®ÀåÀº quotation mark°¡ ´Ù¸£´Ù´Â Á¡À» Á¦¿ÜÇÏ°í´Â Â÷ÀÌÁ¡ÀÌ ¾ø´Ù. ±×·±µ¥ Ãâ·Â°á°ú´Â ´ÙÀ½°ú °°ÀÌ ´Ù¸£°Ô µÈ´Ù.
hello\n\neveryone # ÀÌ°ÍÀº str1ÀÇ Ãâ·Â °á°úÀÌ´Ù.
hello # ÀÌ°ÍÀº str2ÀÇ Ãâ·Â °á°úÀÌ´Ù.
everyone
À§¿Í °°Àº escape character»Ó¸¸ ¾Æ´Ï¶ó ÀÌ¹Ì Á¤ÀǵǾîÁø º¯¼öµéÀ» ¹®ÀÚ¿ ¾È¿¡¼ »ç¿ëÇÏ´Â °æ¿ì¿¡µµ interpolationÀÇ °¡´É ¿©ºÎ¿¡ µû¶ó ±× º¯¼ö°¡ Çؼ®µÇ±âµµ ÇÏ°í ±×·¸Áö ¾Ê±âµµ ÇÑ´Ù.
$str0 = "I'm a boy.";
$str1 = 'hello $str0';
$str2 = "hello $str0";
print $str1 . "\n";
print $str2 . "\n";
°á°ú´Â ´ÙÀ½°ú °°´Ù.
hello $str0 # str1ÀÇ Ãâ·Â °á°ú
hello I'm a boy. # str2ÀÇ Ãâ·Â °á°ú
single-quote ¹®ÀÚ¿¿¡¼ single-quote(')¸¦ Ç¥ÇöÇÏ´Â ¹æ¹ýÀº backslashµÚ¿¡ single-quote¸¦ ¾²´Â °ÍÀÌ´Ù.
print 'I\'m a boy.'; # Ãâ·Â °á°ú I'm a boy.
´ÙÀ½Àº double-quote¾È¿¡¼ Ưº°ÇÑ È¿°ú¸¦ °¡Áö´Â escape characterÀÇ Á¾·ù¿Í ±× Àǹ̿¡ °üÇÑ Ç¥ÀÌ´Ù.
\n | newline ´ÙÀ½ ÁÙ·Î |
\r | return ÁÙÀÇ ¸Ç ¾ÕÀ¸·Î |
\t | tab ÅÇ |
\f | formfeed |
\b | backspace |
\v | vertical tab |
\a | alarm bell |
\e | ESC |
\073 | octal value 8Áø¼ö(¿©±â¼´Â 73(8) = 7*8+3(10) = 59(10)) |
\x7f | hexadecimal value 16Áø¼ö(¿©±â¼´Â 7f(16) = 7*16+15(10) = 127(10)) |
\cC | Á¦¾î¹®ÀÚ C |
\\ | backslash |
\" | double-quote |
\l | ´ÙÀ½ ±ÛÀÚ¸¦ ´ë¹®ÀÚ·Î |
\L | ´ÙÀ½ ±ÛÀÚ¸¦ ¼Ò¹®ÀÚ·Î |
\u | µÚ¿¡ ¿À´Â ¸ðµç ±ÛÀÚ¸¦ ´ë¹®ÀÚ·Î |
\U | µÚ¿¡ ¿À´Â ¸ðµç ±ÛÀÚ¸¦ ¼Ò¹®ÀÚ·Î |
\Q | µÚ¿¡ ¿À´Â ¸ðµç Ư¼ö¹®ÀÚ¿¡(non-alphanumeric) \¸¦ ºÙ¿©¼ |
\E | \U, \L, \QÀÇ ³¡À» Ç¥½Ã |
¸¶Áö¸·À¸·Î back-quote°¡ Àִµ¥, ÀÌ°ÍÀº ¹®ÀÚ¿À̶ó±âº¸´Ù´Â ¹®ÀÚ¿À» shell¿¡¼ ½ÇÇàÇÑ °á°ú °ªÀ» ÀúÀåÇÏ°Ô µÈ´Ù. back-quote´Â back-tickÀ̶ó°í ºÎ¸£±âµµ ÇÑ´Ù.
$time_of_today = `date`;
print $time_of_today; # Ãâ·Â °á°ú Mon Apr 27 20:59:04 KST 1998
À§¿Í °°Àº Äڵ忡¼´Â PerlÀÌ date¶ó´Â ÇÁ·Î±×·¥À» ½ÇÇàÇÏ¿© ³ª¿À´Â °ªÀÌ º¯¼ö¿¡ ÀúÀåµÇ°Ô µÈ´Ù.
back-quote¹®ÀÚ¿¿¡¼´Â double-quote¹®ÀÚ¿¿¡¼¿Í ¸¶Âù°¡Áö·Î interpolationÀÌ µÇ¾îÁø´Ù. ´ÙÀ½ ÄÚµå´Â
shell¿¡¼ "ls | grep doc"¸¦ ½ÇÇàÇÑ °Í°ú °°Àº °á°ú¸¦ Ãâ·ÂÇÏ´Â °ÍÀÌ´Ù.
$keyword = 'doc';
print `ls | grep $keyword`;
Ãâ·Â°á°ú
jdbcprog.doc
vi.doc
xprog.doc
´ÙÀ½ÀÇ Ç¥´Â quotationÀÇ ¿©·¯ °¡Áö ¹æ¹ý¿¡ ´ëÇØ Á¤¸®ÇÑ °ÍÀÌ´Ù.
°ü½ÀÀû »ç¿ë | ÀϹÝÀû »ç¿ë | ÀÇ¹Ì | interpolation¿©ºÎ |
'' | q// | ¹®ÀÚ¿»ó¼ö | x |
"" | qq// | ¹®ÀÚ¿»ó¼ö | o |
`` | qx// | ½ÇÇà¸í·É | o |
() | qw// | list | x |
// | m// | pattern match | o |
s/// | s/// | substitution | o |
y/// | tr/// | translation | x |
ÀϹÝÀû »ç¿ë¿¡¼ q ´ÙÀ½¿¡ ³ª¿ÍÀÖ´Â backslash(/)´Â ´Ù¸¥ ¹®ÀÚ·Î ¹Ù²Ù¾î »ç¿ëÇÒ ¼ö ÀÖ´Ù. ÀÚ¼¼ÇÑ °ÍÀº µÚ¿¡ ³ª¿À´Â ¼Ò´Ü¿øÀ» Âü°íÇϵµ·Ï ÇÏ°í °£´ÜÇÑ ¿¹Á¦¸¦ ÅëÇØ ±× »ç¿ë¹ýÀ» ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.
$abc = q$I'm a boy. You're a girl.$;
$def = qq#Hello,\neveryone#;
$ghi = q&
while (test != 0) {
i++;
printf("%d", i);
}
&;
º¯¼ö abc´Â $¸¦ single-quote(') ´ë½ÅÀ¸·Î »ç¿ëÇÏ¿´°í, def´Â #À» double-quote(") ´ë½ÅÀ¸·Î
»ç¿ëÇÏ¿´´Ù. ghiÀÇ °æ¿ì, multi-line stringÀ» ÁöÁ¤Çϴµ¥, single-quote(') ´ë½Å¿¡ &±âÈ£¸¦
»ç¿ëÇÏ¿´´Ù.
3) scalar variable
scalar variableÀº ÇϳªÀÇ °ªÀ» °¡Áö´Â º¯¼ö¸¦ ¶æÇÏ°í ´ÙÀ½¿¡¼ »ìÆì º¼ vector variable°ú´Â »ó´ëµÇ´Â
Ç¥ÇöÀÌ´Ù. scalar variableÀº ¾Õ¿¡¼ º¸¾Ò´ø ¼öÄ¡³ª ¹®ÀÚ¿°ú °°Àº °ªÀ» ¿À·ÎÁö Çϳª¸¸ °¡Áö´Â °ªÀÌ´Ù. ÀϹÝÀûÀ¸·Î
scalar variableÀº $±âÈ£¸¦ º¯¼ö À̸§ ¾Õ¿¡ ºÙ¿© vector variable°ú ±¸ºÐÇÑ´Ù. Perl¿¡¼´Â C¿¡¼¿Í
¸¶Âù°¡Áö·Î º¯¼öÀÇ À̸§Àº ´ë¼Ò¹®ÀÚ°¡ ´Þ¶óÁö¸é ÀüÇô ´Ù¸¥ º¯¼ö·Î ÀνĵȴÙ. ´ÙÀ½ÀÇ º¯¼öµéÀº ¸ðµÎ ´Ù¸£°Ô ±¸ºÐµÇ´Â scalar
º¯¼öµéÀÌ´Ù.
$abc
$ABC
$account_number_of_bank1
$account_number_of_bank2
$xyz001
scalar variable¿¡ ´ëÇؼ »ê¼ú ¿¬»êÀÚ µîÀÇ ÀϹÝÀûÀÎ ¿¬»êÀÚ¸¦ ¼öÇàÇÒ ¼ö ÀÖ´Ù.
4) vector variable
vector variableÀº Å©°Ô ¼¼ °¡Áö Á¾·ù·Î ³ª´ ¼ö Àִµ¥, array¿Í associative array°¡ ±×°ÍÀÌ´Ù.
Perl¿¡¼´Â array´Â list¶ó´Â À̸§À¸·Î, associative array´Â hash¶ó´Â À̸§À¸·Î ÀÏÄþîÁö±âµµ ÇÑ´Ù.
list´Â º¯¼ö À̸§ ¾Õ¿¡ @Ç¥½Ã¸¦ ÇÏ¿© ±¸ºÐÇÏ°í, hash´Â %±âÈ£¸¦ »ç¿ëÇÑ´Ù. ´ÙÀ½ÀÇ º¯¼öµéÀº °¢°¢ list¿Í hashÀÇ ¿¹ÀÌ´Ù.
@certain_list = ("abc", "def", "123");
@quoted_list = qw(
1, 2, 3,
4, 5, 6,
7, 8,
);
%a_special_hash = (('test', "hello"), ('verify', 123));
%list_like_hash = ("red", 0xf00, "green", 0x0f0, "blue", 0x00f);
%another_special_hash = (
red => 0xf00,
green => 0xf00,
blue => 0xf00,
);
vector variableÀº ´Ù¸¥ vector ÇüÅÂÀÇ variableÀ» Æ÷ÇÔÇÏ¿© ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù.
@next_list = ("abc", "def", "123", @previous_list);
%general_hash = (%a_special_hash, %list_like_hash);
vector variableÀº scalar variable°ú´Â ´Þ¸® ÁÖÀÇÇؼ »ç¿ëÇØ¾ß ÇÑ´Ù. quotationÀ» ÇÏ´Â °æ¿ì¿Í
¾Æ´Ñ °æ¿ì°¡ ´Ù¸£°í, ±× º¯¼ö¸¦ vectorÂ÷¿ø¿¡¼ ´Ù·ç´Â °æ¿ì¿Í ±×ÀÇ ¿ø¼ÒÀÎ scalarÂ÷¿ø¿¡¼ ´Ù·ç´Â °æ¿ì°¡ ´Ù¸£´Ù. ´ÙÀ½
¿¹Á¦¸¦ Âü°íÇÏ¿© Â÷ÀÌÁ¡À» ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.
print "@certain_list";
print @certain_list;
µÎ ¹®ÀåÀÇ Ãâ·Â °á°ú´Â ´ÙÀ½°ú °°´Ù. print´Â ù ¹ø°ÀÇ °æ¿ì¸¦ list¸¦ quotationÇÑ °ÍÀ¸·Î °£ÁÖÇÏ¿© space¸¦ Ãâ·ÂÇØÁÖ°í, µÎ ¹ø°ÀÇ °æ¿ì¸¦ list·Î °£ÁÖÇÏ¿© space¾øÀÌ ºÙ¿©¼ Ãâ·ÂÇØÁØ´Ù.
abc def 123
abcdef123
´ÙÀ½ÀÇ hash¿¡ °ü·ÃµÈ ÄÚµå´Â list¿Í °°À»±î?
print "%a_special_hash";
print %a_special_hash;
list¿Í´Â ´Þ¸® hashÀÇ °æ¿ì interpolationÀÌ ÀϾÁö ¾ÊÀ½À» ¾Ë ¼ö ÀÖ´Ù.
testhelloverify123
%a_special_hash
ÀϹÝÀûÀÎ Àǹ̿¡¼ÀÇ "hash"¶õ µÎ°³ÀÇ °ªÀ» ÇÑ ½ÖÀ¸·Î ÇÏ¿© ÀúÀåÇÏ´Â ¹æ½ÄÀ» ÀǹÌÇϴµ¥, ¿©±â¼µµ ¸¶Âù°¡Áö·Î hash´Â
key¿Í value¸¦ ±¸ºÐÇÏ¿© »ç¿ëÇÏ°Ô µÈ´Ù. ¾Õ¿¡¼ Á¦½ÃÇÑ ¿¹¿¡¼´Â "test"¿Í "verify", "red",
"green", "blue"µîÀÌ key°¡ µÇ°í, "hello", 123, 0xf00, 0x0f0, 0x00fµîÀÌ ±× key·Î
ãÀ» ¼ö ÀÖ´Â value°¡ µÈ´Ù.
print $a_special_hash{'test'};
$key = 'verify';
print $a_special_hash{$key};
listÀÇ °æ¿ì¿¡´Â °¢°¢ÀÇ value¸¦ ó¸®Çϱâ À§ÇØ index(subscript)¸¦ »ç¿ëÇÑ´Ù. ÀÌ·± °æ¿ì¿¡´Â list ÁßÀÇ
ÇϳªÀÇ scalar variable¿¡ ´ëÇÑ ¿¬»êÀÌ µÈ´Ù. C¿¡¼ÀÇ index¿Í´Â ´Þ¸® Perl¿¡¼´Â ..¸¦ ÀÌ¿ëÇؼ
range(¹üÀ§)¿¡ ´ëÇÑ ¿¬»êµµ °¡´ÉÇÏ´Ù.
print $colors[0];
$id_list[3]++;
print @month[8..11]; # @month[8..11]´Â @month[8, 9, 10, 11]°ú °°´Ù.
$b = $namelist[$number];
list ´ÜÀ§ÀÇ ¿¬»êµµ °¡´ÉÇÏ´Ù. ´ëÇ¥ÀûÀÎ °ÍÀ¸·Î list assignment°¡ ÀÖ´Ù.
($a, $b, $c) = (3, 4, 5);
@array = ('a', 'b', 'c', 'd');
vector variable Áß¿¡´Â ¾î¶² °ªµµ °¡ÁöÁö ¾Ê´Â º¯¼ö°¡ Á¸ÀçÇÒ ¼ö Àִµ¥, ÀÌ·± °ÍÀ» null list¶ó°í Çϸç ()·Î Ç¥ÇöÇÑ´Ù.
@a = ();
ƯÈ÷ null list´Â 0, "0"°ú ÇÔ²² Á¶°Ç¹®¿¡¼ false expressionÀ» ÀǹÌÇÏ´Â °ÍÀ¸·Î »ç¿ëµÈ´Ù.
listÀÇ ¸¶Áö¸· À妽º¸¦ ¾Ë±â À§Çؼ´Â $#±âÈ£¸¦ º¯¼öÀ̸§ ¾Õ¿¡ ºÙÀÌ¸é µÈ´Ù. list Áß¿¡ Æ÷ÇԵǾî ÀÖ´Â ¿ø¼ÒÀÇ °³¼ö¸¦ ¾Ë±â
À§Çؼ´Â scalar()ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù. À妽º´Â 0ºÎÅÍ ¸Å°ÜÁö±â ¶§¹®¿¡ ÀϹÝÀûÀ¸·Î $#list + 1 ==
scalar(@list)°¡ ¼º¸³ÇÑ´Ù.
5) Ư¼öÇÑ º¯¼ö(Special variable)
a) Regular expression variable
¿ì¼± $*¶ó´Â Ư¼öº¯¼ö°¡ multi-line mode¸¦ À§ÇØ Perl 4±îÁö »ç¿ëµÇ¾ú´Âµ¥, Perl 5ºÎÅÍ´Â regular
expressionÀ» »ç¿ëÇÒ ¶§ modifier¸¦ »ç¿ëÇÏ´Â ¹æ½ÄÀ¸·Î ¹Ù²î¸é¼ ´õ ÀÌ»ó »ç¿ëµÇÁö ¾Ê°Ô µÇ¾ú´Ù. ÀÌ°ÍÀº ÆÐÅÏ ¸ÅĪÀÇ
regular expressionºÎºÐÀ» Âü°íÇϵµ·Ï ÇÑ´Ù.
special variableµéÀº ±â¾ïÇϱ⠽±µµ·Ï mnemonic nameÀ» °¡Áö°í Àִµ¥, ±×·¯ÇÑ ±â´ÉÀ» »ç¿ëÇÏ·Á¸é Perl ÇÁ·Î±×·¥ÀÇ ¾ÕºÎºÐ¿¡¼
use English;
·Î ÁöÁ¤ÇØÁÖ¾î¾ß ÇÑ´Ù. ´ÙÀ½Àº °¢°¢ÀÇ special variable°ú ±× ¿ªÇÒ¿¡ ´ëÇØ Á¤¸®ÇسõÀº Ç¥ÀÌ´Ù. pattern match¿Í °ü·ÃµÈ º¯¼öµéÀº Áö¿ªº¯¼ö(local special variable)ÀÓÀ» ±â¾ïÇϵµ·Ï ÇÏÀÚ.
$digit |
$1, $2, $3, ... |
ÆÐÅÏ ¹Ù±ù¿¡¼, ¸ÅÄ¡(match)°¡ ÀÏ¾î³ °ýÈ£ ¾ÈÀÇ substringÀ» ÁöÁ¤ÇÒ ¶§ »ç¿ëµÇ´Â º¯¼ö·Î ¿ÞÂÊ °ýÈ£'('ÀÇ ¼ø¼´ë·Î digitÀ§Ä¡¿¡ ¼ýÀÚ°¡ »ç¿ëµÈ´Ù. |
\digit |
\1, \2, \3, ... |
ÆÐÅÏ ¾È¿¡¼ »ç¿ëµÇ´Â $digitÇü º¯¼ö¿Í °°Àº ¿ªÇÒÀ» ÇÑ´Ù. |
$& |
$MATCH |
°¡Àå ¸¶Áö¸·¿¡ ¸ÅÄ¡°¡ ÀÏ¾î³ substringÀ» ÁöÁ¤ÇÏ´Â º¯¼ö |
$` |
$PREMATCH |
$MATCH ¾ÕÂÊÀÇ substringÀ» ÁöÁ¤ÇÏ´Â º¯¼ö |
$' |
$POSTMATCH |
$MATCH µÚÂÊÀÇ substringÀ» ÁöÁ¤ÇÏ´Â º¯¼ö |
$+ |
$LAST_PAREN_MATCH |
ÆÐÅÏ ³»ÀÇ °¡Àå µÚÂÊ¿¡ À§Ä¡ÇÑ °ýÈ£ ¾ÈÀÇ substringÀ» ÁöÁ¤ÇÏ´Â º¯¼ö·Î alternative matching »ç¿ë½Ã Æí¸®ÇÏ´Ù. |
$* |
$MULTILINE_MATCHING |
default·Î 0ÀÇ °ªÀ» °¡Áö¸é¼ single-line matching mode¸¦ ÁöÁ¤ÇÏ°í, 1ÀÇ °ªÀ» °¡Áö°Ô µÇ¸é multi-line matching mode¸¦ ÁöÁ¤ÇÑ´Ù. |
¿©±â¼ Á¦½ÃµÈ ¿©·¯ special variable Áß¿¡¼ $*¸¸ÀÌ °ªÀ» ¹Ù²Ü ¼ö ÀÖ´Â º¯¼öÀÌ°í, ³ª¸ÓÁö º¯¼öµéÀº ¸ðµÎ read-onlyÀÇ ¼Ó¼ºÀ» °¡Áö´Â º¯¼öÀÌ´Ù.
´ÙÀ½Àº local special variableÀ» »ç¿ëÇÑ ¿¹Á¦·Î¼, default input variableÀÎ $_¿¡¼
pattern matchingÀ» ¼öÇàÇÏ°í match°¡ ÀÏ¾î³ ºÎºÐ°ú ¹Ù·Î ¾ÕºÎºÐ, ¹Ù·Î µÞºÎºÐÀÌ $`, $&, $'¿¡
ÀúÀåµÊÀ» º¸¿©ÁØ´Ù.
$_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n";
Ãâ·Â°á°ú´Â abc:def:ghi ÀÌ´Ù.
´ÙÀ½Àº À§Ä¡»óÀ¸·Î °¡Àå µÚÂÊ¿¡¼ ¸ÅÄ¡°¡ ÀϾ´Â ºÎºÐÀº Revision: µÚÂÊÀÇ ¹®ÀÚ¿À̸ç, ÀÌ ¹®ÀÚ¿Àº $+¿¡ ÀúÀåµÊÀ» ¾Ë ¼ö
ÀÖ´Â ¿¹Á¦ÀÌ´Ù. pattern matchingÀÌ ¼º°øÀûÀ¸·Î ¼öÇàµÇ¸é(match°¡ ÀϾ¸é) $rev¿¡ revision°ªÀ»
ÀúÀåÇÏ°Ô µÈ´Ù.
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
b) Per-Filehandle variable
FilehanleÀ̶õ »ç¿ëÀÚ°¡ ÀÛ¼ºÇÑ Perl program°ú ¿ÜºÎ¿¡ Á¸ÀçÇÏ´Â ÆÄÀÏ»çÀÌÀÇ I/O ¿¬°áÀ» À§ÇÑ Perl
programÀÇ À̸§ÀÌ´Ù. UnixÀÇ Ç¥ÁØ file-descriptor¸¦ Áö¿øÇϱâ À§ÇÑ filehandle·Î´Â
STDIN(Ç¥ÁØÀÔ·Â)¿Í STDOUT(Ç¥ÁØÃâ·Â), STDERR(Ç¥ÁØ¿¡·¯Ãâ·Â)°¡ ÀÖ´Ù. ÀÌ¿Ü¿¡µµ »ç¿ëÀÚ°¡ ƯÁ¤ÇÑ ÆÄÀÏÀ» ´Ù·ç±â À§ÇØ
filehandleÀ» ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù. ÀÚ¼¼ÇÑ ³»¿ëÀº ÆÄÀÏÁ¶ÀÛ¿¡ °ü·ÃµÈ ÀýÀ» Âü°íÇϵµ·Ï ÇÏÀÚ.
method HANDLE EXPR ¶Ç´Â HANDLE->method(EXPR)°ú °°Àº Çü½ÄÀ¸·Î ÁöÁ¤ÇÑ ´ÙÀ½¿¡ ´ÙÀ½°ú °°Àº
Äڵ带 »ç¿ëÇÏ¿© ´ÙÀ½ÀÇ º¯¼öµéÀ» ÁöÁ¤ ¶Ç´Â º¯°æÇÒ ¼ö ÀÖ´Ù. HANDLEÀ§Ä¡¿¡´Â »ç¿ëÇÒ filehandleÀÇ À̸§À» ÁöÁ¤Çϸé
µÇ°í, EXPRÀ§Ä¡¿¡´Â ÇöÀç filehandleÀÇ ¼Ó¼ºÀÇ °ªÀ» ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù. »ç¿ë°¡´ÉÇÑ method´Â ¾Æ·¡ Á¤¸®µÇ¾î ÀÖ´Ù.
use FileHandle;
$| |
$OUTPUT_FLUSH_NUMBER | autoflush HANDLE EXPR |
°ªÀ» 0ÀÌ ¾Æ´Ñ °ªÀ¸·Î ÁöÁ¤Çϸé, write¶Ç´Â print¹®Àå ÈÄ¿¡ fflush(3)ÇÔ¼ö¸¦ È£ÃâÇÏ¿© ÇöÀç ¼±ÅÃµÈ output channel·Î Ãâ·ÂÀ» °Á¦ÇÑ´Ù. |
$% |
$FORMAT_PAGE_NUMBER | format_page_nubmer HANDLE EXPR |
ÇöÀç ¼±ÅÃµÈ output channelÀÇ ÇöÀç page ¹øÈ£ÀÌ´Ù. |
$= |
$FORMAT_LINES_PER_PAGE | format_lines_per_page HANDLE EXPR |
ÇöÀç ¼±ÅÃµÈ output channelÀÇ ÇöÀç page ±æÀÌ(ÇÁ¸°Æ® °¡´ÉÇÑ lineÀÇ ¼ö)ÀÌ´Ù. |
$- |
$FORMAT_LINES_LEFT | format_lines_left HANDLE EXPR |
ÇöÀç ¼±ÅÃµÈ output channelÀÇ ³²¾ÆÀÖ´Â lineÀÇ ¼öÀÌ´Ù. |
$~ |
$FORMAT_NAME | format_name HANDLE EXPR |
ÇöÀç ¼±ÅÃµÈ output channelÀÇ ÇöÀç report formatÀÇ À̸§ÀÌ´Ù. |
$~ |
$FORMAT_TOP_NAME | format_top_name HANDLE EXPR |
ÇöÀç ¼±ÅÃµÈ output channelÀÇ ÇöÀç top-of-page formatÀÇ À̸§ÀÌ´Ù. |
´ÙÀ½Àº °ü·ÃµÈ Ư¼öÇÑ º¯¼öµéÀÇ »ç¿ë ¿¹Á¦ÀÌ´Ù.
use FileHandle;
use English;
print '$| = '."$|\n";
print '$% = '."$%\n";
print '$- = '."$-\n";
print '$~ = '."$~\n";
print '$^ = '."$^ \n";
print '$= = '."$=\n";<
print '$= = '."$FORMAT_LINES_PER_PAGE\n";
format_lines_per_page STDOUT 30;
print '$= = '."$FORMAT_LINES_PER_PAGE\n";
print '$= = '."$=\n";
ƯÁ¤ filehandleÀÎ HANDLE¿¡ per-filehandle special variableÀ» ÁöÁ¤ÇØÁÖ´Â ¿¹Á¦´Â ´ÙÀ½°ú °°´Ù.
select((select(HANDLE), $| = 1, $^ = 'mytop')[0]);
c) Global Special variable
$_ | $ARG |
default input string, standard inputÀ̳ª ù ¹ø° argument·Î ³Ñ°ÜÁø ÆÄÀÏÀ» ÀÐÀ» ¶§ ³Ñ°ÜÁ® ¿À´Â stringÀ» ÀǹÌÇÑ´Ù. |
$. | $INPUT_LINE_NUMBER $NR |
¸¶Áö¸·À¸·Î ÀÐÈù filehandleÀÇ ÇöÀç input line numberÀÌ´Ù. ¿©·¯ argument·Î ³Ñ°ÜÁø ÆÄÀϵéÀ» ±¸ºÐÇÏÁö ¾ÊÀ¸¹Ç·Î, ´Ù¸¥ ÆÄÀÏ·Î ¹Ù²î´õ¶óµµ line number°¡ °è¼Ó Áõ°¡ÇÏ°Ô µÈ´Ù. |
$/ | $INPUT_RECORD_SEPARATOR $RS |
input record¸¦ ±¸ºÐÇØÁÖ´Â stringÀ» ÀǹÌÇϸç default°ªÀº newline characterÀÌ´Ù. |
$, | $OUTPUT_FIELD_SEPARATOR $OFS |
output field¸¦ ±¸ºÐÇØÁÖ´Â stringÀ̸ç, print ¿¬»êÀÚ¿¡¼ »ç¿ëµÈ´Ù. ÀϹÝÀûÀ¸·Î´Â comma(,)°¡ separator·Î »ç¿ëµÈ´Ù. |
$\ | $OUTPUT_RECORD_SEPARATOR $ORS |
output record¸¦ ±¸ºÐÇØÁÖ´Â stringÀ¸·Î¼ º¸ÅëÀº print ¿¬»êÀÚ°¡ recordµÚ¿¡ newline³»Áö´Â record separatorÀ» Âï¾îÁÖÁö ¾Ê±â ¶§¹®¿¡ ÇÊ¿äÇÒ °æ¿ì, ÁöÁ¤ÇÏ¿©¾ß ÇÑ´Ù. |
$" | $LIST_SEPARATOR |
listÀÇ ±¸ºÐÀÚ·Î »ç¿ëµÇ´Â stringÀ» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. ±âº» °ªÀ¸·Î space°¡ ÁöÁ¤µÇ¾î ÀÖ´Ù. |
$; | $SUBSCRIPT_SEPARATOR $SUBSEP |
´ÙÂ÷¿ø ¹è¿À» ¸¸µé±â À§ÇØ list³»¿¡¼ º¯¼öµéÀ» ³ª¿ÇÒ °æ¿ì, ±× º¯¼öµéÀÌ ÇϳªÀÇ record·Î ÀνĵǾî¾ß Çϴµ¥, ±×°ÍÀ» À§Çؼ list³»ÀÇ °¢ º¯¼öµéÀ» joinÇϴµ¥ »ç¿ëÇÏ´Â º¯¼öÀÌ´Ù. |
$^L |
$FORMAT_FORMFEED format_formfeed HANDLE EXPR |
printÇÒ ¶§, outputÀÇ format¿¡¼ formfeed·Î »ç¿ëµÉ stringÀ» ÁöÁ¤ÇÏ´Â º¯¼ö·Î¼, ±âº» °ªÀº "\f"ÀÌ´Ù. |
$: |
$FORMAT_LINE_BREAK_CHARACTERS format_line_break_characters HANDLE EXPR |
¿¬¼ÓÀûÀÎ field¸¦ À߶ó¾ß ÇÏ´Â °æ¿ì ±× ±âÁØÀÌ µÇ´Â characterÀÇ ÁýÇÕÀ» Á¤ÀÇÇÏ´Â º¯¼öÀÌ´Ù. default·Î "\n-", ´Ù½Ã ¸»Çؼ newline°ú hyphenÀÌ »ç¿ëµÈ´Ù. |
$^A |
$ACCUMULATOR |
format lineÀ» À§ÇÑ write accumulatorÀÇ ÇöÀç °ªÀ» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. |
$# | $OFMT |
Perl5¿¡¼´Â »ç¿ëµÇÁö ¾Ê´Â, ÀÌÀü ¹öÀü°úÀÇ È£È¯¼ºÀ» À§ÇØ Á¦°øµÇ´Â º¯¼ö·Î, ¼ýÀÚ¸¦ Ãâ·ÂÇϱâ À§ÇÑ output formatÀ» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. Ãʱ⠰ªÀº %.14g ÀÌ´Ù. |
$? | $CHILD_ERROR |
¸¶Áö¸· ½ÇÇà ¹®¿¡¼ returnµÇ´Â status°ªÀÌ´Ù. »óÀ§ 8ºñÆ®´Â child processÀÇ exit valueÀÌ°í, ÇÏÀ§ 8ºñÆ®´Â ¾î¶² signalÀ» ¹Þ¾Ò´ÂÁö¿Í core dump°¡ ÀϾ´ÂÁöÀÇ ¿©ºÎ¿¡ ´ëÇÑ Á¤º¸¸¦ Æ÷ÇÔÇÑ´Ù. |
$! | $OS_ERROR $ERRNO |
error¿¡ °üÇÑ Á¤º¸¸¦ Æ÷ÇÔÇÏ´Â º¯¼ö·Î¼, »ç¿ëµÇ´Â ¹®¸Æ¿¡ µû¶ó error ¹øÈ£ ¶Ç´Â error stringÀ» º¸¿©ÁÖ°Ô µÈ´Ù. |
$@ | $EVAL_ERROR |
eval ¸í·ÉÀ¸·ÎºÎÅÍ ¹ß»ýÇÏ´Â error message¸¦ ´ã´Â º¯¼öÀÌ´Ù. null·Î °ªÀÌ ÁöÁ¤µÉ °æ¿ì ¼º°øÀûÀ¸·Î ½ÇÇàµÇ¾úÀ½À» ÀǹÌÇÑ´Ù. |
$$ | $PROCESS_ID $PID |
ÇöÀç script¸¦ ½ÇÇàÇÏ°í ÀÖ´Â PerlÇÁ·Î±×·¥ÀÇ process id(¹øÈ£)¸¦ Áö´Ï°í ÀÖ´Â º¯¼öÀÌ´Ù. |
$< | $REAL_USER_ID $UID |
ÇöÀç processÀÇ real user id(uid)¸¦ Áö´Ñ º¯¼ö·Î¼, process¸¦ ½ÇÇà½ÃŲ »ç¿ëÀÚÀÇ id¸¦ ÀǹÌÇÑ´Ù. |
$> | $EFFECTIVE_USER_ID $EUID |
ÇöÀç processÀÇ effective user id(euid)¸¦ Áö´Ñ º¯¼ö·Î¼, processÀÇ ¿ø·¡ ¼ÒÀ¯ÀÚÀÇ id¸¦
ÀǹÌÇÑ´Ù. ÀϹÝÀûÀÎ processÀÇ °æ¿ì, uid¿Í °°Àº °ªÀ» °¡ÁöÁö¸¸, setuid bit°¡ ÄÑÁ® ÀÖ´Â ½ÇÇàÆÄÀÏÀÇ °æ¿ì,
uid´Â ½ÇÇàÀÚÀÇ id·Î, euid´Â ÆÄÀÏ ¼ÒÀ¯ÀÚÀÇ °ÍÀ¸·Î ÁöÁ¤µÈ´Ù. |
$( | $REAL_GROUP_ID $GID |
ÇöÀç processÀÇ real group id(gid)¸¦ ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. |
$) | $EFFECTIVE_GROUP_ID $EGID |
ÇöÀç processÀÇ effective group id(egid)¸¦ ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. |
$0 | $PROGRAM_NAME |
ÇöÀç ½ÇÇàÁßÀÎ Perl scriptÀÇ À̸§À» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. |
$[ | |
¹è¿ÀÇ Ã¹ ¹ø° ¿ø¼ÒÀÇ index, ¶Ç´Â substringÀÇ Ã¹ ¹ø° ±ÛÀÚÀÇ index¸¦ ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. ±âº»ÀûÀ¸·Î´Â 0¹ø ¿ø¼ÒºÎÅÍ ¹è¿ÀÌ ½ÃÀÛÇÏÁö¸¸, 1¹ø ¿ø¼ÒºÎÅÍ ¹è¿ÀÌ ½ÃÀÛÇϵµ·Ï 1·Î °ªÀ» ¹Ù²Ü ¼ö ÀÖ´Ù. |
$] | $PERL_VERSION |
PerlÀÇ version°ú patch levelÀ» ¾Ë·ÁÁÖ´Â º¯¼ö·Î¼, 5.004´Â 5¹ø° version¿¡ 4¹ø° patch levelÀÓÀ» ÀǹÌÇÑ´Ù. |
$^D | $DEBUGGING |
debugging flag(-D switch)°¡ ÄÑÁ® ÀÖ´ÂÁö¿¡ °üÇÑ Á¤º¸¸¦ ´ã°í ÀÖ´Â º¯¼öÀÌ´Ù. |
$^F | $SYSTEM_FD_MAX |
ÇöÀç openµÇ¾î »ç¿ëµÇ°í ÀÖ´Â file descriptorÀÇ ÃÖ´ë ¹øÈ£¸¦ ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. º¸Åë ÇÁ·Î±×·¥ÀÌ ½ÃÀÛÇÏ°Ô µÇ¸é ÀÚµ¿À¸·Î 0, 1, 2¹øÀÌ STDIN, STDOUT, STDERR·Î ¼³Á¤µÈ´Ù. |
$^H | |
Perl compilerÀÇ ³»ºÎ ÄÄÆÄÀÏ ÈùÆ®¿¡ °üÇÑ Á¤º¸¸¦ ´ã´Â º¯¼öÀÌ´Ù. |
$^I | $INPLACE_EDIT |
inplace-edit extensionÀÇ °ªÀ» ÀúÀåÇÏ´Â º¯¼öÀÌ´Ù. ÀÌ °ªÀº -i switch¿¡ ÀÇÇØ ÁöÁ¤µÈ´Ù. |
$^O | $OSNAME |
ÇöÀç »ç¿ëµÇ°í ÀÖ´Â Operating SystemÀÇ À̸§À» ÀúÀåÇÏ´Â º¯¼öÀÌ´Ù. |
$^P | $PERLDB |
Perl debugger°¡ ÀÚ½ÅÀ» debugÇÏÁö ¾Êµµ·Ï ²¨ÁÖ´Â ³»ºÎ flag º¯¼öÀÌ´Ù. |
$^T | $BASETIME |
script°¡ ½ÇÇàµÇ±â ½ÃÀÛÇÑ ½Ã°¢À» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. Unix system¿¡¼´Â 70³â 1¿ù 1ÀÏ 0½Ã 0ºÐ 0ÃÊ(epoch)·ÎºÎÅÍÀÇ, ÃÊ´ÜÀ§ ½Ã°£ÀÌ´Ù. |
$^W | $WARNING |
-w switch¿¡ ÀÇÇØ ÁöÁ¤µÇ´Â warning(°æ°í) ¿©ºÎÀÇ °ªÀ» ÀúÀåÇÏ´Â º¯¼öÀÌ´Ù. |
$^X | $EXECUTABLE_NAME |
Perl ¹ÙÀ̳ʸ®°¡ ½ÇÇà ½Ã¿¡ °¡Áö°Ô µÇ´Â À̸§À» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. |
$ARGV | |
argument·Î ³Ñ°ÜÁ® µé¾î¿Â ÆÄÀÏÀÇ À̸§ÀÌ´Ù. |
´ÙÀ½ÀÇ ¿¹Á¦´Â µ¿ÀÏÇÑ Àǹ̸¦ °¡Áö´Â ÄÚµåÀÌ´Ù.
$foo{$a, $b, $c}
$foo{join($;, $a, $b, $c)}
d) Global special array
@ARGV |
³Ñ°ÜÁ® µé¾î¿Â command-line argumentÀÇ list¸¦ ÀúÀåÇÏ´Â º¯¼öÀÌ´Ù. |
@INC |
do, require, useµîÀÇ operator¸¦ »ç¿ëÇÒ ¶§ ÇÊ¿äÇÑ Perl script¸¦ ã´Â µð·ºÅ丮¸¦ ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù. -I switch¸¦ »ç¿ëÇÏ¿© ÁöÁ¤µÈ´Ù. lib moduleÀ» »ç¿ëÇÏ¿©µµ ÁöÁ¤ °¡´ÉÇÏ´Ù. |
@F |
-a switch¸¦ »ç¿ëÇÑ °æ¿ì¿¡, input lineÀ» ºÐ¸®ÇØ ³Ö¾îÁÙ º¯¼öÀÇ array¸¦ ÁöÁ¤ÇÏ´Â list º¯¼öÀÌ´Ù. |
%INC |
module·Î »ç¿ëµÉ Perl scriptÀÇ À̸§°ú ±× scriptÀÇ Àý´ë°æ·Î¸¦ ÀúÀåÇسõÀº hash º¯¼öÀÌ´Ù. |
%ENV |
shell¿¡¼ ³Ñ°ÜÁ® ¿Â hash typeÀÇ È¯°æº¯¼öÀÌ´Ù. ȯ°æº¯¼öÀÇ À̸§°ú ±× °ªÀ» hashÇØ ³õ¾Ò´Ù. |
%SIG |
signal°ú ±×¿¡ ÇØ´çÇÏ´Â signal handler¸¦ ÁöÁ¤ÇÏ´Â hash º¯¼öÀÌ´Ù. |
´ÙÀ½Àº ȯ°æ º¯¼ö¸¦ ÁöÁ¤ÇÏ´Â ¿¹Á¦ÀÌ´Ù.
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin";
signal handler¸¦ »ç¿ëÇÏ´Â ¿¹Á¦¸¦ Âü°íÇϵµ·Ï Ç϶ó. Ctrl-C¸¦ ´·¯ interrupt¸¦ °É°Ô µÇ¸é ÇÁ·Î±×·¥ÀÌ
Áß´ÜµÇ°Ô µÇ´Âµ¥ INT signalÀ» °¡·Îä¼ ƯÁ¤ÇÑ ÇÔ¼ö(signal handler)¸¦ ½ÇÇà½ÃÅ°µµ·Ï ÇÒ ¼ö ÀÖ´Ù.
sub sighandler {
local($sig) = @_;
print "Closing log file...\n";
close(LOG);
exit(0);
}
$SIG{'INT'} = 'handler';
e) Global special Filehandles
ARGV |
@ARGV¿¡¼ ÀúÀåµÈ argument·Î ³Ñ°ÜÁø fileµéÀ» ´Ù·ç´Â filehandleÀÌ´Ù. ´Â <>·Î ÁÙ¿©¼ »ç¿ë °¡´ÉÇÏ´Ù.
| STDERR |
standard error¿¡ ´ëÇÑ filehandleÀÌ´Ù.
| STDIN |
standard input¿¡ ´ëÇÑ filehandleÀÌ´Ù.
| STDOUT |
standard output¿¡ ´ëÇÑ filehandleÀÌ´Ù.
| DATA |
Perl script¿¡¼ __END__¶ó´Â tokenµÚÂÊ¿¡ ³ª¿À´Â ¸ðµç ÀÚ·á¿¡ ´ëÇÑ filehandleÀÌ´Ù.
| _(underline) |
¸¶Áö¸·À¸·Î ´Ù·ç¾ú´ø ÆÄÀÏ¿¡ ´ëÇÑ Á¤º¸¸¦ cache·Î ÀúÀåÇÏ°í ÀÖ´Â filehandleÀÌ´Ù.
|
6) ·¹ÆÛ·±½º(reference)
'·¹ÆÛ·±½º'¶õ ¿ì¸®¸»·Î ¿Å±âÀÚ¸é 'ÂüÁ¶'¶ó°í ÇÒ ¼ö ÀÖ´Ù. ¾î¶² º¯¼ö°¡ Á¸ÀçÇÏ°í ±× À̸§À» ÀúÀåÇÔÀ¸·Î½á ¿ø·¡ÀÇ º¯¼ö¸¦
'ÂüÁ¶'ÇÏ´Â ´Ù¸¥ º¯¼ö°¡ Á¸ÀçÇÑ´Ù¸é ±× º¯¼ö¸¦ symbolic reference¶ó°í ÇÑ´Ù. ¹Ý¸é¿¡ hard reference¶ó´Â
°Íµµ Àִµ¥, ÀÌ°ÍÀº À̸§À» ÅëÇؼ°¡ ¾Æ´Ï¶ó ½ÇÁ¦ÀÇ °ªÀ» ÅëÇؼ ÂüÁ¶ÇÏ°Ô µÈ´Ù. ¿©±â¿¡¼´Â hard reference¿¡ ´õ
ÁßÁ¡À» µÎ¾î ÀÚ¼¼È÷ ¼³¸íÇÏ·Á°í ÇÑ´Ù.
reference¿Í °ü·ÃµÈ ¿ë¾îµé¿¡ ´ëÇؼ Àá±ñ ¼³¸íÀ» ÇÏ°í ³Ñ¾î°¡ÀÚ. reference¸¦ ¸¸µå´Â °ÍÀ» refereceÇÑ´Ù°í
Çϸé, ¹Ý´ëÀÇ °úÁ¤, Áï ÂüÁ¶ ´ë»óÀÌ µÇ´Â º¯¼ö³ª »ó¼öÀÇ °ªÀ» ²¨³»¿À´Â °ÍÀ» dereferenceÇÑ´Ù°í ÇÑ´Ù.
referenceÀÇ ´ë»óÀÌ µÇ´Â °ÍÀ» thingy(referent)¶ó°í ºÎ¸¥´Ù. PerlÀÇ ÀúÀÚÀÎ Larry WallÀÌ
thingy¶ó´Â Ç¥ÇöÀ» °íÁýÇϹǷΠ¿©±â¼µµ thingy¶ó´Â Ç¥ÇöÀ» »ç¿ëÇϵµ·Ï ÇÏ°Ú´Ù.
a) hard reference
hard reference°¡ °¡¸®Å°´Â ´ë»óÀ» Perl¿¡¼´Â ÈçÈ÷ thingy¶ó°í ÁöĪÇϴµ¥, ÀÌ thingy¿¡ µû¶ó, hard
reference¸¦ ¸¸µå´Â ¹æ¹ýÀº Á¶±Ý¾¿ ´Þ¶óÁø´Ù. ´ë»óÀº ¾î¶² °ªÀÌ¶óµµ °¡´ÉÇѵ¥, ¿©±â¼´Â À̸§ ÀÖ´Â º¯¼ö(named
variable)¿Í ¼ºê·çƾ(named subroutine), À̸§ ¾ø´Â ¹è¿(anonymous array), À̸§ ¾ø´Â
hash(anonymous hash), À̸§ ¾ø´Â ¼ºê·çƾ(anonymous subroutine),
ÆÄÀÏÇÚµé(filehandle)¿¡ ´ëÇؼ »ìÆ캸±â·Î ÇÏÀÚ.
¿ì¼± ´ë°³ÀÇ º¯¼ö¿Í ¼ºê·çƾÀº À̸§À» °¡Áø´Ù. ±×·¯¹Ç·Î ÀÌ·¯ÇÑ ÀϹÝÀûÀÎ º¯¼ö³ª ¼ºê·çƾ¿¡ ´ëÇÑ reference°¡ ¸¹ÀÌ ÇÊ¿äÇÏ°Ô µÇ¸ç, hard reference´Â \(backslash)¿¬»êÀÚ¸¦ »ç¿ëÇÔÀ¸·Î½á ¸¸µé ¼ö ÀÖ´Ù.
$scalarref = \$foo; # $foo´Â ÀϹݺ¯¼ö, \´Â referenceÇÏ´Â ¿¬»êÀÚ
$constref = \3.24; # hard reference´Â °ª¿¡ ´ëÇÑ referenceÀÌ´Ù.
$arrayref = \@array1;
$hashref = \%hash2;
$globref = \*STDOUT; # *´Â typeglobÀ̸ç, ¸ðµç ŸÀÔÀ» °¡¸®Å²´Ù.
$subref = \&subroutine3;
°£´ÜÈ÷ ÀϹÝÈÇغ¸¸é, ¾î¶² ÇüÅÂÀÇ º¯¼ö³ª »ó¼ö¿¡ ´ëÇؼ ±× ¾Õ¿¡ \¿¬»êÀÚ¸¦ ºÙ¿©ÁÖ¸é hard reference°¡ µÇ´Â °ÍÀÌ´Ù.
reference¸¦ ÅëÇؼ ±× °ªÀ» »ç¿ëÇϱâ À§Çؼ´Â $, &, @, % ¿¬»êÀÚ ÁßÀÇ Çϳª¸¦ referenceº¯¼ö ¾Õ¿¡
ºÙ¿©ÁÖ¸é µÈ´Ù. reference´Â ±× ´ë»ó(thingy)°¡ ¾î¶² ŸÀÔÀÎÁö¸¦ ½Å°æ ¾²Áö ¾Ê±â ¶§¹®¿¡, »ç¿ëÀÚ°¡ ŸÀÔ Ä³½ºÆÃÀ»
ÇϵíÀÌ Å¸ÀÔ¿¬»êÀÚÀÎ $, %, &, @ µîÀÇ ¿¬»êÀÚ¸¦ ÁöÁ¤ÇÏ´Â °ÍÀÌ ÇʼöÀûÀÌ´Ù.
print $$scalarref; # print $foo;¿Í µ¿ÀÏÇÏ´Ù.
$$constref # 3.24¿Í µ¿ÀÏÇÏ´Ù.
$$hashref{'key1'} # $hash2{'key1'}°ú µ¿ÀÏÇÏ´Ù.
&$subref; # subroutine3¸¦ È£ÃâÇÏ°Ô µÈ´Ù.
shift(@$arrayref); # @array1¿¡ ´ëÇØ shift¿¬»êÀ» ÇÏ´Â °Í°ú °°´Ù.
referenceº¯¼ö¸¦ ´Ù½Ã referenceÇÒ ¼ö Àִµ¥, ÀÌ·² ¶§¿¡´Â ÇÊ¿äÇÑ ¸¸Å \¿¬»êÀ» ºÙ¿© ÁÙ ¼ö ÀÖ°í, °ªÀ» ²¨³¾ ¶§¿¡´Â ŸÀÔ¿¬»êÀÚÀÇ ¼ö°¡ \¿¬»êÀÚÀÇ ¼öº¸´Ù Çϳª ´õ ¸¹Àº°¡¸¦ È®ÀÎÇØ¾ß ÇÑ´Ù.
$multiref = \\\\123; # 123¿¡ ´ëÇØ \¿¬»êÀ» 4¹ø ½ÃÇà
print $$$$$multiref; # $multiref¿¡ ´ëÇؼ $¿¬»êÀ» 4¹ø ½ÃÇà
À̹ø¿¡´Â À̸§ ¾ø´Â ¹è¿°ú ÇØ½Ã¿Í ¼ºê·çƾÀ» ÂüÁ¶ÇÏ´Â hard reference¸¦ ´Ù·ç´Â ¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ. ¿©±â¼
À̸§ÀÌ ¾ø´Ù´Â Ç¥ÇöÀº, ÀÌ¹Ì ¸¸µé¾îÁ®¼ À̸§À» °¡Áö°í ÀÖ´Â º¯¼ö³ª »ó¼öÀÇ °ªÀ» ÂüÁ¶ÇÏ´Â °Ô ¾Æ´Ï¶ó reference¸¦ ¸¸µé¸é¼
±×°ÍÀÌ ÂüÁ¶ÇÏ´Â thingy¸¦ ¸¸µé±â ¶§¹®¿¡ À̸§À» Áö¾îÁÖÁö ¸øÇß´Ù´Â ÀǹÌÀÌ´Ù.
$arrayref = [1, 2, [3, 4, 5]]; # anonymous array reference
$hashref = {
'Meg' => 'Ryan', # anonymous hash reference
'Billy' => 'Christal'
};
$subref = sub {
print "When Harry met Sally\n"; # anonymous subroutine reference
};
À§ÀÇ ¿¹Á¦¸¦ »ìÆ캸¸é, reference´Â º¸ÀÌÁö ¾Ê°í, º¸Åë variable¿¡ °ªÀ» ´ëÀÔÇØ ÁØ °Íó·³ º¸ÀδÙ. ±×·¯³ª ½ÇÁ¦
assignment´Â ´ÙÀ½°ú °°´Ù. Àß ºñ±³Çغ¸µµ·Ï ÇÏÀÚ. ¿¬»êÀÚ·Î »ç¿ëµÇ´Â ±âÈ£µéÀÌ ¾à°£¾¿ ´Ù¸£´Ù´Â °ÍÀ» ¾Ë°Ô µÉ °ÍÀÌ´Ù.
@array = (1, 2, (3, 4, 5));
%hash = (
'Meg' => 'Ryan', # anonymous hash reference
'Billy' => 'Christal'
);
&subroutine1;
sub subroutine1 {
print "Sleepless in Seattle\n"; # anonymous subroutine reference
};
µÎ ¿¹Á¦¸¦ ºñ±³ÇØ º» ¹Ù¿Í °°ÀÌ, anonymous array reference´Â [ ]À», anonymous hash
reference´Â { }¸¦, anonymous subroutine reference´Â sub { }¸¦ ¿¬»êÀÚ·Î »ç¿ëÇÑ´Ù´Â °ÍÀ»
¾Ë ¼ö ÀÖ´Ù.
b) hard reference in nested data structure
$¿¬»êÀÚ·Î dereferenceÇÏ´Â °Í ÀÌ¿Ü¿¡µµ, ->¿¬»êÀÚ(arrow operator)¸¦ »ç¿ëÇؼ array³ª hashÀÇ
°ªÀ» dereferenceÇÏ´Â ¹æ¹ýµµ Àִµ¥, ÀÌ·¯ÇÑ ¹æ¹ýµéÀº '¹è¿ÀÇ ¹è¿'À» ±¸ÇöÇÒ ¶§ ±ä¿äÇÏ°Ô ¾²ÀδÙ. ´ÙÀ½Àº °°Àº È¿°ú¸¦
³»´Â ¿¬»êÀÚ »ç¿ë¿¡ ´ëÇÑ ¿¹ÀÌ´Ù.
$$arrayref[0] = "first";
${$arrayref}[0] = "first";
$arrayref->[0] = "first";
$$hashref{'key2'} = "McLean";
${$hashref}{'key2'} = "McLean";
$hashref->{'key2'} = "McLean";
->¿¬»êÀÚ´Â dereferenceÀÇ ±â´ÉÀ» $¿¬»êÀÚº¸´Ù ´õ Á÷°üÀûÀ¸·Î Á¦½ÃÇϱ⠶§¹®¿¡ CÇÁ·Î±×·¡¹Ö¿¡ Àͼ÷ÇÑ »ç¿ëÀÚ¿¡°Ô
À¯¿ëÇÒ °ÍÀÌ´Ù. ´ÙÀ½Àº ->¿¬»êÀÚ¸¦ ÀÌ¿ëÇÑ 1Â÷¿ø ¹è¿ÀÇ ¿ø¼Ò¸¦ dereferenceÇÏ°í, '¹è¿ÀÇ ¹è¿'³»ÀÇ ¿ø¼Ò¸¦
dereferenceÇÏ´Â ¹æ¹ýÀ» º¸ÀÎ ¿¹ÀÌ´Ù.
$arrayref->[3] = "list-item1";
$arrayref->[3]->[4] = "multi-dimensional-item2";
$arrayref->[3][4] = "multi-dimensional-item2";
$$arrayref[3][4] = "multi-dimensional-item2";
CÇÁ·Î±×·¡¸Ó¿¡°Ô´Â '¹è¿ÀÇ ¹è¿'À» ³ªÅ¸³»´Â À§ÀÇ Ç¥Çö Áß¿¡¼ ¸¶Áö¸· °ÍÀÌ Ä£¼÷ÇØ º¸ÀÏ ¼ö ÀÖÀ¸³ª, Perl¿¡¼´Â []¿¬»êÀÚ°¡
¿ì¼± ¼øÀ§(precedence)°¡ ³·¾Æ¼ reference°¡ ¾î¶»°Ô »ç¿ëµÇ°í ÀÖ´ÂÁö¸¦ ¾Ë¾Æº¸±â°¡ ¾î·Æ´Ù´Â ÀÌÀ¯·Î ±ÇÇÏÁö ¾Ê´Â
¹æ¹ýÀÌ´Ù. ±× À§ÀÇ µÎ °¡Áö ¹æ¹ýÀ» ÀÌ¿ëÇÏ¿© ´ÙÁß ¹è¿À» ³ªÅ¸³»±â·Î ÇÑ´Ù.
$ref_list_of_list = [
["separator", "delimiter", "terminator"],
["long", "short", "int", "signed", "unsigned"],
["physics", "chemistry", "computer"],
];
print $ref_list_of_list[0][2]; # terminator°¡ Ãâ·ÂµÈ´Ù.
print $ref_list_of_list->[2][1]; # electronics°¡ Ãâ·ÂµÈ´Ù.
print $ref_list_of_list->[1]->[3]; # signed°¡ Ãâ·ÂµÈ´Ù.
¸¸¾à for loop¸¦ »ç¿ëÇؼ ¸ðµç ¿ø¼Ò¸¦ Ãâ·ÂÇϱ⸦ ¿øÇÑ´Ù¸é, °¢ sub list°¡ °¡Áö°í ÀÖ´Â ¿ø¼ÒÀÇ °³¼ö¸¦ ¾Ë¾Æ¾ß
Çϴµ¥, length()ÇÔ¼ö¸¦ ½á¼ ¿ø¼ÒÀÇ °³¼ö¸¦ ±¸ÇØ ³»°Å³ª, $#¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© ¸®½ºÆ®º¯¼öÀÇ ¸¶Áö¸· À妽º¸¦ ¾òÀ» ¼ö ÀÖ´Ù.
b) symbolic reference
symbolic reference´Â reference°¡ °¡¸®Å°°í ÀÖ´Â(¿©±â¼´Â thingy°¡ ¾Æ´ÔÀ» À¯ÀÇÇÒ °Í) º¯¼öÀÇ À̸§À»
¹®ÀÚ¿·Î Çؼ ÀúÀåÇÏ°Ô µÈ´Ù. ´Ù½Ã ¸»Çؼ º¯¼öÀÇ À̸§À» °¡Áø´Ù´Â °ÍÀº symbolic reference¸¦ ¸¸µå´Â °ÍÀÌ´Ù.
hard reference°¡ thingy¸¦ Á÷Á¢ Âü°íÇÏ´Â °Í¿¡ ¹ÝÇؼ symbolic reference´Â º¯¼öÀÇ À̸§À» ÅëÇؼ
±× °ª¿¡ Á¢±ÙÇÏ°Ô µÈ´Ù. ±×·¯¹Ç·Î Ç×»ó named variable¿¡ ´ëÇؼ¸¸ symbolic reference¸¦ ¸¸µé ¼ö ÀÖ´Â
°ÍÀÌ´Ù.
symbolic reference¸¦ ¸¸µå´Â °ÍÀº ´Ü¼øÈ÷ º¯¼öÀÇ À̸§À» ÁöÁ¤Çϱ⸸ ÇÏ¸é µÇ¸ç, ±× »ç¿ë¹ýµµ hard reference¿Í ´Ù¸£Áö ¾Ê´Ù. ŸÀÔ¿¬»êÀÚ¸¦ ¾Õ¿¡ ºÙ¿©ÁÖ¸é ±× °ªÀ» ÃëÇÒ ¼ö ÀÖ°Ô µÈ´Ù.
$var1 = "test";
$sr = "var1";
$$sr = "verify"; # $var1 eq "verify"
@var2 = ("hello", "program", "world");
$sr = "var2";
push(@$sr, "perl"); # @var2 eq ('hello', 'program', 'world', 'perl')
7) ¹è¿ÀÇ ¹è¿, ÇؽÃÀÇ ¹è¿, ¹è¿ÀÇ ÇؽÃ
¹è¿ÀÇ ¹è¿Àº »ç½Ç»ó Perl¿¡¼ Á¦°øÇÏ·Á°í ÀǵµÇÏ´Â °ÍÀº ¾Æ´Ï´Ù. PerlÀº ´Ü¼øÈ÷ 1Â÷¿øÀÇ ¹è¿¸¸À» Á¦°øÇϴµ¥, C¿Í´Â ´Þ¸®
scalar°ªÀÌ ³õÀÏ À§Ä¡¿¡ vectorÇü º¯¼ö°¡ ³õÀÏ ¼ö ÀÖÀ¸¹Ç·Î ÀÚ¿¬½º·´°Ô ¹è¿ÀÇ ¹è¿, ÇؽÃÀÇ ¹è¿, ¹è¿ÀÇ Çؽà µî°ú
°°Àº º¹ÇÕÀûÀÎ data structure¸¦ ¸¸µé ¼ö ÀÖ´Ù.
a) ¹è¿ÀÇ ¹è¿
´ÙÀ½°ú °°Àº Äڵ带 ½ÇÇà½ÃÄÑ º¸ÀÚ. ¾Æ¸¶ ¾Æ¹« Ãâ·Âµµ ¾øÀ» °ÍÀÌ´Ù. ±× ÀÌÀ¯´Â PerlÀÌ 1Â÷¿ø ¹è¿¸¸À» Á¦°øÇϱ⠶§¹®¿¡
$list_of_list[0][2]¿Í °°Àº Çü½ÄÀº ¹«½ÃµÇ±â ¶§¹®ÀÌ´Ù.(-w ½ºÀ§Ä¡¸¦ »ç¿ëÇϸé warningÀ» º¼ ¼ö ÀÖ´Ù.)
@list_of_list = (
("separator", "delimiter", "terminator"),
("long", "short", "int", "signed", "unsigned"),
("physics", "chemistry", "computer"),
);
print $list_of_list[0][2];
print $list_of_list[2][1];
print $list_of_list[1][3];
2Â÷¿ø ¹è¿À» ±¸ÇöÇϱâ À§Çؼ´Â reference¸¦ ÀÌ¿ëÇØ¾ß ÇÑ´Ù.
@list_of_list = (
["separator", "delimiter", "terminator"],
["long", "short", "int", "signed", "unsigned"],
["physics", "chemistry", "computer"],
);
print $list_of_list[0][2]; # terminator°¡ Ãâ·ÂµÈ´Ù.
print $list_of_list[2][1]; # electronics°¡ Ãâ·ÂµÈ´Ù.
print $list_of_list[1][3]; # signed°¡ Ãâ·ÂµÈ´Ù.
b) ¹è¿ÀÇ ÇؽÃ
¸®½ºÆ®¸¦ Çؽà °ªÀ¸·Î »ç¿ëÇÏ¿© Çؽø¦ ¸¸µå´Â °Íµµ ºñ½ÁÇÑ ¹æ¹ý¿¡ ÀÇÇØ °¡´ÉÇÏ´Ù.
%hash_of_list = (
token => ["separator", "delimiter", "terminator"],
type => ["long", "short", "int", "signed", "unsigned"],
science => ["physics", "chemistry", "computer"],
);
print $hash_of_list{'token'}->[1]; # delimiter°¡ Ãâ·ÂµÈ´Ù.
print $hash_of_list{'type'}[0]; # longÀÌ Ãâ·ÂµÈ´Ù.
print $hash_of_list{science}; # ARRAY(0xb2074)°°Àº Á¤º¸°¡ Ãâ·ÂµÈ´Ù.
print $hash_of_list{science}->[2]; # computerÀÌ Ãâ·ÂµÈ´Ù.
print @$hash_of_list{science}; # ¾Æ¹« °Íµµ Ãâ·ÂµÇÁö ¾Ê´Â´Ù.
print @{$hash_of_list{science}}; # physicschemistrycomputer Ãâ·Â
c) ÇؽÃÀÇ ÇؽÃ
%hash_of_hash = (
token => {
s => "separator",
d => "delimiter",
t => "terminator"
},
type => {
l => "long",
s => "short",
i => "int"
},
science => {
e => "chemistry",
c => "computer",
},
);
print $hash_of_hash{token}->{s}; # separator Ãâ·Â
print $hash_of_hash{type}{i}; # int Ãâ·Â
print $hash_of_hash{science}; # HASH(0xb205c)°°Àº Á¤º¸°¡ Ãâ·ÂµÈ´Ù.
´Ù¸¸ ÁÖÀÇÇÒ °ÍÀº Çؽóª ¸®½ºÆ®´Â ÇؽÃÀÇ Å°(key)·Î »ç¿ëÇÒ ¼ö ¾ø´Ù´Â °ÍÀÌ´Ù. ÀÌÀ¯´Â key´Â ´Ü¼øÇÑ ¹®ÀÚ¿·Î Ãë±ÞÀÌ µÇ±â ¶§¹®ÀÌ´Ù. ¸®½ºÆ®³ª Çؽø¦ ³Ö´Â´Ù°í Çصµ ¹®ÀÚ¿ ÀÌ»óÀÇ Àǹ̸¦ °¡ÁöÁö´Â ¸øÇÏ°Ô µÈ´Ù.
2. ½Ä(Expression)
1) ±âº» ¿¬»êÀÚ
a) ¼öÄ¡ ¿¬»êÀÚ
PerlÀº ¼öÄ¡¿¡ ´ëÇؼ »çÄ¢¿¬»êÀ» ±âº»ÀûÀ¸·Î Á¦°øÇÑ´Ù. +, -, *, /°¡ ±×¿¡ ÇØ´çÇÏ´Â ¿¬»êÀÚÀÌ´Ù. ±×¸®°í FORTRAN°ú
°°ÀÌ **¿¡ ÀÇÇÑ °ÅµìÁ¦°ö ¿¬»êµµ °¡´ÉÇÏ´Ù. ³ª¸ÓÁö¸¦ ±¸ÇÏ´Â ¿¬»êµµ Àִµ¥, %¸¦ ¿¬»êÀÚ(operator)·Î »ç¿ëÇϸç,
ÇÇ¿¬»êÀÚ(operand) ¸ðµÎ¸¦ Á¤¼ö·Î Ãë±ÞÇÑ´Ù. ¹Ý¸é¿¡ ³ª´©±â ¿¬»êÀÚÀÎ /´Â ÇÇ¿¬»êÀÚ ¸ðµÎ¸¦ ½Ç¼ö·Î Ãë±ÞÇÑ´Ù. ³í¸®¿¬»êÀÚÀÎ
<, <=, ==, >=, >, !µµ »ç¿ë °¡´ÉÇÏ´Ù.
´ÙÀ½Àº ¼öÄ¡ ¿¬»êÀÚÀÇ »ç¿ë ¿¹ÀÌ´Ù.
2 + 3 # 5
5.1 - 2.4 # 2.7
3 * 12 # 36
14 / 2 # 7
10.2 / 0.3 # 34
10 / 3 # 3.33333...
if (3 > 4) {
print "3 is greater than 4.\n";
} else {
print "3 is not greater than 4.\n"; # ºñ±³¹®ÀÌ °ÅÁþÀ̹ǷΠ¿©±â°¡ ½ÇÇàµÊ
}
b) ¹®ÀÚ¿ ¿¬»êÀÚ
¹®ÀÚ¿ ¿¬»êÀÚ¿¡´Â C¿¡¼´Â º¼ ¼ö ¾ø´Â ¿¬»êÀÚµéÀÌ ¸¹ÀÌ µîÀåÇÑ´Ù. ¿ì¼±Àº ¹®ÀÚ¿µéÀ» ºÙÀÌ´Â ¿¬»êÀÚÀÎ .(dot)ÀÌ ÀÖ°í, ¹®ÀÚ¿À» Á¤¼ö¹ø ¹Ýº¹Çؼ ºÙ¿©ÁÖ´Â x¿¬»êÀÚµµ ÀÖ´Ù. ´ÙÀ½ÀÇ ¿¹Á¦¸¦ Âü°íÇϵµ·Ï ÇÏÀÚ.
$b = "Hello, ";
$c = "world";
$a = $b . $c; # $a = "Hello, world"
$a = "boy";
$b = $a x 3; # $b = "boyboyboy"
$c = $a x 4.2; # $b = "boyboyboyboy", 4.2´Â 4·Î castµÈ´Ù.
c) ´ëÀÔ¿¬»êÀÚ(assignment operator)
´ëÀÔ¿¬»êÀÚ¿¡´Â C¿Í ¸¶Âù°¡Áö·Î =ÀÌ ±âº»ÀûÀÎ ¿¬»êÀÚÀÌ´Ù. À̹ۿ¡µµ C¿¡¼ »ç¿ëÇÏ´Â ´ëÀÔ¿¬»êÀÚµéÀº °ÅÀÇ Perl¿¡¼ äÅõǾîÁ®
ÀÖ´Ù. +=, -=, *=, /=, &=, |=. ^=, %=°¡ ±× ¿¹ÀÌ´Ù. ±× ¹Û¿¡µµ .=, x=, ||=, **=,
<<=, >>=ÀÇ ¿¬»êÀÚµéÀÌ ÀÖ´Ù. ÀÌ¿Í °°Àº ´ëÀÔ¿¬»êÀÚµéÀº %¿¬»ê, .¿¬»ê, x¿¬»ê, ||¿¬»êÀ» ¼öÇàÇÑ
ÈÄ¿¡ ±× °á°ú¸¦ ´ëÀÔÇÏ´Â °ÍÀ̹ǷΠº¸Ãæ ¼³¸íÀÌ ¾ø¾îµµ ÀÌÇØ¿¡ ¾î·Á¿òÀÌ ¾øÀ» °ÍÀÌ´Ù.
$a *= 3; # $a = $a * 3;
$b .= "\n"; # $b = $b . "\n"
$c ||= 2; # $c = $c || 2, $c°¡ 2°¡ ¾Æ´Ï¸é 2ÀÇ °ªÀ» ´ëÀÔÇÑ´Ù.
d) ¿¬»êÀÚ ¿ì¼± ¼øÀ§¿Í °áÇÕ¹ýÄ¢
¿©·¯ ¿¬»êÀÚµéÀÇ °áÇÕ¼øÀ§¿Í ¿ì¼± ¼øÀ§¸¦ ´ÙÀ½ÀÇ Ç¥·Î Á¤¸®Çسõ¾Ò´Ù. À§ÂÊ¿¡ ÀÖ´Â ¿¬»êÀÚÀϼö·Ï ¾Æ·¡ÂÊ ¿¬»êÀڵ麸´Ù ¿ì¼± ¼øÀ§°¡ ³ô´Ù. °°Àº ÁÙ¿¡ ÀÖ´Â ¿¬»êÀÚµéÀº °°Àº ¿ì¼± ¼øÀ§ °¡Áö°Ô µÇ°í °áÇÕ¼øÀ§¿¡ µû¶ó ¿¬»êÀÇ ¼ø¼°¡ Á¤ÇØÁø´Ù.
°áÇÕ¼øÀ§ |
¿¬»êÀÚ |
¾øÀ½ |
++ -- |
¿ì |
! ~ -(´ÜÇ׿¬»ê) |
¿ì |
** |
ÁÂ |
=~ !~ |
ÁÂ |
* / % x
|
ÁÂ |
+ -(ÀÌÇ׿¬»ê) . |
ÁÂ |
<< >> |
¾øÀ½ |
file test operator |
¾øÀ½ |
named unary operator |
¾øÀ½ |
< <= > >= lt le gt ge |
¾øÀ½ |
== != <=> eq ne cmp |
ÁÂ |
& |
ÁÂ |
| ^ |
ÁÂ |
&& |
ÁÂ |
|| |
¾øÀ½ |
.. |
¿ì |
?:(»ïÇ׿¬»ê) |
¿ì |
´ëÀÔ ¿¬»êÀÚ (+= -= µî) |
ÁÂ |
, |
¾øÀ½ |
list ¿¬»êÀÚ |
e) ¼öÄ¡¿Í ¹®ÀÚ¿ »óÈ£º¯È¯
PerlÀº ¼öÄ¡¿Í ¹®ÀÚ¿, Á¤¼ö¿Í ½Ç¼ö »çÀÌÀÇ º¯È¯À» ÀÚµ¿À¸·Î ÇØÁØ´Ù. ¼öÄ¡ ¾Õ µÚÂÊ¿¡ ¿À´Â ¾µµ¥¾ø´Â ¹®ÀÚµéÀ» ÀÚµ¿À¸·Î Á¦°ÅÇØÁֹǷΠ´ÙÀ½°ú °°Àº ¿¬»êÀÌ °¡´ÉÇÏ´Ù.
$a = " 3.5abcd" * 4;
print $a; # 3.5 * 4 = 14°¡ Ãâ·ÂµÈ´Ù.
$b = "3" + "4"; # $b = 7
PerlÀÌ ÀÚµ¿À¸·Î º¯È¯ÇØ ÁÖ´Â °ÍÀº ¹®ÀÚ¿ ¶Ç´Â ¼öÄ¡°¡ ÀÌÁßÀûÀÎ Àǹ̸¦ °¡Áú ¶§, ¾ÕÂÊÀ̳ª µÚÂÊ¿¡ À§Ä¡ÇÏ´Â ¿¬»êÀÚ°¡ ÇÊ¿ä·Î ÇÏ´Â ÇÇ¿¬»êÀÚ°¡ ¼öÄ¡Àΰ¡ ¹®ÀÚ¿Àΰ¡¸¦ °áÁ¤ÇÒ ¼ö Àֱ⠶§¹®ÀÌ´Ù.
print "boy" . (3 * 2); # boy6°¡ Ãâ·ÂµÈ´Ù.
print "boy" x (3 * 2); # boyboyboyboyboyboy°¡ Ãâ·ÂµÈ´Ù.
°ýÈ£ ¾ÈÀÇ 3 * 2ÀÌ ¸ÕÀú ¼öÄ¡·Î¼ °è»êÀÌ µÈ´Ù. ¼öÄ¡·Î ÀνĵǴ ÀÌÀ¯´Â *ÀÌ ¼öÄ¡¸¦ ÇÇ¿¬»êÀÚ·Î °¡Áö±â ¶§¹®ÀÌ´Ù. ´ÙÀ½¿¡´Â
¹®ÀÚ¿À» °áÇÕ½ÃÄÑÁÖ´Â . ¿¬»êÀÚ¸¦ ¸¸³ª°Ô µÇ¹Ç·Î (3 * 2)ÀÇ °á°úÀÎ 6À» ¹®ÀÚ¿·Î º¯È¯½ÃÄѼ ÀνÄÇÏ°Ô µÈ´Ù. ÀÌ·¯ÇÑ ¹æ½ÄÀ¸·Î
ÀÚµ¿º¯È¯ÀÌ ÀϾ°Ô µÇ¹Ç·Î ÇÁ·Î±×·¡¸Ó´Â »ç¼ÒÇÑ °Í¿¡ ½Å°æ ¾²Áö ¾Ê¾Æµµ µÈ´Ù.
2) Ãß°¡ ¿¬»êÀÚ
¶Ç Çϳª ÇÁ·Î±×·¡¸Ó°¡ ¸Å´Þ¸± ÇÊ¿ä°¡ ¾ø´Â °ÍÀº ¿¬»êÀÚ¸¦ ¿¬»êÀÚ·Î º¼ °ÍÀΰ¡ ÇÔ¼ö·Î º¼ °ÍÀΰ¡ÀÇ ¹®Á¦Àε¥, Perl¿¡¼ Á¦°øµÇ´Â ´ë°³ÀÇ ¿¬»êÀÚ´Â ¿¬»êÀÚÀÌ¸é¼ ÇÔ¼ö·Î º¼ ¼ö Àֱ⠶§¹®¿¡ Có·³ ()¸¦ ¹Ýµå½Ã ½á ÁÙ ÇÊ¿ä°¡ ¾ø´Ù.
a) scalar operator
rand $a |
0°ú $a »çÀÌÀÇ ÀÓÀÇÀÇ °ª(random number)À» ¹ÝȯÇÑ´Ù. 0ÀÌ»ó $a¹Ì¸¸ÀÇ ¹üÀ§ÀÇ ¼Ò¼ö¸¦ ±¸ÇÏ°Ô µÈ´Ù. |
srand $a |
random number¸¦ ¸¸µé¾î ³¾ ¶§ »ç¿ëÇÏ´Â seed°ªÀ» ÁöÁ¤ÇÑ´Ù. srand(time^$$)ó·³ ÇöÀç½Ã°£À» ¾Ë·ÁÁÖ´Â time¿¬»êÀÚ¿Í ÇöÀç ÇÁ·Î¼¼½ºÀÇ ¹øÈ£¸¦ ÁöÁ¤ÇÏ´Â $$º¯¼ö¸¦ ³Ö¾î¼ »ç¿ëÇϱ⵵ ÇÑ´Ù. |
substr $a, $b, $c |
$a¶ó´Â ¹®ÀÚ¿¿¡¼ $bÀÇ offsetÀ§Ä¡¿¡ $c°³¸¸ÅÀÇ ¿ø¼Ò¸¦ ÁöÁ¤ÇÏ°Ô µÈ´Ù. substrÀ» ÀÌ¿ëÇؼ $a¿¡ »õ·Î¿î °ªÀ» ³Ö¾îÁְųª $a¿¡¼ ¿ø¼Ò¸¦ ²¨³»¿Ã ¼ö ÀÖ´Ù. $cºÎºÐÀº »ý·« °¡´ÉÇÏ´Ù. |
index $a, $b, $c |
¹®ÀÚ¿ $a¿¡¼ $b°¡ ³ªÅ¸³ª´Â À§Ä¡(offset)À» ¾Ë·ÁÁØ´Ù. $cºÎºÐÀº ã±â ½ÃÀÛÇÏ´Â À§Ä¡¸¦ ÁöÁ¤ÇÏ´Â °ÍÀε¥, »ý·«µÇ¸é 0ÀÇ °ªÀÌ ´ë½Å »ç¿ëµÈ´Ù. |
chop $a |
chopÀº $aÀÇ ¸¶Áö¸· ±ÛÀÚ¸¦ Á¦°ÅÇÏ´Â ¿¬»êÀÚÀ̸ç Perl 5¿¡¼´Â ´ÙÀ½ÀÇ chompÀ» ±ÇÀåÇÑ´Ù. $a´Â ¸®½ºÆ®¶óµµ ó¸®ÇÒ ¼ö ÀÖ´Ù. |
chomp $a |
chomp´Â chopÀÇ ¾ÈÀüÇÑ ¹öÀüÀ̸ç, $aÀÇ ¸¶Áö¸· ±ÛÀÚ°¡ $/º¯¼ö($INPUT_RECORD_SEPARATOR, $RS)ÀÇ
°ªÀ» °¡Áö¸é Á¦°ÅÇÏ´Â ¿ªÇÒÀ» ÇÑ´Ù. ´ë°³´Â ÀԷ¿¡ ºÙ¾î¼ µé¾î¿À´Â newline character¸¦ Á¦°ÅÇÒ ¶§ »ç¿ëÇÑ´Ù. |
b) vector operator
shift @a |
¸®½ºÆ® @aÀÇ °¡Àå ¾ÕÂÊ¿¡ ÀÖ´Â ¿ø¼Ò¸¦ ²¨³»ÁØ´Ù. |
unshift @a, $b |
$b¸¦ @aÀÇ °¡Àå ¾ÕÂÊ¿¡ ¾¥¼Å ³Ö¾î ÁØ´Ù. |
pop @a |
@aÀÇ °¡Àå µÚÂÊ¿¡ ÀÖ´Â ¿ø¼Ò¸¦ ²¨³»ÁØ´Ù. |
push(@a, $b) |
@aÀÇ °¡Àå µÚÂÊ¿¡ $b¸¦ Ãß°¡ÇØÁØ´Ù. |
splice @a, $b, $c, @d; |
@aÀÇ offsetÀÌ $bµÇ´Â °÷¿¡¼ $c°³ÀÇ ¿ø¼Ò¸¦ ÁöÁ¤ÇÒ ¶§ »ç¿ëÇÑ´Ù. @d°¡ »ç¿ëµÇ´Â °æ¿ì¿¡´Â @d¸¦ $c°³ÀÇ ¿ø¼Ò¿Í ¹Ù²Ù¾îÁØ´Ù. @d°¡ »ý·«µÇ¸é $c°³ÀÇ ¿ø¼Ò¸¦ ¹ÝȯÇÑ´Ù. |
keys %a |
Çؽà º¯¼ö %aÀÇ key¸¦ ¸ð¾Æ¼ ¸®½ºÆ® ÇüÅ·Π¹ÝȯÇÑ´Ù. |
$a .. $b |
..Àº ¹üÀ§¿¬»êÀÚ(range operator)¶ó°í Çϴµ¥, $a¿¡¼ $b±îÁöÀÇ °ªÀ» ¸®½ºÆ® ÇüÅ·Π¹ÝȯÇÑ´Ù. $a¿Í $b¿¡
µé¾î°¥ ¼ö ÀÖ´Â °ªÀº Á¤¼ö³ª ¹®ÀÚÀÌ´Ù. ¹®ÀÚ¿À» »ç¿ëÇÏ¸é ±× »çÀÌ¿¡ Á¸ÀçÇÒ ¼ö ÀÖ´Â ¸ðµç ¹®ÀÚ¿ Á¶ÇÕÀ» ¸¸µé¾î Áֱ⵵ Çϴµ¥,
½Ã½ºÅÛÀÇ ÀÚ¿øÀ» ³¶ºñÇϱ⠽¬¿ì¹Ç·Î Á¶½ÉÇØ¾ß ÇÑ´Ù. |
, |
comma(,) ¿¬»êÀÚ´Â ¸®½ºÆ®¿¡¼ separator·Î »ç¿ëµÇ´Â comma¿Í´Â ´Ù¸¥ Àǹ̸¦ °¡Áø´Ù. comma ¿¬»êÀÚ´Â ,µÚÂÊÀÇ °ªÀ» ¹ÝȯÇÑ´Ù. ,·Î ¿©·¯ °³ÀÇ °ªÀÌ ³ª¿µÇ¸é ¸¶Áö¸· °ªÀÌ ¸®ÅÏ µÈ´Ù. |
sort @a |
¸®½ºÆ®º¯¼ö @a¸¦ Á¤·ÄÇØÁØ´Ù. sortÀÇ ±âÁØÀ» Á¦½ÃÇÏ´Â ºñ±³ÇÔ¼ö³ª ºí·°À» ÁöÁ¤ÇÒ ¼öµµ Àִµ¥, ¸®½ºÆ® ¾Õ¿¡ ÁöÁ¤ÇØÁØ´Ù. ÀÌ ¶§
ºñ±³ÇÔ¼ö³ª ºí·°¿¡¼´Â <=>, cmp ¿¬»êÀÚ¿Í ³í¸®¿¬»êÀÚµîÀ» »ç¿ëÇÏ¿© +1, -1, 0µîÀÇ °ªÀ» ¹ÝȯÇϵµ·Ï ÇÑ´Ù. |
reverse @a |
¸®½ºÆ®ÀÇ ¿ø¼ÒÀÇ ¼ø¼¸¦ ¹Ý´ë ¹æÇâÀ¸·Î ¹Ù²Ù¾îÁØ´Ù. |
grep PATTERN, @a
grep FILETEST, @a |
¸®½ºÆ®º¯¼ö @a¸¦ Àоîµé¿© ÆÐÅϸÅÄ¡³ª ÆÄÀÏ Å×½ºÆ®¸¦ Çؼ ¼º°øÇÒ °æ¿ì, ±× ¸®½ºÆ®ÀÇ ¿ø¼ÒÀÎ lineÀ̳ª stringÀ» ¹ÝȯÇÑ´Ù. |
join $b, @a |
@aÀÇ ¿ø¼ÒµéÀ» $b¸¦ delimiter·Î ÇÏ¿© ¹¾î ÇϳªÀÇ ¹®ÀÚ¿·Î ¹ÝȯÇÑ´Ù. |
split PATTERN, $a, $b |
¹®ÀÚ¿ $a¸¦ PATTERN¿¡ ³ª¿À´Â character¸¦ delimiter·Î Çؼ ³ª´©¾î ¸®½ºÆ®ÇüÅ·Π¹ÝȯÇÑ´Ù. $b¸¦ »ç¿ëÇÏ°Ô µÇ¸é ±× °ª¿¡ ÇØ´çÇÏ´Â °³¼ö±îÁö ¿ø¼ÒµéÀ» »ý¼ºÇØÁØ´Ù. |
c) file test operator
´ÙÀ½Àº ÆÄÀÏ¿¡ ´ëÇÑ Á¶°Ç½ÄÀ» ¸¸µå´Â operatorÀ» Á¤¸®ÇÑ Ç¥ÀÌ´Ù. ´ÙÀ½ÀÇ Å×½ºÆ®´Â Âü/°ÅÁþÀÇ °ªÀ» °¡Áö°Å³ª ƯÁ¤ÇÑ °á°ú °ªÀ» returnÇØ ÁØ´Ù.
-r |
ÆÄÀÏÀ» ÀÐÀ» ¼ö(readable) Àִ°¡? |
-w |
ÆÄÀÏÀ» ¾µ ¼ö(writable) Àִ°¡? |
-x |
ÆÄÀÏÀ» ½ÇÇà½Ãų ¼ö(executable) Àִ°¡? |
-o |
ÆÄÀÏÀÌ $euidÀÇ »ç¿ëÀÚ ¼ÒÀ¯Àΰ¡? |
-R |
ÆÄÀÏÀÌ $uidÀÇ »ç¿ëÀÚ¿¡ ÀÇÇØ ÀÐÇôÁú ¼ö Àִ°¡? |
-W |
ÆÄÀÏÀÌ $uidÀÇ »ç¿ëÀÚ¿¡ ÀÇÇØ ¾²¿©Áú ¼ö Àִ°¡? |
-X |
ÆÄÀÏÀÌ $uidÀÇ »ç¿ëÀÚ¿¡ ÀÇÇØ ½ÇÇà°¡´ÉÇÑ°¡? |
-O |
ÆÄÀÏÀÌ $uidÀÇ »ç¿ëÀÚ ¼ÒÀ¯Àΰ¡? |
-e |
ÆÄÀÏÀÌ Á¸ÀçÇϴ°¡? |
-z |
ÆÄÀÏÀÇ Å©±â°¡ 0Àΰ¡? |
-s |
ÆÄÀÏÀÇ Å©±â(size) |
-f |
ÆÄÀÏÀÌ Á¤±ÔÆÄÀÏ(µð·ºÅ丮°¡ ¾Æ´Ñ)Àΰ¡? |
-d |
ÆÄÀÏÀÌ µð·ºÅ丮Àΰ¡? |
-l |
ÆÄÀÏÀÌ symbolic linkÀΰ¡? |
-p |
ÆÄÀÏÀÌ FIFO¿Í °°Àº named pipeÀΰ¡? |
-S |
ÆÄÀÏÀÌ socketÀΰ¡? |
-b |
ÆÄÀÏÀÌ block special fileÀΰ¡? |
-c |
ÆÄÀÏÀÌ character special fileÀΰ¡? |
-t |
filehandleÀÌ tty(terminal)¿¡ ¿·Á Àִ°¡? |
-u |
setuid bitÀÌ ÄÑÁ® ÀÖ´Â ÆÄÀÏÀΰ¡? |
-g |
setgid bitÀÌ ÄÑÁ® ÀÖ´Â ÆÄÀÏÀΰ¡? |
-k |
sticky bitÀÌ ÄÑÁ® ÀÖ´Â ÆÄÀÏÀΰ¡? |
-T |
ÆÄÀÏÀÌ ÅؽºÆ®(text) ÆÄÀÏÀΰ¡? |
-B |
ÀÌÁø(binary) ÆÄÀÏÀΰ¡? |
-M |
fileÀÇ ÃÖÁ¾ ¼öÁ¤ ½Ã°£(modification time, mtime) |
-A |
fileÀÇ ÃÖÁ¾ Á¢±Ù ½Ã°£(last access time, atime) |
-C |
fileÀÇ ÃÖÁ¾ º¯°æ ½Ã°£(inode change time, ctime) |
´ÙÀ½Àº file test¸¦ ÀÌ¿ëÇÑ Á¶°Ç½ÄÀÇ ¿¹ÀÌ´Ù.
while (<>) {
chomp; # default input stringÀÇ ¸¶Áö¸· \nÀ» Á¦°ÅÇÑ´Ù.
next unless -f $_; # Á¤±ÔÆÄÀÏÀÌ ¾Æ´Ñ °æ¿ì¿¡´Â ´ÙÀ½À¸·Î ³Ñ¾î°£´Ù.
}
stat($file);
print "Readable\n" if -r _; # _´Â ¸¶Áö¸·À¸·Î »ç¿ëµÈ filehandleÀÌ´Ù.
print "Writable\n" if -w _; # readable, writable, executableÇÑÁö üũÇÑ´Ù.
print "Executable\n" if -x _;
&newfile if -M $file < 5; # mtimeÀÌ 5ÀÏ À̳»¶ó¸é ¼ºê·çƾÀ» È£ÃâÇÑ´Ù.
3) ±âº» ÀÔÃâ·Â ¿¬»êÀÚ
ÀÔÃâ·ÂÀ̶õ ÇÁ·Î±×·¥ÀÌ ÀڷḦ ¿ÜºÎ¼¼°è¿¡¼ ³Ñ°Ü¹Þ°í ±× ÀڷḦ ó¸®ÇÑ °á°ú¸¦ ¿ÜºÎ¼¼°è¿¡ ³Ñ°ÜÁÖ´Â °úÁ¤À» ÀǹÌÇÑ´Ù. Perl¿¡¼
°¡´ÉÇÑ ¹æ¹ýÀº ÆÄÀÏÀ» ÀÌ¿ëÇÑ °ÍÀÌ°í, ÇÁ·Î±×·¡¸Ó´Â filehandleÀ̶ó´Â °ÍÀ» °¡Áö°í ÆÄÀÏÀ» ´Ù·ê ¼ö ÀÖ°Ô µÈ´Ù. ÆÄÀÏÀ»
¿¾î¼ filehandleÀ» ÁöÁ¤ÇÏ´Â °ÍÀº openÇÔ¼ö¸¦ »ç¿ëÇÏ¸é µÇ°í, ´õ ÀÌ»ó »ç¿ëÇÏÁö ¾Ê°Ô µÈ ÆÄÀÏÀ» ´Ý°í
filehandleÀÇ »ç¿ëÀ» Æ÷±âÇÏ´Â °ÍÀº closeÇÔ¼ö¸¦ ÀÌ¿ëÇÏ¸é µÈ´Ù.
±×·¯³ª ÀÔÃâ·Â °úÁ¤¿¡ filehandleÀ» ¸Å¹ø »ç¿ëÇÑ´Ù´Â °ÍÀº ÇÁ·Î±×·¥ ÀÛ¼º¿¡ »ó´çÇÑ ºÒÆíÀ» ¾È°ÜÁÙ °ÍÀÌ´Ù. ±×¸®ÇÏ¿©
Perl¿¡¼´Â Unix½Ã½ºÅÛ°ú ¸¶Âù°¡Áö·Î Ç¥ÁØÀÔ·Â(STDIN), Ç¥ÁØÃâ·Â(STDOUT), Ç¥ÁØ¿¡·¯(STDERR)¸¦ Á¦°øÇÑ´Ù.
Ç¥ÁØÀÔ·ÂÀ̶õ Å°º¸µå¸¦ ÅëÇØ ÀڷḦ ÀԷ¹ްųª, ´Ù¸¥ ÇÁ·Î±×·¥¿¡¼ ÆÄÀÌÇÁ³ª redirectionÀ» ÅëÇØ ÀڷḦ ³Ñ°Ü¹Þ´Â °ÍÀÌ´Ù.
¹Ý¸é¿¡ Ç¥ÁØÃâ·ÂÀº print ¿¬»êÀ̳ª printf¸¦ ÅëÇؼ ȸéÀ¸·Î °á°ú¸¦ Ãâ·ÂÇÏ´Â °ÍÀÌ°í, Ç¥ÁØ¿¡·¯´Â ȸ鿡 ¿¡·¯¸Þ½ÃÁö¸¦
º¸¿©ÁÖ´Â °ÍÀÌ´Ù.
a) ÀÔ·Â
ÀϹÝÀûÀ¸·Î Å°º¸µå·ÎºÎÅÍÀÇ ÀÔ·ÂÀº
$input = <STDIN>;
@input = <STDIN>;
°ú °°Àº ÇüÅ·Π¹Þ¾ÆµéÀÏ ¼ö ÀÖ´Ù. $inputÀ̳ª @input¿¡´Â »ç¿ëÀÚ°¡ Å°º¸µå¸¦ ÅëÇØ ´·¶´ø ¸ðµç Å° ÀÔ·ÂÀÌ ¹®ÀÚ¿ ¶Ç´Â ¸®½ºÆ®ÀÇ ÇüÅ·ΠÀúÀåµÈ´Ù.
Perl¿¡¼ Áñ°Ü »ç¿ëµÇ´Â ÀԷ¹æ½Ä¿¡´Â ´ÙÀ½°ú °°Àº °ÍµéÀÌ ÀÖ´Ù. ªÀº Äڵ忡¼ ±ä ÄÚµå±îÁö ¸ðµÎ °°Àº È¿°ú¸¦ °¡Áö¸ç, ¹®¸ÆÀûÀ¸·Î °°Àº Àǹ̸¦ °¡Áø´Ù.
print while <STDIN>
print $_ while <STDIN>;
print $_ while definded($_ = <STDIN>);
for (;<STDIN>;) { print; }
for (;<STDIN>;) { print $_; }
while (<STDIN>) { print; }
while (<STDIN>) { print $_; }
while (defined($_ = <STDIN>)) { print $_ };
$_´Â default input stringÀ» ÀúÀåÇÏ´Â º¯¼ö·Î¼ ´ë°³ÀÇ ¿¬»êÀÚ¿¡¼ »ý·« °¡´ÉÇÏ´Ù. °Å²Ù·Î ¸»Çؼ, ¿¬»êÀÚ¸¸ ³ª¿À´Â °æ¿ì´Â ´ë°³ $_¸¦ ¿°µÎ¿¡ µÐ °ÍÀ̶ó°í º¼ ¼ö ÀÖ´Ù. ¿¡¼ µé¾î¿Â ¸ðµç ÀÔ·ÂÀº $_¿¡ ÀÚµ¿À¸·Î ÀúÀåµÈ´Ù. $_´ë½Å¿¡ ´Ù¸¥ º¯¼ö¸¦ »ç¿ëÇصµ º°·Î »ó°üÀº ¾øÀ¸³ª, »ý·«Çؼ »ç¿ëÇÒ ¼ö´Â ¾ø´Ù.
STDIN°°Àº Ç¥ÁØÀÔÃâ·Â¿¡ °ü·ÃµÈ filehandleÀº ±»ÀÌ openÀÇ °úÁ¤ÀÌ ÇÊ¿ä ¾øÀ¸³ª, ÀϹÝÀûÀÎ ÆÄÀÏ¿¡ ´ëÇÑ filehandleÀº openÀÇ °úÁ¤ÀÌ ¸í½ÃµÇ¾î¾ß ÇÑ´Ù.
open(FILE, "test.c");
$_ = <FILE>;
print;
ÀÌ ¿¹Á¦´Â test.c¶ó´Â ÆÄÀÏÀ» FILEÀ̶ó´Â filehandleÀ» ÅëÇؼ ´Ù·ç°Ô µÇ°í default input string¿¡
ÇÑ ÁÙ ÀÔ·ÂÀ» ¹Þ¾Æ¼ ±×°ÍÀ» ´Ù½Ã Ãâ·ÂÇØÁÖ´Â ÀÛ¾÷À» ÇÏ°Ô µÈ´Ù. ±×·¯³ª ´ë°³´Â ÇÑ ÁÙ¸¸ ÀÔ·ÂÀ» ¹Þ´Â °Ô ¾Æ´Ï¶ó ÆÄÀÏ Àüü¿¡
´ëÇؼ ¿©·¯ ÁÙÀÇ ÀÔ·ÂÀ» ¹Þ¾Æ¾ß ÇϹǷΠloop¾È¿¡¼ »ç¿ëÇϰųª ¸®½ºÆ®¿¡ ÀúÀåÇß´Ù°¡ shift¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© ²¨³»¾î »ç¿ëÇÒ ¼ö
ÀÖ´Ù. ±×·¯³ª ¸®½ºÆ®¿¡ ÆÄÀÏÀÇ Å« ³»¿ëÀÌ ÀúÀåµÇ´Â °ÍÀº ½Ã½ºÅÛ¿¡ ºÎÇϸ¦ ÁÖ°Ô µÇ¹Ç·Î loop¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀ» ÀÍÈ÷µµ·Ï ÇÏÀÚ.
while ($input = <FILE>) {
print "/* $input */\n";
}
ÀÌ ¿¹Á¦´Â FILEÀ» ÅëÇØ $input¿¡ ÀÔ·ÂÀ» ¹Þ°í ±× lineÀ» comment±âÈ£·Î °¨½Î´Â ÀÛ¾÷À» ¼öÇàÇÏ´Â °ÍÀÌ´Ù.
Perl ÇÁ·Î±×·¥ÀÌ filter·Î »ç¿ëµÇ´Â °Ô ¾Æ´Ï¶ó¸é ´ë°³´Â ÇÁ·Î±×·¥ÀÇ argument·Î ÆÄÀÏ À̸§À» ³Ñ°Ü¹Þ¾Æ »ç¿ëÇÏ°Ô
µÇ´Âµ¥, ÀÌ°ÍÀº ¾ÆÁÖ °£´ÜÇÏ´Ù. ´ÙÀ½Àº argument·Î ³Ñ°ÜÁø ¸ðµç ÆÄÀÏÀ» ¿¾î¼ ÇÑ ÁÙ¾¿ ÀÔ·ÂÀ» ¹Þ¾Æ¼ Ãâ·ÂÇÏ´Â ¿¹ÀÌ´Ù.
while (<>) {
print;
}
ÀÌ ÄÚµå´Â ´ÙÀ½°ú µ¿ÀÏÇÑ Àǹ̸¦ °¡Áø´Ù.
@ARGV = ('-') unless @ARGV;
while ($ARGV = shift) {
open(ARGV, $ARGV) or warn "Can't open $ARGV: $!\n";
while () {
print;
}
}
argument·Î ³Ñ°Ü¹Þ´Â ÆÄÀÏÀ̸§¿¡ ´ëÇؼ´Â ½Å°æ¾µ ÇÊ¿ä ¾øÀÌ <>¸¸ »ç¿ëÇÏ¸é °¢ ÆÄÀÏ¿¡ ´ëÇؼ openÇÏ°í ÀоîµéÀÌ´Â °úÁ¤ÀÌ ¸ðµÎ ÀÚµ¿ÀûÀ¸·Î ½ÇÇàµÇ´Â °ÍÀÌ´Ù.
b) Ãâ·Â
print¿¬»êÀ» »ç¿ëÇÏ´Â °æ¿ì¿¡´Â default·Î Ãâ·ÂÀÇ ¹æÇâÀÌ STDOUTÀ¸·Î Á¤ÇØÁø´Ù. ±×·¯¹Ç·Î ´ÙÀ½ÀÇ ¼¼ ¹®ÀåÀº °°Àº Àǹ̸¦ °¡Áø´Ù.
print "Hello world\n";
print STDOUT "Hello world\n";
print STDOUT Hello, " ", world, "\n";
STDOUTµÚ¿¡ ,¸¦ ÂïÁö ¾Êµµ·Ï ÁÖÀÇÇÏÀÚ. STDOUTÀº Ãâ·Â ´ë»óÀÌ ¾Æ´Ï¶ó Ãâ·ÂÀÇ ¹æÇâÀ̱⠶§¹®ÀÌ´Ù.
ƯÁ¤ ÆÄÀÏ¿¡ ´ëÇؼ Ãâ·ÂÀ» Çϱâ À§Çؼ´Â ¹Ì¸® ¾²±â mode·Î openÀ» ÇØ¾ß ÇÏ°í ±× ÀÌÈÄ¿¡ print ¿¬»ê¿¡ Ãâ·Â ¹æÇâÀ¸·Î ±× ÆÄÀÏÀÇ filehandleÀ» ÁöÁ¤ÇÏ¸é µÈ´Ù.
open(OUTPUT, "> output.log");
print OUTPUT "Result : All men are alive.\n");
shell¿¡¼ »ç¿ëÇÏ´ø ¹æ½ÄÀÇ tokenÀ» ÀÌ¿ëÇÑ Ãâ·Â¹æ¹ýµµ ÀÖ´Ù. ÀÌ°ÍÀ» here document ¹®¹ýÀ̶ó°í Çϸç tokenÀ»
ÀÏÁ¾ÀÇ quote·Î »ý°¢ÇÏ´Â °ÍÀÌ´Ù. ¿©·¯ ÁÙ¿¡ °ÉÃÄÁø Ãâ·Â¿¡ ´ëÇؼ ¹®¼¸¦ ¾²µíÀÌ Ãâ·ÂÇÒ ¼ö ÀÖ´Ù´Â °Ô ÀåÁ¡ÀÌ´Ù.
print OUTPUT << EOF
But most of payload are lost.
Estimation of loss are still being carried.
EOF
print ¿¬»êÀÚ´Â EOF¶ó´Â ÅäÅ«À» quote·Î °£ÁÖÇÏ°í µÎ°³ »çÀÌÀÇ ÅؽºÆ®¸¦ ÁöÁ¤µÈ Ãâ·Â¹æÇâÀ¸·Î ³»º¸³»°Ô µÈ´Ù. ÅäÅ«¿¡ µû¶ó¼ º¸´Ù Á¤±³ÇÑ Ãâ·Â ¹æ¹ýÀÌ Àִµ¥, ¿©±â¼´Â ¸î °¡Áö¸¸ ´õ »ìÆ캸±â·Î ÇÏ°Ú´Ù.
print << "" x 10
Hello, world!
ÀÌ ¿¹Á¦´Â quote°¡ null stringÀ̹ǷÎ(±×·¯¹Ç·Î ""Á¶Â÷ »ý·« °¡´ÉÇÏ´Ù.) ´ÙÀ½ ÁÙ¸¸À» Ãâ·Â ´ë»óÀ¸·Î »ï°Ô µÇ°í print ¿¬»êÀ» 10¹ø ¼öÇàÇÏ°Ô µÈ´Ù.
print << `ANYTOKEN`
id
echo hello
ANYTOKEN
tokenÀ» °¨½Î´Â °ÍÀÌ back-quote(backtick)ÀÓ¿¡ À¯ÀÇÇ϶ó. back-quote´Â ¸í·ÉÀ» ½ÇÇà½ÃÄÑÁÖ´Â ¿ªÇÒÀ»
ÇÑ´Ù. ±×·¯¹Ç·Î µÎ ÅäÅ« »çÀÌ¿¡ Á¸ÀçÇÏ´Â id¿Í echo hello¶ó´Â ¸í·ÉÀÌ ½ÇÇàµÈ °á°ú°¡ Ãâ·ÂµÇ°Ô µÈ´Ù.
c) ¿¡·¯ Ãâ·Â
¿¡·¯´Â ÆÄÀÏ·Î º¸³¾ ¼öµµ ÀÖÁö¸¸, STDERR·Î ¸ð¾Æ¼ º¸³»´Â °ÍÀÌ ÀϹÝÀûÀÌ´Ù. ±×·¯¹Ç·Î printÀÇ Ãâ·Â¹æÇâÀ» STDERR·Î ¸í½ÃÇÏ¸é µÈ´Ù.
print STDERR "Can't find such a file!\n";
4) ºñ±³ ¿¬»êÀÚ
Perl¿¡¼´Â ¹®ÀÚ¿¿¡¼ »ç¿ëµÇ´Â ³í¸®¿¬»êÀÚ¿Í ¼öÄ¡¿¡¼ »ç¿ëµÇ´Â ³í¸®¿¬»êÀÚ°¡ µû·Î Á¦°øµÈ´Ù. ´ÙÀ½ÀÇ Ç¥¸¦ ÂüÁ¶Ç϶ó.
ºñ±³ Ç¥Çö½Ä |
¼öÄ¡¿¬»êÀÚ |
¹®ÀÚ¿¿¬»êÀÚ |
°°Àº°¡? |
== |
eq |
°°Áö ¾ÊÀº°¡? |
!= |
neq |
º¸´Ù ÀÛÀº°¡? |
< |
lt |
º¸´Ù Å«°¡? |
> |
gt |
°°°Å³ª ÀÛÀº°¡? |
<= |
le |
°°°Å³ª Å«°¡? |
>= |
ge |
°°Áö ¾ÊÀº°¡? |
<=> |
cmp |
<=> ¿¬»êÀÚ¿Í != ¿¬»êÀÚÀÇ Â÷ÀÌÁ¡Àº != ¿¬»êÀÚÀÇ °á°ú´Â 1(Âü) ¶Ç´Â 0(°ÅÁþ)Àε¥ ¹ÝÇؼ, <=> ¿¬»êÀÚÀÇ °á°ú´Â ÁÂÃø ÇÇ¿¬»êÀÚ¿¡¼ ¿ìÃø ÇÇ¿¬»êÀÚ¸¦ »« °á°ú·Î -1ÀÇ °ªÀ» °¡Áú ¼ö ÀÖ´Ù.
¼öÄ¡¿¡ °üÇÑ ºñ±³¿¬»êÀÚ´Â ±»ÀÌ ¼³¸íÇÏÁö ¾Ê´õ¶óµµ CÇÁ·Î±×·¡¹ÖÀ» Á¶±ÝÀÌ¶óµµ ÇØ º» »ç¶÷À̶ó¸é ´©±¸³ª ¾Ë ¼ö ÀÖÀ¸¸®¶ó »ý°¢Çؼ
¼³¸íÀ» »ý·«ÇÏ¿´´Ù. ±×·¯³ª ¹®ÀÚ¿¿¡ °üÇÑ ºñ±³¿¬»êÀÚ´Â ´Ù¼Ò »ý¼ÒÇÒ °ÍÀÌ´Ù. stringÀ» ºñ±³ÇÏ´Â °ÍÀº, µÎ ¹®ÀÚ¿ÀÌ »çÀü¿¡
¾²¿©Á® ÀÖ´Ù¸é ¾î¶² ¹®ÀÚ¿ÀÌ »ó´ëÀûÀ¸·Î ¾ÕÂÊ¿¡ À§Ä¡Çϴ°¡¿¡ µû¶ó °áÁ¤µÈ´Ù. »çÀü¿¡ ¾²ÀÌÁö ¾Ê´Â ±ÛÀÚ¶ó¸é ASCIIÄÚµå °ª¿¡ µû¶ó
°áÁ¤µÇ°Ô µÈ´Ù. ´ÙÀ½Àº ÂüÀÎ Á¶°Ç½ÄÀÌ´Ù.
"" lt "1"
"1" lt "A"
"A" lt "a"
¼ýÀÚ´Â ¿µ¹®ÀÚº¸´Ù ¾ÕÂÊ¿¡ À§Ä¡ÇÏ°í ´ë¹®ÀÚ°¡ ¼Ò¹®ÀÚº¸´Ù ¾ÕÂÊ¿¡ À§Ä¡ÇÑ´Ù. null character°¡ ´Ù¸¥ ¾î¶² ±ÛÀڵ麸´Ù ¾Õ¼±â ¶§¹®¿¡ ´ÙÀ½°ú °°Àº Á¶°Ç½Äµµ ÂüÀÓÀ» ¾Ë ¼ö ÀÖ´Ù.
"Korea" lt "Korean"
±×¸®°í ´ÙÀ½ÀÇ ¿¹Á¦´Â ¹®ÀÚ¿¿¡ °ü°èµÇ´Â ³í¸®¿¬»êÀÚ¸¦ »ç¿ëÇÑ ¿¹ÀÌ´Ù.
$a = "Jones"; # ClintonÀ̶ó´Â ´Ü¾î°¡ Jones¶ó´Â ´Ü¾îº¸´Ù »çÀü¿¡¼
$b = "Clinton"; # ¾ÕÂÊ¿¡ À§Ä¡ÇϹǷΠ("Jones" lt "Clinton")Àº °ÅÁþÀÌ´Ù.
if ($a lt $b) {
print "$a is less than $b.\n";
} else {
print "$a is greater than or equal to $b.\n";
}
5) ³í¸®¿¬»êÀÚ(logical operator)
bitwise operator·Î´Â C¿Í ¸¶Âù°¡Áö·Î &(bit AND), |(bit OR), ^(bit XOR)µîÀÇ ¿¬»êÀÚ
µîÀÌ Á¦°øµÈ´Ù. ³í¸®¿¬»êÀÚ ¶ÇÇÑ C¿Í ¸¶Âù°¡Áö·Î &&¿Í ||°¡ »ç¿ëµÇ´Âµ¥, short-circuit ¿¬»êÀÚ¶ó´Â
Ư¼ºÀ» È°¿ëÇÑ ±â¹ýÀÌ Perl¿¡¼ ÀÚÁÖ »ç¿ëµÈ´Ù.
open(FILE, $filename) || die "Can't open $filename!\n";
open(FILE, $filename) && (@data = ) && close(FILE) ||
die "Can't open $filename!\n";
ÀÌ ¹®ÀåÀº fileÀ» openÇÏ°í ÀоîµéÀÌ°í closeÇÏ´Â ¸ðµç °úÁ¤ÀÌ ¼º°øÀûÀ¸·Î ¼öÇàµÇÁö ¸øÇÏ°í 3°³ÀÇ ÇÇ¿¬»êÀÚÀÎ open, read, close ¹®Àå ÁßÀÇ ¾î¶² Çϳª¶óµµ ½ÇÆÐÇÏ°Ô µÇ¸é die ¹®ÀåÀÌ ¼öÇàµÇ´Â °ÍÀÌ´Ù.
&&¿Í ||´Â ±âÈ£°¡ ¾Æ´Ï¶ó and ¶Ç´Â or·Î ´ë½Å »ç¿ëÇÒ ¼ö ÀÖ´Ù.
open(FILE, $filename) or die "Can't open $filename!\n";
6) À̸§ ÀÖ´Â ´ÜÇ× ¿¬»êÀÚ(named unary operator)
´ÙÀ½ÀÇ named unary operatorµéÀº ÇÔ¼öó·³ °¢°¢ÀÇ À̸§À¸·Î »ç¿ëµÇ´Â ´ÜÇ× ¿¬»êÀÚÀÌ´Ù. named unary
operator´Â ¾î¶² binary operator(ÀÌÇ× ¿¬»êÀÚ)º¸´Ù´Â ¿ì¼± ¼øÀ§°¡ ³ô´Ù. ±×·¡¼ expression(Ç¥Çö½Ä)À»
¾µ ¶§¿¡´Â ÇÇ¿¬»êÀÚ¿¡ ´ëÇÑ ¿¬»êÀÚµéÀÇ ¿ì¼± ¼øÀ§¸¦ ÃæºÐÈ÷ °í·ÁÇÏ´Â °ÍÀÌ ¹Ù¶÷Á÷ÇÏ´Ù. ¸ðÈ£ÇÒ °æ¿ì¿¡´Â ¹Ýµå½Ã ()¸¦ »ç¿ëÇÏ¿© ¿ì¼±
¼øÀ§¸¦ ¸í½ÃÇÏ´Â °ÍÀÌ ¾ÈÀüÇÏ´Ù.
alarm | caller | chdir |
chroot | cos | defined |
delete | do | eval |
exists | exit | exp |
gethostbyname | getnetbyname | getpgrp |
getprotobyname | glob | gmtime |
goto | hex | int |
lc | lcfirst | length |
local | localtime | log |
lstat | my | oct |
ord | quotemeta | rand |
readlink | ref | require |
reset | return | rmdir |
scalar | sin | sleep |
sqrt | srand | stat |
uc | ucfirst | umask |
undef |
3. Control Structure(Á¦¾î ±¸Á¶)
Á¦¾î±¸Á¶¿¡¼ °¡Àå Áß¿äÇÑ °ÍÀº Á¶°Ç½Ä(conditional expression)ÀÌ´Ù. Á¶°Ç½ÄÀÌ ÂüÀÏ ¶§¿Í °ÅÁþÀÏ ¶§, ÇÁ·Î±×·¥ÀÇ
Á¦¾î(control)ÀÌ ºÐ±âÇØ¾ß Çϱ⠶§¹®ÀÌ´Ù. Perl¿¡¼ Á¶°Ç½ÄÀÌ °ÅÁþÀÌ µÇ´Â °æ¿ì´Â °è»êÀÇ °á°ú°¡ ´ÙÀ½°ú °°À» ¶§ÀÌ´Ù.
0 # ¼öÄ¡ 0
"" # null string
undef # null string
ÀÌ·± °æ¿ì¸¦ Á¦¿ÜÇÑ ¸ðµç °æ¿ì´Â ÂüÀÎ Á¶°Ç½ÄÀ¸·Î °£ÁֵȴÙ.
1) if
if/else ±¸¹®Àº CÀÇ ¹®¹ý°ú ºñ½ÁÇÏ´Ù. ´Ù¸¸ Á¶°Ç½ÄÀÇ °á°ú¿¡ µû¸£´Â ¹®ÀåÀº ¹Ýµå½Ã ºí·° ¾È¿¡ À§Ä¡ÇØ¾ß ÇÑ´Ù.
if (Á¶°Ç½Ä) {
# ÂüÀÏ ¶§ ½ÇÇàÇÏ´Â ¹®ÀåµéÀ» ¾´´Ù.
} else {
# °ÅÁþÀÏ ¶§ ½ÇÇàÇÏ´Â ¹®ÀåµéÀ» ¾´´Ù.
}
µÎ°³ ÀÌ»óÀÇ Á¶°Ç½ÄÀ» ³ª¿Çϱâ À§Çؼ´Â ´ÙÀ½°ú ºñ½ÁÇÑ Çü½ÄÀ¸·Î Á¶°Ç¹®À» ±¸¼ºÇØ¾ß ÇÑ´Ù.
if (Á¶°Ç½Ä1) {
# Á¶°Ç½Ä1ÀÌ ÂüÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
} elsif (Á¶°Ç½Ä2) {
# Á¶°Ç½Ä2°¡ ÂüÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
} elsif (Á¶°Ç½Ä3) {
# Á¶°Ç½Ä3ÀÌ ÂüÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
} else {
# ¸ðµç Á¶°Ç½ÄÀÌ ÂüÀÌÁö ¾ÊÀ» ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
}
´ÙÀ½Àº if/else¸¦ ÀÌ¿ëÇÑ Á¶°Ç¹®ÀÇ ¿¹Á¦ÀÌ´Ù.
print "How old are you? ";
$a = <STDIN>; # »ç¿ëÀÚ¿¡°Ô ÀÔ·ÂÀ» ¹Þ´Â´Ù.
chop($a); # ÀÔ·ÂÀÇ ¸¶Áö¸· ±ÛÀÚ(newline)À» ¶¼¾î³½´Ù.
if ($a < 10) {
print "You're a child.\n";
} elsif ($a < 20) {
print "You're a teenager.\n";
} else {
print "You're so old.\n";
}
½ÇÇàÇÒ ¹®ÀåÀÌ ÇϳªÀÏ ¶§¿¡´Â ¸Å¹ø { }·Î ºí·°À» ÁöÁ¤ÇÏ¿© ¾²±â°¡ ¹ø°Å·Ó´Ù. ÀÌ·² ¶§¿¡´Â if Á¶°Ç½ÄÀ» ¹®Àå µÚÂÊ¿¡ À§Ä¡½Ãų ¼ö ÀÖ´Ù.
print "$a is equal to $b.\n" if $a eq $b;
2) unless
unless´Â Á¶°Ç½ÄÀÇ °á°ú¸¦ if¿Í´Â ¹Ý´ë·Î Çؼ®ÇÑ´Ù. ´ÙÀ½ÀÇ Çü½ÄÀ» Âü°íÇ϶ó
unless (Á¶°Ç½Ä) {
# Á¶°Ç½ÄÀÌ °ÅÁþÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
} else {
# Á¶°Ç½ÄÀÌ ÂüÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
}
´ÙÀ½Àº if¿¡¼ ³ª¿Ô´ø ¿¹Á¦¿Í °°Àº °á°ú¸¦ º¸¿©ÁÖ´Â ¿¹Á¦ÀÌ´Ù.
print "How old are you? ";
$a = <STDIN>;
chop($a);
unless ($a < 20) {
print "You're so old.\n";
} elsif ($a >= 10) {
print "You're a teenager.\n";
} else {
print "You're a child.\n";
}
3) while/until
while¹®°ú until¹®Àº ´ÙÀ½°ú °°Àº Çü½ÄÀ» °¡Áø´Ù.
while (Á¶°Ç½Ä) {
# Á¶°Ç½ÄÀÌ ÂüÀÎ µ¿¾ÈÀº °è¼Ó ½ÇÇàµÉ ¹®Àåµé
}
until (Á¶°Ç½Ä) {
# Á¶°Ç½ÄÀÌ °ÅÁþÀÌ µÉ ¶§±îÁö °è¼Ó ½ÇÇàµÉ ¹®Àåµé
}
while°ú untilÀÇ °ü°è´Â if¿Í unlessÀÇ °ü°è¿Í À¯»çÇÏ´Ù. ´ÙÀ½Àº °°Àº °á°ú¸¦ º¸¿©ÁÖ´Â while/until±¸¹®ÀÇ ¿¹Á¦ÀÌÀÌ´Ù.
$a = 0;
while ($a < 10) {
$a++;
print "$a\n"; # 1 2 3 4 5 6 7 8 9 10ÀÌ Â÷·Ê´ë·Î Ãâ·ÂµÈ´Ù.
}
$a = 0;
until ($a >= 10) {
$a++;
print "$a\n"; # 1 2 3 4 5 6 7 8 9 10ÀÌ Â÷·Ê´ë·Î Ãâ·ÂµÈ´Ù.
}
continue ºí·°À» »ç¿ëÇÏ¿© loop¸¦ µ¹ ¶§¸¶´Ù ½ÇÇàÇØÁÖ´Â ºÎºÐÀ» ºÐ¸®ÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½Àº while/continue ±¸¹®ÀÇ Çü½ÄÀÌ´Ù.
while (Á¶°Ç½Ä) {
# Á¶°Ç½ÄÀÌ ÂüÀÏ ¶§ ½ÇÇàµÇ´Â ¹®Àåµé
} continue {
# Á¶°Ç½Ä¿¡ °ü°èµÇ´Â º¯¼öµéÀÇ Áõ°¨½Ä(auto-in/decrement)
}
´ÙÀ½ÀÇ ¿¹Á¦´Â À§ÂÊÀÇ while/untilÀÇ ¿¹Á¦¿Í °°Àº °á°ú¸¦ º¸¿©ÁØ´Ù. ±×·¯³ª À§ÂÊÀÇ Çü½Äº¸´Ù´Â º¸´Ù ¾ÈÀüÇÑ ±¸¹® Ç¥±â¹ýÀ¸·Î º¼ ¼ö ÀÖ´Ù. continue ºí·° ¾È¿¡¼¸¸ Áõ°¨½ÄÀÌ ±¸¼ºµÇ´Â °æ¿ì º¯¼öÀÇ ÃßÀûÀÌ ¿ëÀÌÇÏ´Ù.
$i = 1;
while ($i <= 10) {
print "Count-up: i = $i\n";
} continue {
$i++;
}
4) for
for±¸¹®ÀÇ ±âº»Àû Çü½ÄÀº C±¸¹®°ú µ¿ÀÏÇÏ´Ù. ±×·¯³ª Perl¸¸ÀÇ È®ÀåµÈ Çü½Äµµ ¼Ò°³µÉ °ÍÀÌ´Ù.
for (ÃʱâÈ; Á¶°Ç½Ä; Áõ°¨½Ä) {
# Á¶°Ç½ÄÀÌ ÂüÀÏ µ¿¾È ½ÇÇàµÉ ¹®Àåµé
}
°¡Àå ¸ÕÀú ÃʱâÈ ¹®ÀåÀÌ ½ÇÇàµÈ´Ù. ±×¸®°í´Â Á¶°Ç½ÄÀ» °Ë»çÇÑ ÈÄ¿¡, ÂüÀÌ¸é ºí·°³»ÀÇ ¹®ÀåÀ» ½ÇÇàÇÏ°í ºí·°ÀÌ ³¡³ª¸é Áõ°¨½ÄÀ» ½ÇÇàÇÑ´Ù. ±×¸®°í ´Ù½Ã Á¶°Ç½ÄÀ» °Ë»çÇÏ´Â ÀϺÎÅÍ ´Ù½Ã ¹Ýº¹ÇÑ´Ù.
´ÙÀ½Àº 1ºÎÅÍ 10±îÁöÀÇ ÇÕÀ» °è»êÇÏ´Â ¿¹Á¦ÀÌ´Ù.
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i; # $sum = $sum + $i, $sum¿¡ $iÀÇ °ªÀ» ´õÇÑ´Ù.
}
print "Sum is " . $sum . "\n";
ÀÌÁ¦ C¸¦ ¶Ù¾î³Ñ´Â PerlÀÇ for ±¸¹®¿¡ ´ëÇؼ »ìÆ캸µµ·Ï ÇÏ°Ú´Ù. ¹üÀ§¿¬»êÀÚ(range operator)ÀÎ ..¸¦ »ç¿ëÇÑ Çü½ÄÀº ´ÙÀ½°ú °°´Ù.
for ($a .. $b) {
# $a°ªºÎÅÍ $b°ª±îÁö $b-$a+1¹ø ¼öÇàµÇ´Â ¹®Àåµé
}
¹üÀ§¿¬»êÀÚ ..´ë½Å¿¡ list¿¬»êÀÚÀÎ ,¸¦ »ç¿ëÇÏ¿© ¿ø¼Ò¸¦ ³ª¿ÇÏ´Â °Íµµ °¡´ÉÇÏ´Ù. ÀÌ¿Í °°Àº list ÇüÀÇ º¯¼ö³ª °ªÀ»
»ç¿ëÇÏ´Â for±¸¹®Àº ´Ü¼øÈ÷ Á¤¼ö¹ø ¼öÇàµÇµµ·Ï ÇÏ´Â µ¥¸¸ »ç¿ëµÇ´Â °ÍÀº ¾Æ´Ï´Ù. ´ÙÀ½ÀÇ ¿¹Á¦´Â for¹®ÀÌ CÀÇ for±¸¹®°ú´Â
´Ù¸£°Ô, ´õ À¯¿¬ÇÑ Çü½ÄÀ» °¡ÁüÀ» º¸¿©ÁØ´Ù.
for (10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) {
print "Count-down: " . $_ . "\n"; # $_¿¡´Â 10 .. 0ÀÌ °¢°¢ µé¾î°£´Ù.
}
for (0 .. 10) {
print "Count-up: " . $_ . "\n"; # $_¿¡´Â 10 .. 0ÀÌ °¢°¢ µé¾î°£´Ù.
}
±×·¯³ª list°¡ ¾Æ´Ï¶ó ¹üÀ§ ¿¬»êÀÚ¸¦ »ç¿ëÇÑ °æ¿ì¿¡´Â, ¼ö¿ÀÌ ÁÙ¾îµå´Â Á¶°Ç½ÄÀº °ÅÁþÀ¸·Î °áÁ¤µÈ´Ù.
for (10 .. 1) {
print "Count-down: " . $_ . "\n"; # ÀÌ loop´Â ½ÇÇàµÇÁö ¾Ê´Â´Ù.
}
ÀÌ·± °æ¿ì¿¡´Â reverse ¿¬»êÀÚ¸¦ ÀÌ¿ëÇÏ¿© ¿øÇÏ´Â °á°ú¸¦ ¾òÀ» ¼ö ÀÖ´Ù.
for (reverse 1 .. 10) {
print "Count-down: " . $_ . "\n";
}
5) foreach
foreach±¸¹®Àº csh¿¡¼ »ç¿ëµÇ¾ú´ø ±¸¹®Àε¥ CÇÁ·Î±×·¡¸Ó¿¡°Ô´Â ¾à°£ »ý¼ÒÇÒ ¼ö ÀÖÁö¸¸ vectorÇü ÀڷḦ ¸¹ÀÌ ´Ù·ç´Â ÀÛ¾÷¿¡¼´Â ²À ÇÊ¿äÇÑ Á¦¾î ±¸Á¶ÀÌ´Ù. Perl¿¡¼´Â, foreach´Â forÀÇ µ¿ÀǾîÀÌ´Ù.
foreach scalarº¯¼ö (listÇüÀÇ_º¯¼ö_¶Ç´Â_°ª) {
# scalarº¯¼ö¿¡ ´ëÇÑ Á¶ÀÛÀ» ÇÏ´Â ¹®Àåµé
}
listÇüÀÇ º¯¼ö ¶Ç´Â °ª¿¡¼ Çϳª¾¿ scalarº¯¼ö¿¡ ´ëÀÔÇÏ¿© list³»ÀÇ °¢ ¿ø¼Òµé¿¡ ´ëÇؼ °¢°¢ ºñ½ÁÇÑ À¯ÇüÀÇ Ã³¸®¸¦ ÇØ
ÁÙ ¼ö ÀÖ´Ù. ´ÙÀ½Àº foreach¸¦ »ç¿ëÇÏ¿© ÇöÀç µð·ºÅ丮¿¡ Á¸ÀçÇÏ´Â ÆÄÀϵéÀÇ permissionÀ» ¹Ù²Ù¾îÁÖ´Â ¿¹Á¦ÀÌ´Ù.
foreach $f (`ls`) {
chmod 0755, $f; # chmod´Â fileÀÇ permission mode¸¦ ¹Ù²Ù¾îÁÖ´Â ¿¬»êÀÚ
}
´ÙÀ½Àº hash Çü º¯¼ö¿¡¼ keys ¶ó´Â ¿¬»êÀÚ¸¦ ÅëÇØ arrayÀ» ¾ò¾î³»´Â ¹æ½ÄÀ¸·Î ȯ°æº¯¼öÀÇ À̸§°ú °ªÀ» Ãâ·ÂÇÏ´Â
¿¹Á¦ÀÌ´Ù. foreach±¸¹®Àº ÀÌ·¯ÇÑ ¹æ½ÄÀ¸·Î »ç¿ëµÇ±â ¶§¹®¿¡ index°¡ ¼øÂ÷ÀûÀÌÁö ¾ÊÀº vectorÇü º¯¼ö¸¦ ´Ù·ç´Âµ¥ ÀÖ¾î¼
CÀÇ for±¸¹®º¸´Ù È¿À²ÀûÀÌ°í Á÷°üÀûÀÌ´Ù.
foreach $key (keys %ENV) {
print "Key:$key, Value:$ENV{$key}\n";
}
6) do/while, do/until
do/while ¶Ç´Â do/until±¸¹®Àº À§¿¡¼ ¼³¸íÇß´ø whileÀ̳ª until±¸¹®À» Âü°íÇÏ¸é ±×´ÙÁö ¾î·Á¿î °³³äÀº ¾Æ´Ò µí
½Í´Ù. ¸¹Àº CÇÁ·Î±×·¡¸ÓµéÀÌ ¾Ë°í ÀÖ´Ù½ÃÇÇ do·Î ½ÃÀÛÇÏ´Â ±¸¹®ÀÇ while±¸¹®(until±¸¹®)°úÀÇ Â÷ÀÌÁ¡Àº Á¶°Ç½ÄÀÌ
loopº¸´Ù ¸ÕÀú ¼öÇàµÇ´Â°¡ ¾Æ´Ï¸é loop°¡ ÃÖ¼ÒÇÑ ÇÑ ¹ø ¼öÇàµÈ ÈÄ¿¡ ¼öÇàµÇ´Â°¡ÀÇ Â÷ÀÌÀÌ´Ù. do±¸¹®¿¡¼´Â Á¶°Ç½ÄÀÌ loop
µÚÂÊ¿¡ Á¦½ÃµÇ¹Ç·Î ÃÖ¼ÒÇÑ 1¹øÀº Á¶°Ç°ú´Â »ó°ü¾øÀÌ ¼öÇàµÊÀ» ¾Ë ¼ö ÀÖ´Ù.
do±¸¹®ÀÇ Çü½ÄÀº ´ÙÀ½°ú °°´Ù.
do {
# Á¶°Ç½ÄÀÌ ÂüÀÎ µ¿¾È¿¡ ½ÇÇàµÉ ¹®Àåµé
} while (Á¶°Ç½Ä);
do {
# Á¶°Ç½ÄÀÌ ÂüÀÌ µÉ ¶§±îÁö(°ÅÁþÀÎ µ¿¾È¿¡) ½ÇÇàµÉ ¹®Àåµé
} until (Á¶°Ç½Ä);
´ÙÀ½Àº °°Àº °á°ú¸¦ º¸¿©ÁÖ´Â do/while, do/until±¸¹®ÀÇ ¿¹Á¦ÀÌ´Ù.
$i = 0;
do {
print "Count-up: $i\n";
$i++;
} while ($i <= 10);
$i = 0;
do {
print "Count-up: $i\n";
$i++;
} until ($i > 10);
7) goto
CÇÁ·Î±×·¥°ú ¸¶Âù°¡Áö·Î ¹®ÀåÀ̳ª ºí·°¿¡ labelÀ» ÁöÁ¤ÇÒ ¼ö Àִµ¥, labelÀÌ ÁöÁ¤µÈ °÷ÀÌ loop ³»ºÎ°¡ ¾Æ´Ï¶ó¸é
¾îµð·Îµç Á¦¾î(control)¸¦ ¿Å±æ ¼ö ÀÖ´Ù. ±×·¯³ª ÀÌ·¯ÇÑ gotoÀÇ »ç¿ëÀº ÀûÇÕÇÑ °÷À¸·ÎÀÇ Á¦¾î À̵¿À̶ó Çصµ ¹Ù¶÷Á÷ÇÏÁö´Â
¾Ê´Ù. Perl¿¡¼´Â ´Ù¸¥ ¾ð¾îµéº¸´Ù ´õ dzºÎÇÑ Á¦¾î ±¸Á¶(control structure)¸¦ Áö¿øÇÑ´Ù´Â °ÍÀ» À¯³äÇÏ±æ ¹Ù¶õ´Ù.
´ÙÀ½Àº gotoÀÇ Çü½ÄÀÌ´Ù.
goto LABEL;
¿¹Á¦¸¦ Âü°íÇϵµ·Ï ÇÏÀÚ. OUTÀ̶ó´Â labelÀº °Ç³Ê¶Ù°í INÀ̶ó´Â labelÀÌ À§Ä¡ÇÑ °÷À¸·Î Á¦¾î°¡ ¿Å°ÜÁø´Ù.
goto IN;
OUT:
print "This message is printed to standard out.\n";
IN:
print "Enter your input : ";
$input = <STDIN>;
gotoÀÇ °¡Àå Å« ¹®Á¦Á¡Àº LABELÀÌ gotoÀÇ ¾ÕÂÊ, µÚÂÊ ¾îµð¿¡ Á¸ÀçÇÏ´ÂÁö ¾Ë ¹æ¹ýÀÌ ¾ø±â ¶§¹®¿¡, ÇÁ·Î±×·¥ÀÇ Àüü¸¦ °Ë»öÇÏ°Ô µÈ´Ù´Â °ÍÀÌ´Ù. Perl ÇÁ·Î±×·¥ÀÌ Ä¿Áü¿¡ µû¶ó °Ë»ö½Ã°£ÀÌ ´Ã¾î³¯ °ÍÀº ÀÚ¸íÇÏ´Ù.
±×·¯³ª goto´Â ÁöÁ¤ÇØÁÖ´Â º¯¼ö °ª¿¡ µû¶ó µ¿ÀûÀ¸·Î Á¦¾î°¡ ³Ñ°ÜÁö´Â labelÀ» ¹Ù²Ü ¼ö ÀÖ´Ù. ´ÙÀ½ÀÇ ¿¹Á¦´Â $iÀÇ °ª¿¡ µû¶ó goto°¡ ½ÇÇàµÉ labelÀÌ ¿©·¯ °³ ³ª¿µÉ ¼ö ÀÖÀ½À» º¸¿©ÁØ´Ù.
goto ("LINE0", "LINE1", "LINE2")[$i];
ÀϹÝÀûÀ¸·Î gotoÀÇ ÇÇ¿¬»êÀÚ·Î µû¶ó¿À´Â labelÀº 0ºÎÅÍ ¹øÈ£°¡ ¸Å°ÜÁöÁö¸¸, $[ÀÇ °ªÀ» 1·Î ÁöÁ¤Çϸé labelÀº 1¹øºÎÅÍ
¹øÈ£°¡ ¸Å°ÜÁø´Ù. (Ư¼öÇÑ º¯¼ö¿¡ °ü·ÃÇؼ »ìÆ캸¾ÒµíÀÌ, $[Àº arrayÀÇ index ½ÃÀÛ ¹øÈ£¸¦ ÁöÁ¤ÇÏ´Â global
special variableÀÌ´Ù. )
8) next, last, redo
loop ³»¿¡¼ Á¦¾î¸¦ ¿Å±â´Â ¹æ¹ýÀÌ Á¸ÀçÇÑ´Ù. next, last, redo°¡ ±×°ÍÀ̸ç, CÇÁ·Î±×·¥°ú ºñ±³ÇÏÀÚ¸é, next´Â continue, last´Â break¿Í ºñ½ÁÇÑ ±â´ÉÀ» °¡Áö°í ÀÖ´Ù.
a) next
next´Â loop³»ÀÇ Á¦¾î¸¦ ´ÙÀ½ Â÷·Ê·Î ³Ñ±â°Ô µÈ´Ù. CÀÇ continue¹®°ú °°Àº ±â´ÉÀ» ÇÑ´Ù. ±×·¯³ª Perl¿¡¼ÀÇ
continue ºí·°°ú CÀÇ continue¹®ÀåÀº ´Ù¸¥ Àǹ̸¦ °¡Áö°Ô µÈ´Ù. ´ÙÀ½ ¿¹Á¦¸¦ ÅëÇØ nextÀÇ »ç¿ë¹ýÀ» ÀÍÈ÷µµ·Ï ÇÏÀÚ.
LINE: while (<STDIN>) {
next LINE if /^#/; # ÁÖ¼®Àº °Ç³Ê¶Ú´Ù.
next LINE if /^$/; # null lineµµ °Ç³Ê¶Ú´Ù.
} continue {
$count++; # next¿¡ ÀÇÇØ Á¦¾î°¡ ¿Å°ÜÁ®µµ count´Â Áõ°¡ÇÑ´Ù.
}
LABELÀÌ ¾îµð¿¡ À§Ä¡ÇÏµç °£¿¡ next¹®À» ¸¸³ª°Ô µÇ¸é continueºí·°¿¡¼ Áõ°¨½ÄÀ» ¼öÇàÇÑ ÈÄ¿¡ ±× label·Î Á¦¾î¸¦ ¿Å±â°Ô µÊÀ» ¾Ë ¼ö ÀÖ´Ù. CÀÇ continue¿Í °°ÀÌ ´Ü¼øÇÏ°Ô ÀÛµ¿½ÃÅ°´Â °Íµµ ¾î·Á¿î ÀÏÀÌ ¾Æ´Ï´Ù.
for ($i = 0; $i < 10; $i++) {
if ($i == 5) {
next;
}
print "$i\n";
}
b) last
last´Â loop¸¦ ºüÁ®³ª¿À´Â ±â´ÉÀ» ¼öÇàÇÑ´Ù. loop¸¦ ºüÁ®³ª¿À´Â °ÍÀ̱⠶§¹®¿¡ continueºí·°³»ÀÇ ¹®Àåµéµµ ´ç¿¬È÷
¼öÇàµÇÁö ¾Ê´Â´Ù. labelÀÌ loopÀÇ °¡Àå ¾ÕÂÊÀ» °¡¸®Å²´Ù°í Çؼ loop¸¦ ´Ù½Ã ¹Ýº¹ÇÏ´Â °ÍÀÌ ¾Æ´Ô¿¡ À¯ÀÇÇ϶ó.
LINE: while (<>) {
last LINE if /^$/; # null stringÀ» ¸¸³ª¸é Á¾·áÇÑ´Ù.
print; # default input stringÀÎ $_¸¦ Ãâ·ÂÇÑ´Ù.
}
À§ÀÇ ¿¹Á¦¿¡¼´Â LINEÀ̶ó´Â labelÀ» ¸í½ÃÇÏÁö ¾Ê¾Æµµ loop¸¦ Á¾·áÇÒ ¼ö ÀÖ´Ù. ±×·¯³ª loop ¹ÛÀÇ labelÀ» ÁöÁ¤ÇÏ´Â °æ¿ì¿¡´Â compile-time error°¡ ¹ß»ýÇÏ°Ô µÈ´Ù.
OUT:
print "Hello\n";
LINE: while (<>) {
last OUT if /^$/; # OUT¸¦ ãÀ» ¼ö ¾ø¾î¼ ¿¡·¯°¡ ¹ß»ýÇÑ´Ù.
print;
}
CÀÇ break¿Í ¸¶Âù°¡Áö·Î last´Â ´ÙÁß loop¿¡¼ last ±¸¹®ÀÌ ¼ÓÇØ ÀÖ´Â loop¸¸À» ºüÁ®³ª¿Ã »ÓÀÌ´Ù.
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
print "($i, $j)\n";
last if $j >= 5;
}
# loop³»ÀÇ last¿¡ ÀÇÇØ Á¦¾î°¡ ¿Å°ÜÁö´Â °÷Àº ¹Ù·Î ÀÌ°÷ÀÌ´Ù.
}
c) redo
redo¹®ÀåÀº loop¸¦ »õ·Î ½ÃÀÛÇÏ´Â ±â´ÉÀ» °¡Áø´Ù. C¿¡¼´Â º¼ ¼ö ¾ø¾ú´ø ±â´ÉÀÌ´Ù. redoµµ next³ª last¿Í °°Àº Çü½ÄÀ» °¡Áö°í ºñ½ÁÇÑ ¿ªÇÒÀ» ÇÑ´Ù. ´ÙÀ½ÀÇ ¿¹Á¦¸¦ º¸°í ÀÌÇØÇϵµ·Ï ÇÏÀÚ.
while (<>) {
chomp;
if (s/\\$//) {
$_ .= <>;
redo;
}
print;
}
labelÀ» »ç¿ëÇÏ´Â ´ÙÀ½ ¿¹Á¦´Â ¾ÕÀÇ ¿¹Á¦¿Í °°Àº Àǹ̸¦ °¡Áø´Ù.
LINE: while ($line = ) {
chomp($line);
if ($line =~ s/\\$//) {
$line .= ;
redo LINE;
}
print $line;
}
ÀÌ µÎ ¿¹Á¦´Â backslash(\)·Î ³¡³ª´Â lineÀ» ¸¸³ª¸é ´ÙÀ½ lineÀ» À̹ø line¿¡ ºÙ¿©ÁÖ´Â ÀÛ¾÷À» ¼öÇàÇÑ´Ù.
4. ¼ºê·çƾ(Subroutine)
1) Á¤ÀÇ¿Í »ç¿ë
¼ºê·çƾÀ̶õ CÀÇ ÇÔ¼ö(function)¿Í °°Àº °ÍÀ¸·Î º¼ ¼ö ÀÖ´Ù. ¼ºê·çƾÀº ½ÇÇà °¡´ÉÇÑ ¹®ÀåµéÀ» Æ÷ÇÔÇϱâ´Â Çϳª, Á¤ÀÇ
ÀÚü´Â ½ÇÇà°¡´ÉÇÑ ¹®ÀåÀº ¾Æ´Ï°í script ¾î´À °÷¿¡³ª À§Ä¡ÇÒ ¼ö ÀÖ´Ù. ¼ºê·çƾÀÇ Á¤ÀÇ¿Í È£ÃâÀº ´ÙÀ½°ú °°Àº Çü½ÄÀ» °¡Áø´Ù.
sub ¼ºê·çƾÀ̸§ ¼ºê·çƾºí·° # ¼ºê·çƾÀÇ Á¤ÀÇ
&¼ºê·çƾÀ̸§; # ¼ºê·çƾÀÇ È£Ãâ
do ¼ºê·çƾÀ̸§;
¼ºê·çƾÀ» È£ÃâÇϱâ À§Çؼ ±âº»ÀûÀ¸·Î & ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù. ±×·¯³ª ¼ºê·çƾÀº do, require, use,
evalµîÀÇ ¿¬»êÀÚ¸¦ ÅëÇØ »ç¿ëµÉ ¼ö ÀÖ´Ù. & ¿¬»êÀº °ýÈ£¸¦ »ç¿ëÇϰųª ÀÌ¹Ì Á¤ÀǵÈ(¶Ç´Â ÀÌ¹Ì importµÈ) °æ¿ì
»ý·« °¡´ÉÇÏ´Ù.
´ÙÀ½Àº ¼ºê·çƾÀÇ »ç¿ë ¿¹ÀÌ´Ù.
sub sum {
$sum = 0;
for ($i = 0; $i < 10; $i++) {
$sum += $i;
}
print $sum;
}
¢²
¿©±â¼ ´ëºÎºÐÀÇ ´«Ä¡ ºü¸¥ ÇÁ·Î±×·¡¸Ó¶ó¸é C¿Í´Â ´Þ¸® argument°¡ ³Ñ°ÜÁö´Â °ÍÀÌ formalÇÏ°Ô Á¤ÀǵǾî ÀÖÁö ¾ÊÀ½À»
±ú´Þ¾ÒÀ» °ÍÀÌ´Ù. Perl¿¡¼´Â default input list·Î argument¸¦ ÁÖ°í¹ÞÀ» ¼ö ÀÖ´Ù. ¼ºê·çƾ ³»ºÎ¿¡¼
argument¸¦ ¹Þ¾Æ¼ »ç¿ëÇÒ parameterµéÀº ´ÙÀ½¿¡¼ »ìÆì º¼ localÀ̳ª my ¿¬»êÀÚ¸¦ ÀÌ¿ëÇÏ¿© ÃʱâȽÃų ¼ö
ÀÖ´Ù. ´ÙÀ½ÀÇ ¿¹Á¦´Â argument¸¦ parameter·Î ³Ñ°Ü¹Þ´Â ¹æ¹ýÀ» ¼³¸íÇÏ°í ÀÖ´Ù.
sub sum {
local($a, $b) = @_; # default input listÀÎ @_¿¡¼ ³Ñ°Ü¹Þ¾Æ
$sum = $a + $b; # $a¿Í $b¿¡ Â÷·Ê´ë·Î ÀúÀåÇÑ´Ù.
}
print ¢²(1, 5); # ¸¶Áö¸·À¸·Î ´ëÀÔµÈ º¯¼ö°¡ returnµÈ´Ù.
sub listdouble {
local(*list) = @_; # @_¸¦ ´Ù½Ã list¿¡ ³ÖÀ¸·Á¸é *var·Î ¹Þ¾Æ¼
foreach $e (@list) { # @varÀÇ ÇüÅ·Π»ç¿ëÇÏ¸é µÈ´Ù. *´Â ¾î¶² ÇüÅÂÀÇ
$e *= 2; # º¯¼ö·Îµµ »ç¿ëµÉ ¼ö ÀÖ´Â º¯¼ö±âÈ£ÀÌ´Ù.
}
}
@array = (1, 2, 3, 4, 5);
&listdouble(@array); # @array eq (2, 4, 6, 8, 10)
2) my¿Í local
my¿Í localÀº º¯¼öÀÇ »ç¿ë¹üÀ§(scope)¸¦ ÁöÁ¤ÇÏ´Â modifierÀÌ´Ù. localÀº º¯¼ö¸¦ dynamic
variable·Î ¸¸µé°í, my´Â º¯¼ö¸¦ lexical variable·Î ¸¸µé¾îÁØ´Ù. dynamic variableÀÇ °æ¿ì ±×
º¯¼ö°¡ ¼±¾ðµÈ ºí·° ³»¿¡¼ È£ÃâµÈ ¼ºê·çƾ¿¡¼ º¸ÀÌ°Ô µÈ´Ù. ±×·¯³ª lexical variableÀº ±× ºí·°À» ¹þ¾î³ª°Ô µÇ¸é
ÀüÇô »ç¿ëÇÒ ¼ö ¾ø°Ô µÈ´Ù.(lexical variableÀº CÇÔ¼ö ³»ºÎÀÇ ÀϹÝÀûÀÎ º¯¼öÀÇ ¼ºÁú°ú °°´Ù.)
sub power {
my ($num, $count) = @_;
my $result = 1;
if ($count == 1) {
$result = $num;
} else {
$result = power($num, --$count) * $num;
}
}
$val = power(2, 7);
print "value=$val\$";
ÀÌ ¿¹Á¦´Â my¸¦ »ç¿ëÇÑ °ÅµìÁ¦°öÀ» °è»êÇÏ´Â ¼ºê·çƾÀÌ´Ù. 2ÀÇ 7½ÂÀ» °è»êÇÏ¿© 128À» µ¹·ÁÁÖ±â À§ÇØ recursionÀ» »ç¿ëÇÏ¿´´Ù.
5. ÆÐÅÏ ¸ÅĪ(Pattern Matching)
'ÆÐÅÏ ¸ÅĪ'(pattern matching)À̶õ, ÁÖ¾îÁø ´Ü¶ô(paragraph) ¼Ó¿¡¼, Á¦½ÃµÈ Ç¥ÇöÇüÀÎ patternÀÌ
ÀÏÄ¡ÇÏ´Â °æ¿ì, ÀÏÄ¡ÇÏ´Â ºÎºÐ(lineÀ̳ª string, substringµî)À» ã¾Æ³»´Â ÀÛ¾÷À» ÀÏÄ´´Ù. Perl¿¡¼ÀÇ
patternÀº 'Á¤±Ô Ç¥Çö½Ä'(regular expression)À¸·Î ³ªÅ¸³¾ ¼ö ÀÖ´Ù. ´ÙÀ½ ¼Ò´Ü¶ôµéÀ» ÅëÇØ regular
expressionÀ» ÀÍÇô, ÁøÁ¤ÇÑ Perl ÇÁ·Î±×·¡¹Ö ½Ç·ÂÀ» ½×µµ·Ï ÇÏÀÚ.
1) Á¤±Ô Ç¥Çö½Ä(regular expression)
´ÙÀ½Àº regular expression¿¡¼ »ç¿ëµÇ´Â ±âÈ£µéÀÇ Àǹ̸¦ Á¤¸®ÇÑ Ç¥ÀÌ´Ù. Perl¿¡¼ÀÇ pattern matchingÀº regular expressionÀ» »ç¿ëÇÒ ¼ö ÀÖÀ¸¹Ç·Î È®½ÇÇÏ°Ô ÀÍÇôµÎ´Â °ÍÀÌ ¹Ù¶÷Á÷ÇÏ´Ù.
. |
newlineÀ» Á¦¿ÜÇÑ ÀÓÀÇÀÇ ÇÑ ±ÛÀÚ |
[a-z0-9] |
a-z±îÁöÀÇ ¿µ¹®ÀÚ¿Í ¼ýÀÚ ÁßÀÇ ÇÑ ±ÛÀÚ |
[^a-z0-9] |
a-z±îÁöÀÇ ¿µ¹®ÀÚ¿Í ¼ýÀÚ°¡ ¾Æ´Ñ ÇÑ ±ÛÀÚ |
\a |
Alarm, beep |
\d |
10Áø¼ö Á¤¼ö ÇÑ ±ÛÀÚ, [0-9] |
\D |
10Áø¼ö Á¤¼ö°¡ ¾Æ´Ñ ÇÑ ±ÛÀÚ, [^0-9] |
\w |
¿µ¹®ÀÚ ¶Ç´Â ¼ýÀÚ, _(underline) ÁßÀÇ ÇÑ ±ÛÀÚ, [a-zA-Z0-9_] |
\W |
\w°¡ ¾Æ´Ñ ÇÑ ±ÛÀÚ, [^a-zA-Z0-9_] |
\s |
°ø¹é¹®ÀÚ(whitespace) ÇÑ ±ÛÀÚ (space, tab, newline ...) |
\S |
\s°¡ ¾Æ´Ñ ÇÑ ±ÛÀÚ |
\n |
newline |
\r |
carriage return |
\t |
tab |
\f |
formfeed |
\b |
[]³»ºÎ¿¡¼ »ç¿ëµÉ °æ¿ì backspace |
\0, \000 |
null character |
\nnn |
nnnÀ̶ó´Â 8Áø¼öÀÇ °ªÀ» °¡Áö´Â ASCII ÇÑ ±ÛÀÚ |
\xnn |
nnÀ̶ó´Â 16Áø¼öÀÇ °ªÀ» °¡Áö´Â ASCII ÇÑ ±ÛÀÚ |
\cX |
Ctrl+X¿¡ ÇØ´çÇÏ´Â ÇÑ ±ÛÀÚ |
\metachar |
\|, \*, \\, \(, \), \[, \{, \^, \$, \+, \?, \.¿Í °°ÀÌ Ç¥ÇöµÇ¸ç |, *, \, (, ), [, {, ^, $, +, ?, .ÀÇ Àǹ̸¦ °¡Áö´Â ÇÑ ±ÛÀÚ |
(abc) |
µÚ¿¡ °¡¼ ÂüÁ¶µÉ(backreference) abc¶ó´Â ¹®ÀÚ¿ |
\1 |
ù ¹ø° ÂüÁ¶ ¹®ÀÚ¿, open parenthesis, '('¿¡ ÀÇÇØ ¼ø¼°¡ °áÁ¤µÊ |
\2 |
µÎ ¹ø° ÂüÁ¶ ¹®ÀÚ¿ |
\n |
n¹ø° ÂüÁ¶ ¹®ÀÚ¿, 10ÀÌ»óÀÏ °æ¿ì backreferenceµÉ ¹®ÀÚ¿ÀÌ ¾øÀ¸¸é, 8Áø¼ö °ªÀÇ ASCII ÇÑ ±ÛÀÚÀÇ Àǹ̸¦ °¡Áú ¼ö ÀÖ´Ù. |
x? |
x°¡ ³ª¿ÀÁö ¾Ê°Å³ª 1¹ø ³ª¿À´Â °æ¿ì |
x* |
x°¡ 0¹ø ÀÌ»ó ¹Ýº¹µÇ´Â ¹®ÀÚ¿ |
x+ |
x°¡ 1¹ø ÀÌ»ó ¹Ýº¹µÇ´Â ¹®ÀÚ¿ |
x{m,n} |
x°¡ m¹ø ÀÌ»ó, n¹ø ¹Ì¸¸À¸·Î ³ª¿À´Â ¹®ÀÚ¿ |
x{m,} |
x°¡ m¹ø ÀÌ»ó ³ª¿À´Â ¹®ÀÚ¿ |
x{m} |
x°¡ m¹ø ³ª¿À´Â ¹®ÀÚ¿ |
abc |
a¿Í b¿Í c°¡ ¿¬¼ÓÀ¸·Î ºÙ¾î¼ ³ª¿À´Â ¹®ÀÚ¿ |
abc|def|ghi |
¹®ÀÚ¿ abc, def, ghi ÁßÀÇ Çϳª¿¡ ÇØ´çµÇ´Â °æ¿ì |
´ÙÀ½Àº Å©±â°¡ ¾ø´Â, ÁöÁ¤µÈ À§Ä¡°¡ ÀÏÄ¡Çϵµ·Ï Á¦½ÃÇÒ ¼ö ÀÖ´Â À§Ä¡ Ç¥½Ã Á¶°Ç(assertion)µéÀ» ³ª¿ÇÑ Ç¥ÀÌ´Ù.
\b |
[]¹Ù±ù¿¡¼ \w¿Í \W°¡ ¼·Î ¹Ù²î°í ÀÖ´Â word boundary¸¦ ÀǹÌÇÔ |
\B |
\b°¡ ¾Æ´Ñ °æ¿ì |
\A |
stringÀÇ Ã³À½ À§Ä¡ |
\Z |
stringÀÇ ¸¶Áö¸· À§Ä¡ |
\G |
¸¶Áö¸·À¸·Î m//g¿¡ ÀÇÇØ match°¡ µÈ À§Ä¡ |
^ |
stringÀÇ °¡Àå óÀ½ À§Ä¡ Ç¥½Ã·Î /m modifier°¡ ÀÖÀ¸¸é lineÀÇ Ã³À½À» ÀǹÌÇÑ´Ù. |
$ |
stringÀÇ °¡Àå ¸¶Áö¸· À§Ä¡ Ç¥½Ã·Î /m modifier°¡ ÀÖÀ¸¸é lineÀÇ ³¡À» ÀǹÌÇÑ´Ù. |
(?=...) |
...À§Ä¡¿¡ ³õÀÏ ¹®ÀÚ¿ÀÌ ¸ÅÄ¡ µÈ´Ù´Â Á¶°Ç |
(?!...) |
...À§Ä¡¿¡ ³õÀÏ ¹®ÀÚ¿ÀÌ ¸ÅÄ¡ µÇÁö ¾Ê´Â´Ù´Â Á¶°Ç |
´ÙÀ½ ¿¹¹®À» °¡Áö°í regular expressionÀ¸·Î pattern matchingÀ» ÇÏ´Â °£´ÜÇÑ ¿¹¸¦ ¼±º¸À̱â·Î ÇÑ´Ù. ¿¹¹®Àº °¢ ÁÙ ³¡¸¶´Ù newlineÀÌ ºÙ¾îÀÖ´Â °ÍÀ¸·Î °¡Á¤ÇÑ´Ù.
In France a man who has ruined himself for women is generally regarded
with sympathy and admiration; there is a feeling that it was worth while,
and the man who has done it feels even a certain pride in the fact; in
England he will be thought and will think himself a damned fool. That is
why Antony and Cleopatra has always been the least popular of Shakespeare's
greater plays.
/France/; # 1¹ø° ÁÙ
/(women|there|That) is/; # 1, 2, 4¹ø° ÁÙ
/\we{2}\w/; # feel°ú been, 3, 5¹ø° ÁÙ
/is$/; # 4¹ø° ÁÙ
/^England/; # 4¹ø° ÁÙ
if (/Engl(\w+)/) {
print "Engl$1"; # English, England
}
print if /the/; # there, the, the, 2, 3, 5¹ø° ÁÙ
print if /\bthe\b/; # the, the, 3, 5¹ø° ÁÙ
print if /man|women/; # man, man, 1, 3¹ø° ÁÙ
if ( /(feel(s|ing))/ ) {
print $1; # feeling, feels, 2, 3¹ø° ÁÙ
}
if (/([A-Z]([a-z][a-z]+))/) {
print $2; # rance, ngland, ntony
}
ÁÖ¸ñÇÒ °ÍÀº /man|women/¿¡¼ womenÀ̶ó´Â ´Ü¾î°¡ ´Ü¶ô ³»¿¡ Á¸ÀçÇÔ¿¡µµ ºÒ±¸ÇÏ°í match°¡ ÀϾÁö´Â ¾Ê´Â´Ù´Â
°ÍÀÌ´Ù. ÀÚ¼¼ÇÑ ÀÌÀ¯´Â modifier¿¡ °üÇÑ ¼³¸íÀ» ÂüÁ¶Çϵµ·Ï ÇÏÀÚ. ´ÙÀ½Àº ´Ü¼øÇÑ ±âÈ£µéÀÇ ³ª¿ °°¾Æ¼ ¾à°£ ´õ ¾î·Á¿ï °Í
°°Àº ¿¹Á¦¸¦ °ñ¶óº¸¾Ò´Ù.
(0|0x)\d*\s\1\d* # 0x1234 0x4321°°Àº 16Áø¼ö µÎ °³¿¡¼ matchµÈ´Ù.
/.*foo/ # foo·Î ³¡³ª´Â ´Ü¾î¿¡¼ matchµÈ´Ù.
/^(\d+\.?\d*|\.\d+)$/; # ¿Ã¹Ù¸¥ ¼Ò¼öÇ¥Çö¿¡¼ match°¡ ÀϾÙ.
´ÙÀ½Àº the¸¦ °ü»ç·Î °¡Áö´Â ¸í»çµéÀ» ¸ðµÎ Ãâ·ÂÇÏ´Â ¿¹Á¦ ÇÁ·Î±×·¥ÀÌ´Ù.
while (<>) {
if (/\bthe\b\s+(\w+)/g) {
print "$1\n";
}
}
2) match, substitute, translate
match´Â ÁöÁ¤µÇ¾î ÀÖ´Â regular expressionÀ¸·Î Ç¥Çö °¡´ÉÇÑ substringÀÌ Á¸ÀçÇϴ°¡ÀÇ ¿©ºÎ¸¦ °áÁ¤ÇÒ ¶§
»ç¿ëÇÏ´Â ¿¬»êÀÌ°í, mÀÌ ±× operatorÀÌ´Ù. ÀÌ¿Í ºñ½ÁÇÑ Á¾·ùÀÇ ¿¬»êÀڷμ, regular expressionÀ¸·Î Ç¥ÇöµÈ
substringÀ» ġȯ(substitute)ÇØÁÖ´Â s///¿¬»êÀÚ¿Í Ç¥Çö½ÄÀÇ °¢ ±ÛÀÚ¸¦ Çؼ®(translate)ÇÏ°í º¯È¯ÇØÁÖ´Â
tr///¿¬»êÀÚ°¡ ÀÖ´Ù. ´ÙÀ½Àº °¢ ¿¬»êÀÚµéÀÇ Çü½ÄÀÌ´Ù.
¿¬»êÀÚ//modifier ¶Ç´Â ¿¬»êÀÚ///modifier
¹®Àå ³»¿¡¼ ÈçÈ÷ ½Ç¼öÇϱ⠽¬¿î, teh¶ó°í À߸ø Ä£ ºÎºÐÀ» the·Î ¹Ù²Ù¾î ÁÖ´Â ¿¬»êÀ» ´ÙÀ½°ú °°ÀÌ ±¸ÇöÇÒ ¼ö ÀÖ´Ù.
s/\b(teh)\b/the/g;
g°¡ ¹Ù·Î modifierÀε¥, °¢ ¿¬»ê¸¶´Ù »ç¿ë °¡´ÉÇÑ modifierÀÇ Á¾·ù°¡ ´Ù¸£´Ù.
m/PATTERN/gimosx
/PATTERN/gimosx
match¿¬»êÀÚ´Â ÈçÈ÷ »ý·«Çؼ »ç¿ëÇÏ¿´´Ù. ¾Õ¿¡¼ ÀÍÇû´ø ÆÐÅϸÅÄ¡¿¡¼´Â m¿¬»êÀÚ¸¦ ÀϺη¯ »ç¿ëÇÏÁö ¾Ê¾Ò´ø °ÍÀÌ´Ù.
modifier |
ÀÇ¹Ì |
g |
global, ¹®Àå ³»¿¡¼ ¿©·¯ ¹ø match°¡ ÀϾ ¶§¸¶´Ù |
i |
case-insensitive, ´ë¼Ò¹®ÀÚ ±¸ºÐ ¾øÀÌ |
m |
multiple-line, ¿©·¯ lineÀ» ÇѲ¨¹ø¿¡ »ç¿ëÇÔ |
o |
compile once, compile½Ã¿¡ patternÀ» µü Çѹø Çؼ®ÇÔ |
s |
single-line, stringÀ» ÇÑ lineÀ¸·Î °¡Á¤ÇÔ |
v |
Extended Regular expressionÀ» »ç¿ëÇÔ |
´ÙÀ½ÀÇ µÎ ¿¹Á¦¸¦ ºñ±³ÇØ º¸µµ·Ï ÇÏÀÚ.
if (/(man|women)/g) {
print $1;
}
if (/(man|women)/) {
print $1;
}
ù ¹ø° ¿¹Á¦ÀÇ °á°ú´Â man, women, manÀÌ µÇÁö¸¸, µÎ ¹ø° ¿¹Á¦ÀÇ °á°ú´Â man, man»ÓÀÌ´Ù. °á°ú°¡ ´Þ¶óÁö´Â
ÀÌÀ¯´Â g modifier¶§¹®Àε¥, ÇÑ ¹®Àå¿¡ ¿©·¯ ¹ø match°¡ ÀϾ °æ¿ì, ¸Å¹ø 󸮸¦ ÇÒ °ÍÀÎÁö(g
modifier»ç¿ë), ¾Æ´Ï¸é ´ÙÀ½ ¹®ÀåÀ¸·Î ³Ñ¾î°¥ °ÍÀÎÁö¸¦ modifier¸¦ º¸°í °áÁ¤Çϱ⠶§¹®ÀÌ´Ù.
substitute¿¬»êÀÚ´Â »ý·«°¡´ÉÇÏÁöµµ, µ¿ÀÇ¾î ¿¬»êÀÚµµ Á¸ÀçÇÏÁöµµ ¾Ê´Â´Ù. ±×·¯³ª pattern match·ùÀÇ ¿¬»êÀÚµé Áß¿¡¼ °¡Àå ºó¹øÇÏ°Ô »ç¿ëµÇ´Â ¿¬»êÀÚÀÌ´Ù.
s/PATTERN/REPLACEMENT/egimosx
e |
expression, REPLACEMENTºÎºÐ¿¡µµ expressionÀ» »ç¿ëÇÔ |
g |
¹®Àå ³»¿¡¼ match°¡ ÀϾ ¶§¸¶´Ù ¸Å¹ø ó¸® |
i |
´ë¼Ò¹®ÀÚ ±¸ºÐ ¾øÀÌ |
m |
multi-line mode |
o |
´Ü Çѹø¸¸ compileÇÔ |
s |
single-line mode |
x |
Extended regular expressionÀ» »ç¿ëÇÔ |
´ÙÀ½Àº substitute ¿¬»êÀÚ¸¦ »ç¿ëÇÑ ¿¹ÀÌ´Ù.
s/^([^ ]+) +([^ ]+)/$2 $1/; # µÎ ´Ü¾îÀÇ ¼ø¼¸¦ ¹Ù²Ù¾îÁØ´Ù.
s/red/green/g; # ¹®Àå ³»ÀÇ ¸ðµç red¸¦ greenÀ¸·Î ´ëÄ¡ÇÑ´Ù.
s/\bt(e)(h)\b/t$1$2/g; # teh¸¦ the·Î °íÃÄÁØ´Ù. $1°ú $2´Â backreference
s/(\d)(\d\)\.(\d+)/$1\.$2$3/; # 42.9À» 4.29·Î ¹Ù²Ù´Â ½ÄÀ¸·Î ¼Ò¼öÁ¡À» ¿Å±ä´Ù.
translate¿¬»êÀº À§ÀÇ µÎ ¿¬»ê°ú´Â ¾à°£ ´Ù¸£´Ù. ù ¹ø°·Î´Â string¿¡ ´ëÇÑ Á¶ÀÛÀÌ ¾Æ´Ï¶ó Ç¥Çö½Ä ³»ÀÇ ±ÛÀÚ Çϳª
Çϳª¿¡ ´ëÇÑ Çؼ®°ú º¯È¯¿¡ °ü·ÃµÈ Á¶ÀÛÀÌ°í, µÎ ¹ø°·Î´Â Ç¥Çö½Ä¿¡´Â regular expressionÀ» »ç¿ëÇÏÁö ¾Ê´Â´Ù´Â
Á¡ÀÌ´Ù.
tr/SEARCHLIST/REPLACEMENT/cds;
y/SEARCHLIST/REPLACEMENT/cds;
y´Â trÀÇ µ¿ÀǾî·Î »ç¿ëµÈ´Ù. SEARCHLIST¿¡ ³ª¿À´Â ±ÛÀÚµéÀº REPLACEMENT¿¡ ³ª¿À´Â ±ÛÀÚµé°ú n´ë n ¶Ç´Â n´ë 1·Î ´ëÀÀÀÌ µÇ¾î º¯È¯ÀÌ ÀÌ·ç¾îÁø´Ù. ´ÙÀ½ÀÇ ¿¹Á¦µéÀ» Âü°íÇÏ¿© ÀÌÇØÇϵµ·Ï ÇÏÀÚ.
tr/ABC/XYZ/;
A´Â X·Î, B´Â Y·Î, C´Â Z·Î ¹Ù²ï´Ù. ±×·¯¹Ç·Î ABVWSDC¶ó´Â ¹®ÀÚ¿Àº XYVWSDZ·Î ¹Ù²î°Ô µÇ´Â °ÍÀÌ´Ù.
y/0-9//;
tr [A-Z] [a-z];
¹®ÀÚ¿ ³»ÀÇ ¼ýÀÚ´Â ¸ðµÎ Á¦°ÅµÈ´Ù. ¹®ÀÚ¿ ABC0123DEF´Â ABCDEF·Î ¹Ù²î°Ô µÈ´Ù. ¸ðµç ´ë¹®ÀÚ°¡ ¼Ò¹®ÀÚ·Î ¹Ù²ï´Ù. "I am a BoY"´Â "i am a boy"°¡ µÉ °ÍÀÌ´Ù.
translate¿¬»êÀÌ À§ÀÇ µÎ ¿¬»ê°ú ¸¹ÀÌ ´Ù¸¥ ¸¸Å, modifier ¶ÇÇÑ Å©°Ô ´Ù¸¥ Àǹ̸¦ °¡Áø´Ù.
c |
complement, SEARCHLIST¿¡ ¾ø´Â ±ÛÀÚ°¡ 󸮵ȴÙ. |
d |
delete, ¹ß°ßµÇ¾úÁö¸¸ 󸮵ÇÁö ¾ÊÀº ±ÛÀÚ¸¦ Áö¿î´Ù. |
s |
squash, µ¿ÀÏÇÏ°Ô ¹Ýº¹µÈ ±ÛÀÚ¸¦ »©³½´Ù. |
´ÙÀ½Àº modifier¸¦ »ç¿ëÇÑ translate¿¬»êÀÇ ¿¹ÀÌ´Ù.
tr/a-zA-Z/ /c; # ¾ËÆĺªÀÌ ¾Æ´Ñ ±ÛÀÚ°¡ space·Î ¹Ù²ï´Ù.
tr/a-zA-Z//s; # "I've been a fool."ÀÌ "I've ben a fol."·Î ¹Ù²ï´Ù.
Perl 4±îÁö´Â multi-line/single-line mode¸¦ »ç¿ëÇϱâ À§ÇØ global special variable
ÁßÀÇ $*¸¦ ÀÌ¿ëÇÏ¿´´Ù. pattern match ¿¬»êÀ» Çϱâ Àü¿¡ $*À» 1·Î, $" = ""·Î ÁöÁ¤Çϸé multi-line
mode·Î ¹Ù²Ü ¼ö ÀÖ¾ú´Ù. Perl 5ÀÇ global special variable¿¡ $*°¡ ¾ð±ÞµÇÁö ¾Ê´Â °ÍÀ¸·Î ¹Ì·ç¾îº¸¾Æ,
ÀÌ ¹æ¹ý ´ë½Å¿¡ À§¿¡¼ ¹àÈù ¹Ù¿Í °°ÀÌ modifier¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀÌ ¾ÈÀüÇÒ °ÍÀÌ´Ù.
3) ¹®ÀÚ¿ pattern Á¶ÀÛ
¹®ÀÚ¿ º¯¼ö¿¡ ´ëÇؼ pattern match °ü·Ã ¿¬»êÀ» ÇÏ´Â ¹æ¹ýÀº =~(pattern binding
operator)¿¬»êÀÚ¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¿¬»êÀÚ´Â ÁöÁ¤µÈ º¯¼ö¸¦ pattern¿¡ ´ëÇØ match½ÃÄѺ¸°Å³ª
substituteÇϰųª translateÇϵµ·Ï ÇØÁØ´Ù. ¸¶Ä¡ ´ëÀÔ¿¬»êÀÚ ~=¿Í ºñ½ÁÇÏ°Ô º¸ÀÌÁö¸¸ ´ëÀÔ¿¬»êÀÚ·Î »ç¿ëµÉ ¼ö ¾ø´Â
¿¬»êÀÚÀÌ´Ù.
($a = $b) =~ s/red/green/;
$cnt = tr/*/*/;
$cnt = $sky =~ tr/*/*/;
ù ¹ø° ¹®Àå¿¡¼ °ýÈ£´Â »ý·«°¡´ÉÇѵ¥, ¿ì¼±Àº $b¿¡ ´ëÇؼ substitute¿¬»êÀÌ ÀϾ°í ±× °á°ú °ªÀÌ $a¿¡ µé¾î°¡°Ô
µÈ´Ù. ´Ù½Ã ¸»Çؼ ¹®ÀÚ¿ Áß¿¡¼ red°¡ ¸ðµÎ greenÀ¸·Î ¹Ù²î¾îÁø ¹®ÀÚ¿ Àüü°¡ $a¿¡ ´ëÀԵǴ °ÍÀÌ´Ù. µÎ ¹ø° ¹®Àå¿¡¼
tr¿¬»êÀÚ´Â default input string¿¡¼ '*' character¸¦ ã¾Æ¼('*'´Â interpolation°ú °ü·Ã
¾ø´Ù.) ±× °³¼ö¸¦ ¼¼¾îÁØ´Ù.
vectorÇü ÀڷᱸÁ¶ÀÎ ¹è¿À̳ª Çؽÿ¡ ´ëÇؼ´Â pattern match¸¦ ÇÒ ¼ö°¡ ¾ø°í, ¿ø¼Ò¸¦ Çϳª¾¿ ²¨³»¾î ±×
scalarÇü ¿ø¼Ò¿¡ ´ëÇؼ ÀÛ¾÷À» ¹Ýº¹ÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½ ¿¹Á¦¿¡¼ ³ª¿À´Â scalar¿¬»êÀº vectorÇü º¯¼öÀÇ ¿ø¼ÒÀÇ ¼ö¸¦
¾Ë·ÁÁØ´Ù.
@list = (1, 2, 3, 4, 5, 6, 7);
for ($i = 0; $i < scalar(@list); $i++) {
$list[$i] =~ s/3/9/g;
}
print @list;
file¿¡ ´ëÇؼ pattern match °ü·ÃÀÛ¾÷À» ÇÏ´Â °ÍÀº vectorÇü º¯¼ö¿¡ ´ëÇؼ ÀÛ¾÷ÇÏ´Â °Í°ú ¸¶Âù°¡ÁöÀÇ ¹æ¹ýÀÌ
ÇÊ¿äÇÏ´Ù. ÇÑ ¹®À徿 ÀÐ¾î µé¿©¼ ±× ¹®ÀåÀ» º¯¼ö¿¡ ÀúÀåÇÏ°í ±× º¯¼ö¿¡ pattern match ¿¬»êÀ» ÇÏ´Â °ÍÀÌ´Ù.
open(FILE1, "testfile.1") || die "Can't open such a file!\n";
while () {
s/\r//g;
print;
}
DOS»ó¿¡¼ ¸¸µé¾îÁø ÆÄÀÏÀº ¸®ÅÏ Å°¸¦ ÀÔ·ÂÇÒ ¶§¸¶´Ù \r°ú \nÀÌ ÆÄÀÏ¿¡ ¾²¿©Áö´Âµ¥ ¹ÝÇØ, Unix¿¡¼ ¸®ÅÏ Å°´Â \n¸¸À»
½áÁÙ »ÓÀÌ´Ù. ±×·¡¼ DOSÆÄÀÏÀ» Unix¿¡ ¿Å°Ü¿À´Â °æ¿ì, ^MÀ̶ó´Â ÀÌ»óÇÑ ±ÛÀÚ(½ÇÁ¦·Î´Â \rÀÌ Å͹̳ο¡ º¸ÀÌ´Â ÇüÅÂÀÏ
»ÓÀÌ´Ù.)°¡ ¸Å ÁÙ ³¡¸¶´Ù º¸ÀÌ°Ô µÈ´Ù. ÀÌ·¯ÇÑ ±ÛÀÚ¸¦ Á¦°ÅÇØÁØ °á°ú¸¦ º¸¿©ÁÖ´Â ¿¹Á¦¸¦ Á¦½ÃÇÑ °ÍÀÌ´Ù. Perl script·Î
¾²°Ô µÇ¸é ´õ °£´ÜÇÏ°Ô »ç¿ëÇÒ ¼ö Àִµ¥, ´ÙÀ½ÀÇ Äڵ带 d2u.plÀ̶ó´Â À̸§À¸·Î ÀÛ¼ºÇÑ ÈÄ¿¡, ½ÇÇà °¡´ÉÇÏ°Ô
permission mode¸¦ ¹Ù²Ù¾îÁÖ°í(chmod a+x d2u.pl) µµ½ºÆÄÀÏ¿¡ ´ëÇؼ ½ÇÇà(d2u.pl
dostext.txt)ÇÏ¿© º¸ÀÚ.
#!/usr/local/bin/perl -pi
s/\r//g;
ÀÛ¼ºÀÚ : Á¶¿µÀÏ, ¼¿ï´ëÇб³ Àü»ê°úÇаú 95Çйø
Hello: ¾È³çÇϽʴϱî;¾È³ç;¿©º¸¼¼¿ä;´©±¸¼¼¿ä
|
|
|
|
°Ô½Ã¹°: 115 °Ç, ÇöÀç: 1 / 1 ÂÊ |
|
¹øÈ£ |
Á¦ ¸ñ |
ÀÛ¼ºÀÚ |
µî·ÏÀÏ |
¹æ¹® |
115 |
perl format
|
JMJS |
24.6.22 |
70
|
114 |
perl -p -i -e s/abc/def/g
|
JMJS |
24.3.26 |
92
|
113 |
Deleting from a Hash
|
JMJS |
24.1.23 |
103
|
112 |
yaml file Àбâ
|
JMJS |
23.11.30 |
143
|
111 |
alzipcon
|
JMJS |
22.11.26 |
199
|
110 |
file2date.pl
|
JMJS |
24.6.1 |
316
|
109 |
UTF-8, EUC-KR
|
JMJS |
19.12.15 |
282
|
108 |
wget.pl LWP Simple get
|
JMJS |
19.9.17 |
319
|
107 |
X
|
JMJS |
19.9.17 |
312
|
106 |
X
|
JMJS |
19.9.17 |
316
|
105 |
X
|
JMJS |
19.9.17 |
313
|
104 |
X
|
JMJS |
19.9.17 |
312
|
103 |
X
|
JMJS |
19.9.17 |
309
|
102 |
X
|
JMJS |
19.9.17 |
311
|
101 |
X
|
JMJS |
19.9.17 |
311
|
100 |
jsame.pl
|
JMJS |
09.5.21 |
1826
|
99 |
jos.pl
|
JMJS |
08.11.29 |
1564
|
98 |
jmngr.pl - jmjspro manager
|
JMJS |
08.11.29 |
1602
|
97 |
vhdl.pm
|
JMJS |
08.11.29 |
1567
|
96 |
jreg.pl
|
JMJS |
08.11.29 |
1559
|
95 |
jtree.pm
|
JMJS |
08.11.29 |
1635
|
94 |
w2c.pl
|
JMJS |
08.11.29 |
1601
|
93 |
jdread.pl
|
JMJS |
08.11.29 |
1382
|
92 |
ffile.zip ffilediff.pl
|
JMJS |
09.4.24 |
1429
|
91 |
rgb2ycbcr.pl
|
JMJS |
10.5.12 |
1732
|
90 |
proview.pl
|
JMJS |
08.11.29 |
1773
|
89 |
smi_shift.pl
|
JMJS |
10.10.3 |
1403
|
88 |
srt2smi.pl srtshift.pl
|
JMJS |
18.7.14 |
1602
|
87 |
Perl CGI Input Test, getenv.cgi
|
JMJS |
18.11.21 |
1268
|
86 |
hash
|
JMJS |
24.6.22 |
1550
|
85 |
tr a-z A-Z ġȯ , ´ë¼Ò¹®ÀÚ º¯È¯ , ¹®ÀÚ¿ Á¶°Ç , ...
|
JMJS |
24.6.22 |
1350
|
84 |
JARRAY->jfunc
|
JMJS |
08.10.2 |
1318
|
83 |
¼öÄ¡ÇÔ¼ö - ...
|
JMJS |
09.5.7 |
1787
|
82 |
¿ªÇà·Ä3x3
|
JMJS |
09.5.7 |
1843
|
81 |
hex2int.pl
|
JMJS |
10.9.12 |
2563
|
80 |
stat - file status º¸±â
|
JMJS |
07.12.16 |
1432
|
79 |
Perl Tip
|
JMJS |
24.6.22 |
2575
|
78 |
SMI file sync delay
|
JMJS |
07.7.18 |
1534
|
77 |
rmword.pl
|
JMJS |
07.6.18 |
1449
|
76 |
pack unpack
|
JMJS |
24.6.22 |
1350
|
75 |
[Perl]read_dir.pl
|
JMJS |
07.2.22 |
1632
|
74 |
[Perl]jtab2char - tab¹®ÀÚ¸¦ ƯÁ¤ char·Î ¹Ù²ãÁÖ±â
|
JMJS |
07.2.22 |
1595
|
73 |
Perl ¼ýÀÚ Sorting
|
JMJS |
11.5.17 |
1684
|
72 |
[CGI]GET and POST
|
JMJS |
06.11.20 |
3345
|
71 |
Perl Grammer
|
JMJS |
06.9.4 |
2926
|
70 |
Ç¥ÁØ ¸ðµâ
|
JMJS |
06.8.10 |
1828
|
69 |
Getopt::Long example
|
JMJS |
06.8.10 |
2021
|
68 |
array of array
|
JMJS |
06.4.22 |
1423
|
67 |
[HTML]Å×ÀÌºí ¹è°æ¿¡ ±×¸²³Ö±â
|
JMJS |
06.3.8 |
1514
|
66 |
l2w.pm - line to word
|
JMJS |
08.12.1 |
1505
|
65 |
make_random_chars
|
JMJS |
05.8.10 |
1351
|
64 |
mail º¸³»´Â script
|
JMJS |
05.3.23 |
1319
|
63 |
½ÇÇàÆÄÀÏÀ» ½ÇÇà½ÃÅ°°í Ç¥ÁØ¿¡·¯ Àбâ
|
JMJS |
09.2.25 |
1325
|
62 |
Æ˾÷â(°øÁöâ)¶ç¿ì±â
|
Jinsung |
03.12.16 |
2428
|
61 |
print <<ENDLINE
|
JMJS |
03.12.5 |
1423
|
60 |
[HTML]Target
|
JMJS |
03.5.21 |
1447
|
59 |
[Perl]jutil.pm
|
JMJS |
03.5.3 |
1699
|
58 |
[Perl]jdiff.pl
|
JMJS |
02.6.6 |
1595
|
57 |
[Perl]cfilter.pm - commant filter
|
JMJS |
02.1.31 |
1632
|
56 |
[HTML]ÀÚµ¿À¸·Î ´Ù¸¥ ÆäÀÌÁö À̵¿
|
JMJS |
02.1.19 |
1465
|
55 |
STDIN file open glob -w warning
|
JMJS |
24.6.22 |
1502
|
54 |
opendir , readdir , closedir
|
JMJS |
24.6.22 |
1505
|
53 |
rand_diff
|
JMJS |
01.10.23 |
1401
|
52 |
vcd2v.pl
|
JMJS |
15.8.25 |
1471
|
51 |
file upload
|
JMJS |
24.6.22 |
1424
|
50 |
umask(070) <- chmod o-rwx
|
Áø¼º |
01.3.16 |
1578
|
49 |
head2index
|
Áø¼º |
01.3.15 |
1278
|
48 |
grep.pl
|
JMJS |
11.1.20 |
1401
|
47 |
chr(13) = \M
|
Áø¼º |
01.3.15 |
1269
|
46 |
stty
|
JMJS |
01.3.12 |
1379
|
45 |
des.pl
|
JMJS |
01.3.8 |
1344
|
44 |
chr_plus.pl
|
JMJS |
01.3.8 |
1355
|
43 |
get passwd in unix
|
JMJS |
19.8.18 |
1203
|
42 |
unsort
|
JMJS |
19.9.10 |
1439
|
41 |
read from website web
|
JMJS |
19.5.15 |
1241
|
40 |
vec
|
JMJS |
01.3.8 |
1497
|
39 |
chomp $_ @_ say defined use 5.010 state grep any ...
|
JMJS |
18.10.27 |
1423
|
38 |
[Perl Tk] µµ½ºÃ¢ ¾øÀÌ, ÆÞ Tk ½ºÅ©¸³Æ® ½ÇÇàÇϱâ; ...
|
JMJS |
17.9.30 |
1376
|
37 |
@bs = sort {$a <=> $b} @as;
|
JMJS |
17.7.12 |
1454
|
36 |
if ( -e -z -s -f -d -T -B -M )
|
JMJS |
16.3.10 |
1538
|
35 |
mkmm.pl - make freemind
|
JMJS |
12.5.22 |
1486
|
34 |
perl -pi.orig -e "s/\bu_(\w+_t)\b/u$1/g" *.c
|
JMJS |
11.8.26 |
1484
|
33 |
.xls output csv ÆÄÀÏ¿¡ \n "°¡ ÀÖÀ»¶§
|
JMJS |
11.4.13 |
1367
|
32 |
jdcat.pl juncat.pl
|
JMJS |
11.2.24 |
1513
|
31 |
print chr(ord('a'));
|
JMJS |
01.3.8 |
1424
|
30 |
require "/user/home/jin1/jin1.pm";
|
JMJS |
00.11.28 |
1369
|
29 |
for(my $i=0; $i < @abc; $i ++) {}
|
JMJS |
00.11.15 |
1455
|
28 |
remove blank $abc =~ s/[\s]+//g;
|
JMJS |
00.11.15 |
1397
|
27 |
array and for
|
JMJS |
00.11.14 |
1239
|
26 |
ȨÆäÀÌÁö ÀÚµ¿ À̵¿
|
JMJS |
00.10.28 |
1297
|
25 |
html¿¡¼ ¾²ÀÌ´Â ±âȣǥ½Ã¹æ¹ý
|
JMJS |
00.9.30 |
1252
|
24 |
¹è¿ÀÇ ¹üÀ§ ¿¬»êÀÚ
|
JMJS |
00.9.20 |
1547
|
23 |
for(1..3) { print "$_";}
|
JMJS |
09.4.24 |
1232
|
22 |
package
|
JMJS |
24.6.22 |
1298
|
21 |
ÁÙ¿¡¼ ÇѱÛÀÚ °íÄ¡±â
|
JMJS |
00.8.7 |
1350
|
20 |
hotkey function by getc
|
JMJS |
00.7.21 |
1452
|
19 |
read, sysread, syswrite
|
JMJS |
00.7.21 |
2264
|
18 |
crypt
|
JMJS |
00.7.13 |
1564
|
17 |
csv2gan.pl
|
JMJS |
11.4.4 |
1568
|
16 |
hexa
|
JMJS |
24.6.18 |
1395
|
15 |
file in out.pl
|
JMJS |
00.7.5 |
1584
|
14 |
ARGV
|
JMJS |
00.7.5 |
1419
|
13 |
jtime.pm - localtime, timelocal
|
JMJS |
09.4.9 |
1932
|
12 |
@INC
|
JMJS |
00.7.5 |
1770
|
11 |
$#myarray
|
JMJS |
00.7.5 |
1531
|
10 |
Hash of Array
|
JMJS |
00.7.5 |
1345
|
9 |
package JARRAY JHASH
|
JMJS |
00.7.4 |
1586
|
8 |
foreach, while each, dos2unix.pl, next last
|
JMJS |
09.4.24 |
1558
|
7 |
X
|
JMJS |
24.6.22 |
1363
|
6 |
file lock
|
JMJS |
00.7.4 |
1487
|
5 |
gettime
|
JMJS |
00.7.4 |
1450
|
4 |
[Perl]jcdread.pl
|
JMJS |
00.7.4 |
1531
|
3 |
[Perl]jdread.pl
|
JMJS |
00.7.4 |
1435
|
2 |
Monti Hall problem
|
JMJS |
24.6.27 |
1423
|
1 |
DESTROY
|
JMJS |
09.7.20 |
1377
|
|
|
|
|