±Û¾´ÀÌ Guido Socher ±Û¾´ÀÌ ¼Ò°³: Guido´Â ¿À·§µ¿¾È ¸®´ª½º ÆÒÀ̾ú°í Perl ÇÙÄ¿¿´´Ù. ¿äÁò ±×´Â ÁýÀ» »õ·Ó°Ô ´ÜÀåÇÏ°í Á¤¿ø¿¡ »ø·¯µå¿Í ´Ù¸¥ °ÍµéÀ» ½É¾î Å°¿ì´Âµ¥ ¹«Ã´ ¹Ù»Ú´Ù. Â÷·Ê: |
¿ä¾à:
Perl ù¹ø°¿¡¼´Â ÆÞÀÇ ÀϹÝÀûÀÎ ³»¿ëÀ» »ìÆ캸¾Ò´Ù.
perl part II¿¡¼´Â ù¹ø° ½ÇÁ¦ÀûÀÎ ÇÁ·Î±×·¥À» ÀÛ¼ºÇÑ´Ù.
!/usr/bin/perl -w
# vim: set sw=8 ts=8 si et: # # uncomment strict to make the perl compiler very # strict about declarations: #use strict; # global variables: use vars qw($opt_h); use Getopt::Std; # &getopts("h")||die "ERROR: No such option. -h for help\n"; &help if ($opt_h); # #>>your code<< # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- sub help{ print "help message\n"; exit; } #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- __END__ |
Äڵ带 º¸¸é &getopts()´Â ¸í·É¾î ¿É¼ÇÀ» Àбâ À§ÇØ ¶óÀ̺귯¸® Getopt::Std
¿¡ ÀÖ´Â ¼ºê·çƾÀ» È£ÃâÇÑ´Ù. ÀÌ ÇÔ¼ö´Â ¸í·É¾î ¿É¼Ç¿¡ ÀÇÇØ Á¦°øµÈ Àü¿ª º¯¼ö $opt_<option>¸¦
¼³Á¤ÇÑ´Ù.
¸í·É¾î »óÀÇ ¸ðµç ¿É¼ÇÀº "-" (minus sign)À¸·Î ½ÃÀÛÇÏ°í ÇÁ·Î±×·¥ À̸§°ú ´Ù¸¥ º¯¼ö »çÀÌ¿¡ ¿Â´Ù.
(Note: ÀÌ°ÍÀº ÀϹÝÀûÀÎ Unix ±ÔÄ¢ÀÌ´Ù.)
&getopts¿¡ ÁÖ¾îÁø ¹®ÀÚ¿(À§ÀÇ ÇÁ·Î±×·¥¿¡¼´Â "h")Àº ÇÁ·Î±×·¥ÀÌ Çã¿ëÇÏ´Â ¸ðµç
¿É¼ÇÀ» ³ª¿ÇÏ°í, ¸¸¾à ¿É¼ÇÀÌ ÄÝ·ÐÀ¸·Î µÈ º¯¼ö¸¦ °¡Áö°í ÀÖ´Ù¸é ¿É¼Ç ¹®ÀÚ´Â ¹Ýµå½Ã Äݷеڿ¡ ÀÖ¾î¾ß ÇÑ´Ù.
&getsopt("d:x:h")´Â ÇÁ·Î±×·¥ÀÌ -d, -x, -h¶ó´Â ¿É¼ÇÀ» °¡Áö°í ÀÖ´Ù´Â ÀǹÌÀÌ´Ù.
¿É¼Ç -d¿Í -x´Â ¾Æ±Ô¸ÕÆ®¸¦ °¡Áö°í Àֱ⠋š¹®¿¡ "-o somgthing"Àº ¸Â´Â °ÍÀÌÁö¸¸
"-o -x foo"´Â -o µÚ¿¡ ¾Æ±Ô¸ÕÆ®°¡ ¾ø±â ¶§¹®¿¡ Ʋ¸°°ÍÀÌ´Ù.
¸¸¾à ¿É¼Ç -h°¡ ¸í·É¾î¿¡¼ ÁÖ¾îÁø´Ù¸é $opt_hº¯¼ö´Â &help·Î Á¤ÇØÁø´Ù.
±×¸®°í ($opt_h);´Â help·Î Á¤ÇØÁö¹Ç·Î ¿É¼Ç -h°¡ ¸í·É¾î¿¡¼ ÁÖ¾îÁö¸é
¼ºê·çƾ help°¡ ºÒ·ÁÁø´Ù°í ÇÒ ¼ö ÀÖ´Ù.
sub help{¹®ÀåÀº ¼ºê·çƾÀ» ¼±¾ðÇÏ´Â °ÍÀÌ´Ù. Áö±Ý À̼ø°£
ÀÌ ¸ðµç ÄÚµåÀÇ ÀÚ¼¼ÇÑ ºÎºÐÀ» ÀÌÇØÇÏ´Â °ÍÀ» Áß¿äÄ¡ ¾Ê´Ù. ´ÜÁö ÇÊ¿äÇÒ¶§ ´ç½ÅÀÇ
ÁÖ¿ä ±â´É¿¡ ÀÌ templateÀ» »ç¿ëÇÒ ¼ö ÀÖÀ¸¸é µÈ´Ù.
#!/usr/bin/perl -w
# vim: set sw=8 ts=8 si et: # # uncomment strict to make the perl compiler very # strict about declarations: #use strict; # global variables: use vars qw($opt_d $opt_x $opt_h); use Getopt::Std; # &getopts("d:x:h")||die "ERROR: No such option. -h for help\n"; &help if ($opt_h); if ($opt_d && $opt_x){ die "ERROR: options -x and -d are mutual exclusive.\n"; } if ($opt_d){ printf("decimal: %d\n",hex($opt_d)); }elsif ($opt_x){ printf("hex: %X\n",$opt_x); }else{ # wrong usage -d or -x must be given: &help; } #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- sub help{ print "convert a number to hex or dec. USAGE: numconv [-h] -d hexnum umconv [-h] -x decnum OPTIONS: -h this help
|
ˤ˂
numconvÇÁ·Î±×·¥À» ´Ù¿î·Îµå ¹ÞÀ¸·Á¸é Ŭ¸¯ÇϽÿÀ.
´ÙÀ½ Àå¿¡¼´Â ¿ì¸®´Â ÀÌ ÇÁ·Î±×·¥¿¡ ´ëÇØ ´õ¿í Ä£¼÷ÇØÁú°ÍÀÌ´Ù. ÀÌÇØÇØ µÎ±â¸¦ ¹Ù¶õ´Ù.
BLOCKÀº {}¾È¿¡ ÀÖ´Â ¹®ÀåÀÇ ¼ö¸¦ ÀǹÌÇÑ´Ù. ÀÌ°ÍÀº If¹®ÀåÀÌ ´ÙÀ½°ú °°ÀÌ
¾²ÀÏ ¼ö ÀÖ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù.:
printf("hello\n") if ($i);
if ($i == 2){
|
ÆÞ¿¡¼µµ C¿¡¼ ¾²´Â °Íó·³ && ¿Í || ¿¬»êÀÚ¸¦ ¾µ ¼ö ÀÖ´Ù.
printf("hello\n") if ($i);
À§¿Í °°Àº ÄÚµå´Â ¾Æ·¡¿Í °°ÀÌ ¾²ÀÏ ¼ö ÀÖ´Ù.
($i) && printf("hello\n");
Ưº°È÷ ||´Â template¾È¿¡¼ ³¡ÀÌ ³ ¹®Àå¾È¿¡ ¾² ÀÏ ¼ö ÀÖ´Ù.
&getopts("d:x:h")||die "ERROR\n";
"¿É¼ÇÀÌ ÀÖÀ¸¸é °¡Á®¿À°í ¾øÀ¸¸é ³¡³´Ù.". die() ÇÔ¼ö´Â ±âº»ÀûÀ¸·Î ÇÁ·Î±×·¥À» ³¡³»°í
ÇÁ¸°Æ® ¹®À» ¼öÇàÇÏ´Â °Í°ú °°Àº ±â´ÉÀ» ÇÑ´Ù. ÀÌ ÇÔ¼ö´Â ¸Þ½ÃÁö¸¦ ÇÁ¸°Æ®ÇÏ°í ÇÁ·Î±×·¥À»
³¡³»°Ô µÈ´Ù.
&getopts("d:x:h")||die "ERROR\n";
ÀÌ°ÍÀº ´ÙÀ½°ú °°Àº ¹®ÀåÀÌ´Ù.
die "ERROR\n"; if (! &getopts("d:x:h"));
"!"Àº ³í¸®ÀûÀ¸·Î ºÎÁ¤ ¿¬»êÀÚÀÌ´Ù. ´Ù½Ã ÀÌ°ÍÀº ´ÙÀ½°ú °°ÀÌ ¾²ÀÏ ¼ö ÀÖ´Ù.
die "ERROR\n"; unless (&getopts("d:x:h"));
unless´Â if-not°ú °°Àº °ÍÀÌ°í if(!..) º¸´Ù ÀбⰡ ÆíÇÏ´Ù.
´ç½ÅÀÌ º»°Í°ú °°ÀÌ ÆÞ¿¡¼´Â if-¹®ÀåÀ» ¾²´Âµ¥ ÇÑ°¡Áö ¹æ¹ýÀÌ Àִ°ÍÀÌ ¾Æ´Ï´Ù.
ÀÌ ¸ðµç ¹æ¹ýÀ» ´Ù ¾µ ÇÊ¿ä´Â ¾ø°í, ´ç½ÅÀÌ ¾²±â¿¡ ÆíÇÑ ¹æ¹ýÀ» ¾²¸é µÈ´Ù.
|
#!/usr/bin/perl
use strict; my $i=1; print "i is $i\n"; |
ÀÌ ÇÁ·Î±×·¥Àº Á¤È®ÇÏ°í i´Â 1À̶ó´Â °á°ú¸¦ Ãâ·ÂÇÑ´Ù. ÀÌÁ¦ ½Ç¼ö¿¡ ÀÇÇØ i ´ë½Å¿¡ j¸¦ ½è´Ù°í »ý°¢Çϸé:
#!/usr/bin/perl
# $i=1; print "i is $j\n"; |
ÀÌ ÄÚµå´Â ÆÞ¿¡¼ Àß ¼öÇàµÉ°ÍÀÌ°í "i is "¶ó´Â °á°ú¸¦ Ãâ·ÂÇÑ´Ù.
ÆÞ ¸ðµâ "use strict;"´Â ÄÄÆÄÀÏ·¯¿¡°Ô ÀÌ·¯ÇÑ ÇÁ·Î±×·¥ÀÌ À߸ø‰ç´Ù´Â °ÍÀ» ¾Ë¸°´Ù.
"strict"¸¦ ¾µ¸é ¸ðµç º¯¼ö´Â ¼±¾ðµÇ¾ß µÇ°í À§ÀÇ ÇÁ·Î±×·¥Àº ¿¡·¯ ¸Þ½ÃÁö°¡ Ãâ·ÂµÈ´Ù.
#!/usr/bin/perl
use strict; my $i=1; print "i is $j\n"; |
ÀÌ ÇÁ·Î±×·¥Àº ´ÙÀ½°ú °°Àº ¸Þ½ÃÁö¸¦ Ãâ·ÂÇÏ°í ¿¡·¯ Ãâ·ÂÀ» ½±°Ô ÇÑ´Ù.
Global symbol "$j" requires explicit package name at ./vardec line 4. Execution of ./vardec aborted due to compilation errors. Exit 255ÆÞ¿¡¼ º¯¼ö´Â "my"¸¦ »ç¿ëÇÏ¿© ¼±¾ðµÇ°Å³ª framework¿¡¼ º¸ÀΰͰú °°ÀÌ "use vars qw()"¸¦ »ç¿ëÇÏ¿© ¼±¾ðµÈ´Ù.:
Àü¿ª º¯¼ö´Â use vars¿¡ ÀÇÇØ ¼±¾ðµÈ´Ù.
ÀÌ·¯ÇÑ º¯¼öµéÀº ¶óÀ̺귯¸®¸¦ Æ÷ÇÔÇÏ´Â ¸ðµç Äڵ忡¼ Àü¿ªÀûÀÌ´Ù.
ÇöÀç ÇÁ·Î±×·¥ ÈÀϾȿ¡¼ Áö¿ªÀûÀÎ º¯¼ö(ÈÀϾÈÀÇ ¸ðµç ¼ºê·çƾ¾È¿¡¼´Â
Àü¿ªÀûÀÌ´Ù.)´Â ¼ºê·çƾ ¹Ù±ùÀÇ ÇÁ·Î±×·¥ÀÇ ½ÃÀۺκп¡ my
¸¦ »ç¿ëÇÏ¿© ¼±¾ðµÈ´Ù.
ÇöÀç ¼ºê·çƾ ¾È¿¡¼ Áö¿ªÀûÀÎ º¯¼ö´Â ¼ºê·çƾ ¾È¿¡¼
my¸¦ »ç¿ëÇÏ¿© ¼±¾ðµÈ´Ù.
»ç¶÷µéÀº ½© ÇÁ·Î±×·¡¹Ö¿¡¼ º¯¼ö¸¦ ¼±¾ðÇϰųª °ªÀ» ÇÒ´çÇÒ ¶§ $ Ç¥½Ã¸¦ ¹«½Ã ÇÏ´Â °æÇâÀÌ ÀÖ´Ù. ÀÌ°ÍÀº ÆÞ¿¡¼´Â ºÒ°¡´ÉÇÏ¸ç ½ºÄ®¶ó º¯¼ö¸¦ »ç¿ëÇÒ¶§´Â Ç×»ó $ Ç¥½Ã¸¦ ½á¾ßÇÑ´Ù.
±×¸®°í º¯¼ö¸¦ ¼±¾ðÇÒ¶§ °ªÀ» Á÷Á¢ ÇÒ´çÇÒ¶§µµ ¾²ÀÏ ¼ö ÀÖ´Ù.
my $myvar=10; ´Â $myvar¸¦ ¼±¾ðÇÏ°í ÀÌ º¯¼ö¿¡ ÃʱⰪÀ¸·Î 10À» ÇÒ´ç
ÇÑ´Ù´Â ÀǹÌÀÌ´Ù.
#!/usr/bin/perl
use strict; my $result; my $b; my $a; $result=&add_and_duplicate(2,3); print "2*(2+3) is $result\n"; $b=5;$a=10;
# add two numbers and multiply with 2:
|
|
AC Access Class AC Air Conditioning AFC Automatic Frequency Control AFS Andrew File System ...±× ¸®½ºÆ® ÈÀÏÀº ÀÌ°÷¿¡¼ ´Ù¿î·Îµå ÇÒ ¼ö ÀÖ´Ù. ÀÌ ÆÄÀÏÀÇ ¹®¹ýÀº ´ÙÀ½°ú °°´Ù.:
.... open(FD,"abb.txt")||die "ERROR: can not read file abb.txt\n"; while( #do something } close FD; .... |
open ÇÔ¼ö´Â ÆÄÀÏ Áö½ÃÀÚ¸¦ ù¹ø° Àü´ÞÀÎÀÚ·Î °®°í ÆÄÀÏ À̸§À» µÎ¹ø°
Àü´ÞÀÎÀÚ·Î °¡Áø´Ù. ÆÄÀÏ Áö½ÃÀڴ Ưº°ÇÑ º¯¼öÀÌ´Ù. ÀÌ°ÍÀ» openÇÔ¼ö¿¡ ¾²¸é
ÇÔ¼ö ¾È¿¡¼ ÆÄÀϾȿ¡¼ ÀڷḦ ÀÐÀ» ¼ö ÀÖ°í, ¸¶Áö¸·À¸·Î close ÇÔ¼ö¿¡ Àü´ÞÇÑ´Ù.
<FD>·Î ÆÄÀÏÀ» Àд °ÍÀÌ ³¡³ª¸é <FD>´Â while loop¿¡ ÀÎÀÚ·Î ÁÖ¾îÁú ¼ö ÀÖ°í
ÀÌ°æ¿ì ÁÙ ´ÜÀ§·Î ÆÄÀÏÀ» Àдµ¥ »ç¿ëµÇ°Ô µÈ´Ù.
ÀüÅëÀûÀ¸·Î ÆÄÀÏ Áö½ÃÀÚ´Â ÆÞ¿¡¼ ´ë¹®ÀڷΠǥ½ÃµÈ´Ù.
ÀÐÇôÁø ÀÚ·á´Â ¾îµð¿¡ ÀÖÀ»±î? ÆÞÀº ¸î°¡ÁöÀÇ ¹¬½ÃÀûÀÎ º¯¼ö¸¦ °¡Áö°í ÀÖ´Ù.
ÀÌ·± º¯¼öµéÀº ¼±¾ð ¾øÀÌ ¾²À̴µ¥ Ç×»ó Á¸ÀçÇÑ´Ù. ±×Áß ÇÑ°¡Áö·Î $_ º¯¼ö°¡ ÀÖ´Ù.
ÀÌ º¯¼ö´Â À§ÀÇ while loop¿¡¼ ÇöÀç ÀоîµéÀÎ ÇÑ ¶óÀÎÀÇ Á¤º¸¸¦ °¡Áö°í ÀÖ´Ù.
Çѹø ´ÙÀ½ÀÇ Äڵ带 ½ÇÇà Çغ¸±â¸¦:(ÄÚµå ¹Þ±â
):
#!/usr/bin/perl
use strict; my $i=0; open(FD,"abb.txt")||die "ERROR: can not read file abb.txt\n"; while(<FD>){ # increment the line counter. You probably # know the ++ from C: $i++; print "Line $i is $_"; } close FD; |
|
º¸ÀÌ´Â °Íó·³ À§ÀÇ ÄÚµå´Â print "Line $i is $_ \n"¿Í °°ÀÌ ¾²ÀÌÁö ¾Ê¾Ò´Âµ¥, ÀÌ°ÍÀº $_ º¯¼ö°¡ ÅؽºÆ® ÈÀÏ ¾È¿¡¼ °³Çà ¹®ÀÚ(\n)¸¦ °°ÀÌ ÀÐ¾î µé¿´±â ¶§¹®ÀÌ´Ù.
ÀÌÁ¦ ¿ì¸®´Â ÆÄÀÏÀ» Àд ¹æ¹ýÀ» ¾Ë¾Ò´Ù. È®½ÇÇÏ°Ô ¿ì¸®ÀÇ ÇÁ·Î±×·¥À» ³¡³»±â À§Çؼ´Â ¿ì¸®´Â µÎ°¡Áö »çÇ×À» ´õ ¹è¿ö¾ß ÇÑ´Ù.
#!/usr/bin/perl -w
# vim: set sw=8 ts=8 si et: # use strict; # global variables: use vars qw($opt_h); my $i=0; use Getopt::Std; # &getopts("h")||die "ERROR: No such option. -h for help.n"; &help if ($opt_h); # open(FD,"abb.txt")||die "ERROR: can not read file abb.txt\n"; while(<FD>){ $i++; if (m/^(\S+)\s/){ # $1 holds now the first word (\S+) print "$1 is the abbreviation on line $i\n"; }else{ print "Line $i does not start with an abbreviation\n"; } } close FD; # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- sub help{ print "help text\n"; exit; } #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- __END__ |
¸¸¾à Á¤±Ô Ç¥ÇöÀÌ ÇöÀç ¶óÀο¡ ¼º°øÀûÀ¸·Î Àû¿ëÀÌ µÇ¸é, ÀÏÄ¡ ¿¬»êÀÚ (m/ /)´Â
1À» ¹ÝȯÇÕ´Ï´Ù. ±×·¯¹Ç·Î, if-¹®Àå¾È¿¡ ÀÌ °ÍÀ» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
$1 º¯¼ö´Â ½ÇÁ¦·Î È®½ÇÇÑ ÀÚ·á°¡ ÀÖ´ÂÁö È®ÀÎµÇ¾ß Çϱ⠶§¹®¿¡ $1 º¯¼ö¸¦¸¦ »ç¿ëÇϱâ Àü¿¡
´Â ²À ÀÏÄ¡ ¿¬»êÀÚ¿¡ if-¹®ÀåÀ» »ç¿ëÇÏ¿©¾ß ÇÑ´Ù.
#!/usr/bin/perl -w
my %htab; my $index; # load the hash with data: $htab{"something"}="value of something"; $htab{"somethingelse"}=42; # get the data back: $index="something"; print "%htab at index \"$index\" is $htab{$index}\n"; $index="somethingelse"; print "%htab at index \"$index\" is $htab{$index}\n"; |
ÀÌ ÇÁ·Î±×·¥ÀÌ ½ÇÇàµÉ¶§ ¿ì¸®°¡ ¾òÀ» ¼ö ÀÖ´Â °ÍÀº:
%htab at index "something"Àº "value of something"ÀÌ°í %htab at index "somethingelse" Àº 42¶ó´Â °ÍÀÌ´Ù.ÀÌÁ¦ ¿ì¸®ÀÇ ÇÁ·Î±×·¥Àº ³¡ÀÌ ³´Ù:
1 #!/usr/bin/perl -w
2 # vim: set sw=4 ts=4 si et: 3 # 4 use strict; 5 # global variables: 6 use vars qw($opt_h); 7 my %htab; 8 use Getopt::Std; 9 # 10 &getopts("h")||die "ERROR: No such option. -h for help.n"; 11 &help if ($opt_h); 12 # 13 open(FD,"abb.txt")||die "ERROR: can not read file abb.txt\n"; 14 print "Abbreviations with several meanings in file abb.txt:\n"; 15 while(<FD>){ 16 if (m/^(\S+)\s/){ 17 # we use the first word as index to the hash: 18 if ($htab{$1}){ 19 # again this abbrev: 20 if ($htab{$1} eq "_repeated_"){ 21 print; # same as print "$_"; 22 }else{ 23 # this is the first duplicate we print first 24 # occurance of this abbreviation: 25 print $htab{$1}; 26 # print the abbreviation line that we are currently reading: 27 print; 28 # mark as repeated (= appears at least twice) 29 $htab{$1}="_repeated_"; 30 } 31 }else{ 32 # the first time we load the whole line: 33 $htab{$1}=$_; 34 } 35 } 36 } 37 close FD; 38 # 39 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 40 sub help{ 41 print "finddup -- Find abbreviations with several meanins in the 42 file abb.txt. The lines in this file must have the format: 43 abrev meaning 44 \n"; 45 exit; 46 } 47 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 48 __END__ |
ÀÌ ÇÁ·Î±×·¥Àº ¾î¶»°Ô ÀÛµ¿Çϴ°¡? ÀÌ ÇÁ·Î±×·¥Àº ¶óÀÎ ´ÜÀ§·Î ÆÄÀÏÀ» Àаí, ±× ¶óÀÎÀ» %htab (line 33)¶ó´Â Çؽ¬ Å×ÀÌºí¿¡ ÀúÀåÇÑ´Ù. How does it work? We read the file line by line and store the lines in our hash called %htab (line 33). Çؽ¬ Å×À̺íÀÇ À妽º´Â ¾à¾îÀÌ´Ù. Çؽ¬ Å×À̺íÀ» »ç¿ëÇϱâ Àü¿¡ ±× ¾È¿¡ ´Ù¸¥ °ªÀÌ ÀúÀåµÇ¾î ÀÖ´ÂÁö È®ÀÎÇÑ´Ù. (line 18). ¸¸¾à Çؽ¬ Å×À̺í¾È¿¡ ´Ù¸¥ °ªÀÌ ÀÖÀ¸¸é µÎ °¡Áö °¡´É¼ºÀÌ ÀÖ´Ù.
ÀÌ ÄÚµå´Â
´ÙÀ½¿¡¼ ´Ù¿î ¹ÞÀ» ¼ö ÀÖ°í Çѹø ½ÇÇàÇØ º¸±â¸¦ ¹Ù¶õ´Ù.
º» À¥ ÆäÀÌÁö´Â ¸®´ª½ºÆ÷Ä¿½º ¿¡µðÅÍ ÆÀ¿¡ ÀÇÇØ °ü¸® µË´Ï´Ù.
© Guido Socher LinuxFocus 1999 |
¹ø¿ªÁ¤º¸:
|
1999-11-01, generated by lfparser version 0.8