Perl¤ÎÀâÌÀ
1.¥ª¡¼¥×¥ó¤È¥¯¥í¡¼¥º¡¡¡¡¡¡¡¡ 2.¥Õ¥¡¥¤¥ë¤ÎÆþ½ÐÎÏ¡¡¡¡ 3.¾¤ÎÆþ½ÐÎÏ´Ø¿ô¡¡¡¡
4.¥Õ¥¡¥¤¥ë¤Î¸ºß¥Á¥§¥Ã¥¯¡¡¡¡ 5.¥Õ¥¡¥¤¥ë¤ÎÇÓ¾

¡¡¡¡£·. ¥Õ¥¡¥¤¥ëÁàºî¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£



¡¡1.¥ª¡¼¥×¥ó¤È¥¯¥í¡¼¥º

¥â¡¼¥Éµ­½Ò¡¡
ÆþÎÏ ÆþÎÏ ½ÐÎÏ Äɲà Æþ½ÐÎÏ Æþ½ÐÎÏ Æþ½ÐÎÏ ÆþÎÏ open(FILE,"file"); open(FILE,"<file"); open(FILE,">file"); open(FILE,">>file"); open(FILE,"+<file"); open(FILE,"+>file"); open(FILE,"+>>file"); open FILE; close(FILE); ¥Õ¥¡¥¤¥ë¤¬Ìµ¤¤¤È¥¨¥é¡¼ Ʊ¾å ¥Õ¥¡¥¤¥ë¤Î̵ͭ¤Ë´Ø¤ï¤é¤ººîÀ® Ʊ¾å¡¢¤¢¤ë¤È¤­¤ÏÄɲà ¥Õ¥¡¥¤¥ë¤¬Ìµ¤¤¤È¥¨¥é¡¼ ̵¤±¤ì¤ÐºîÀ®¡£´û¤Ë¤¢¤ì¤Ð¾å½ñ¤­ ̵¤±¤ì¤ÐºîÀ®¡£´û¤Ë¤¢¤ì¤Ð¡¤Äɲà $FILE=¥Õ¥¡¥¤¥ë̾¤ò¥ª¡¼¥×¥ó ³ºÅö¥Õ¥¡¥¤¥ë¤ò¥¯¥í¡¼¥º


FILE	¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥é¤È¸Æ¤Ó¡¢Ä̾ï¤Ï¡¢Ç¤°Õ¤ÎÂçʸ»ú¤Ç½ñ¤­¡¢
	Æþ½ÐÎϤ䥯¥í¡¼¥º¤Ç»ÈÍѤµ¤ì¤Þ¤¹¡£

> 	¤³¤Îµ­¹æ¤Ï¡¢¥Õ¥¡¥¤¥ë¤Î¥ª¡¼¥×¥ó¥â¡¼¥É¤òɽ¤·¤Þ¤¹¡£
file	¥Õ¥¡¥¤¥ë̾¡¡Ëô¤Ï¡¡¥Ñ¥¹Ì¾+¥Õ¥¡¥¤¥ë̾

¢¨µ­¹æ¤ÎÁ°¤Ë¥Ö¥é¥ó¥¯¤¬¤¢¤ë¤È¥ª¡¼¥×¥ó¥¨¥é¡¼¤Ë¤Ê¤ê¤Þ¤¹¡£


¥ª¡¼¥×¥ó¡¡µ­½ÒÎã 1
open(IN, "< infile"); open(IN, "< csv/pgm.csv"); open(IN, "< /file/pgm.csv"); open(IN, "< c:\file/pgm.csv"); $file = 'csv/pgm.csv'; open(IN, "< $file"); open(OUT, "|sort>temp/wkfile.out"); open(IN, "|sort<temp/wkfile.out"); # ¸½¥Õ¥©¥ë¥À¡¼¤Îinfile¤òopen # csv¥Õ¥©¥ë¥Àpgm.csv ¤òopen # ¥ë¡¼¥È»ØÄê¤Î¾ì¹ç # c ¥É¥é¥¤¥Ö»ØÄê¤Î¾ì¹ç # ÊÑ¿ô $file ¤ÎÆâÍÆ¤Ç open # # close ¤ÈƱ»þ¤Ë¥½¡¼¥È¡¢³ÊǼ # open ¤ÈƱ»þ¤Ë¥½¡¼¥È¸åÆþÎÏ


¡Ã¡¡¥Ñ¥¤¥×¤Ç¤¹¡£¼¡¤ÎDos command / unix ¤Î¥³¥Þ¥ó¥É¤ÈƱ¤¸¹Í¤¨Êý¤Ç¤¹¡£ c:\>type infile.out | sort > wkfile.out %cat infile.out | sort > wkfile.out ¢¨ open ʸ¤Ï¡¢"..." ¤ÎÃæ¤Ë unix ¥³¥Þ¥ó¥É¤òÆþ¤ì¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£ ¢¨ Windows ¤Ç¤Ï¡¢¥Õ¥©¥ë¥À¡¼¤Î¥Ñ¥¹¤Î¶èÀÚ¤êʸ»ú¤Ë¡Ö¡ï¡×¤ò»È¤¤¤Þ¤¹¤¬¡¢ ¡¡ unix ¤Ç¤Ï¡¢¡Ö¡¿¡×¤ò»È¤¤¤Þ¤¹¡£ ¡¡ Windows ¤Ç¤â¡Ö¡¿¡×¤¬Í­¸ú¤Ê¤Î¤Ç¡¢¤³¤³¤Ç¤Ï¡¢¡Ö¡¿¡×¤ò»ÈÍѤ·¤Þ¤¹¡£



¡¡2.¥Õ¥¡¥¤¥ë¤ÎÆþ½ÐÎÏ


¥Õ¥¡¥¤¥ë¤ÎÆþÎÏ¡¡µ­½ÒÎã 2
open(IN,"input.txt"); while(<IN>) { print $_; } close(IN); # input.txt ¤ò¥ª¡¼¥×¥ó # ÆþÎϥ쥳¡¼¥É¤Î¤¢¤ë¸Â¤êŽ¤¼¡½èÍý¤ò·«ÊÖ¤¹ # ÆþÎÏÆâÍƤò½ÐÎϤ¹¤ë # ¥¯¥í¡¼¥º

ÆþÎÏ¥¨¥ê¥¢¤Ï¡¢°ÅÌÛ¤ÎÊÑ¿ô¤Ç¤¢¤ë $_ ¤È·è¤á¤é¤ì¤Æ¤¤¤Þ¤¹¡£ ¤â¤·Ç¤°Õ¤ÎÊÑ¿ô̾¤Ë¤·¤¿¤±¤ì¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤·¤Þ¤¹¡£ while($rec = <IN>) { print $rec; }

¥Õ¥¡¥¤¥ë¤Î½ÐÎÏ¡¡µ­½ÒÎã 3
open(OUT,"> out.txt"); print OUT '1st record' . "\n"; print OUT '2nd record' . "\n"; close(OUT); # out.txt ¤ò½ÐÎϥ⡼¥É¤Çopen¤¹¤ë # 1st record¡¡¤ò½ÐÎÏ # 2nd record¡¡¤ò½ÐÎÏ # ¥¯¥í¡¼¥º
¢¨ ¥ì¥³¡¼¥É¤Î¶èÀÚ¤ê¤Ç¤¢¤ë "\n" ¤ò¾Êά¤¹¤ë¤ÈŽ¤£±¥ì¥³¡¼¥É¤Ç¤Ä¤Ê¤¬¤Ã¤Æ¤·¤Þ¤¤¤Þ¤¹¡£


¥Õ¥¡¥¤¥ë¤ÎÆþ½ÐÎÏ¡¡µ­½ÒÎã 4
open(IN,"in.txt"); open(OUT,"out.txt"); while($a = <IN>) { print OUT $a; } close(IN); close(OUT); # in.txt ¤òÆþÎϥ⡼¥É¤Ç¥ª¡¼¥×¥ó # out.txt ¤ò½ÐÎϥ⡼¥É¤Ç¥ª¡¼¥×¥ó # ÆþÎϤ·¤¿¥ì¥³¡¼¥É¤Ë¤Ï¡¢²þ¹Ô¤ò´Þ¤à # ¥¯¥í¡¼¥º #


ɸ½àÆþÎϤÈɸ½à½ÐÎÏ¡¡µ­½ÒÎã 5
$a = <>; print $a; while(<>) { print; } # ɸ½àÆþÎϤ«¤é 1 ¹ÔÆÉ¤ß ÊÑ¿ô $a ¤ËÂåÆþ # $a ¤ò°õºþ(ɽ¼¨)¤·¤Þ¤¹¡£ # ɸ½àÆþÎϤ«¤éÆþÎϤ¬¤Ê¤¯¤Ê¤ë¤Þ¤Ç # ÆþÎϤÎÆâÍƤò°õºþ¤·¤Þ¤¹¡£
¢¨ ɸ½àÆþÎϤϡ¢¥³¥Þ¥ó¥É¥×¥í¥ó¥×¥È²èÌ̤è¤ê¡¢¥­¡¼¥Ü¡¼¥ÉÆþÎÏ É¸½à½ÐÎϤϡ¢¥³¥Þ¥ó¥É¥×¥í¥ó¥×¥È²èÌ̤Ëɽ¼¨ ¢¨ ɸ½àÆþÎϤâɸ½à½ÐÎϤ⡢¥Õ¥¡¥£¥ë¥Ï¥ó¥É¥é¤¬É¬Íפ¢¤ê¤Þ¤»¤ó¡£ ¡¡ Æþ½ÐÎÏÎΰè¤â¾Êά¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤É¤Á¤é¤â¡¡$_ ¤È¤ß¤Ê¤µ¤ì¤Þ¤¹¡£ »ØÄꤷ¤¿¾ì¹ç¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡£ while ( $_ = <>) { print $_; } ¢¨ ¥­¡¼¥Ü¡¼¥ÉÆþÎϤÎÂå¤ï¤ê¤Ë file ¤È¤·¤Æ¡¢pqrm.dat ¤È¤¤¤¦ file ¤ò ¡¡ ¤Ä¤¯¤Ã¤Æ¤½¤³¤Ë¡¢Å¬Åö¤ÊÆâÍƤò½ñ¤­¹þ¤ß¤Þ¤¹¡£dos command ¤Î ¡¡ ¥ê¥À¥¤¥ì¥¯¥·¥ç¥ó¤ò»È¤¦¤È¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡£ > test.pl < pqrm.dat


½ÐÎϸ好¡¼¥È¤·¤Æ¡¢ÆþÎÏ¡¡µ­½ÒÎã 6
open(OUT, "| sort > test.out"); print OUT 3 . "\n"; print OUT 1 . "\n"; print OUT 2 . "\n"; close(OUT); open(IN, "test.out"); while(<IN>) { chop($_); print $_; } close(IN); # ¥¯¥í¡¼¥º¤ÈƱ»þ¤Ë¥½¡¼¥È¤µ¤ì¤ë # ¥½¡¼¥È¸å¤ÎÆþÎÏ # ¥ì¥³¡¼¥É¤ÎºÇ¸åÈø¤Î²þ¹Ô¤òcut # ÆþÎÏÆâÍƤò½ÐÎϤ¹¤ë # 1 2 3 ¤Èɽ¼¨ #



¡¡3.¾¤ÎÆþ½ÐÎÏ´Ø¿ô

getc µ­½ÒÎã
$a=getc; print $a ; open (IN "< infile.txt"); $a = getc(IN); print $a . "\n" ; while(<IN>){ ... # ɸ½àÆþÎϤ«¤é£±Ê¸»úÆþÎÏ # ÆþÎÏÆâÍƤò½ÐÎϤ¹¤ë # ÆþÎÏ¥Õ¥¡¥¤¥ë¤«¤é£±Ê¸»úÆþÎÏ # £²Ê¸»úÌܤ«¤é²þ¹Ô¤Þ¤Ç¤òÆþÎÏ # ¤½¤Î¸å¤ÏÄ̾ï¤É¤ª¤ê¤ÎÆþÎÏ
read µ­½ÒÎã
open(IN, "< test.out"); $rec = 'abcdefghi'; read(IN,$rec,2,5); print $rec . "\n" # ÆþÎϥǡ¼¥¿=12345 ¤Î¤È¤­ # ÆþÎϥǡ¼¥¿¤ÎÀèƬ£²¥Ð¥¤¥È # ¤òÊÑ¿ô¤Î£¶¥Ð¥¤¥ÈÌܤ˥»¥Ã¥È # abcde12hi ¤òɽ¼¨
seek µ­½ÒÎã
open(IN,"number.txt"); $p = 2; seek(IN,$p*22,0); $l = read(IN,$a,22); if ($l != 22) { $a = "error\n"; } chop($a); print "$a\n"; # record size 22 byte ²þ¹Ô´Þ #¥Õ¥¡¥¤¥ë¤ÎÀèƬ¤Ï0¤Ç2¤Ï3ÈÖÌÜ


¢¨ getc ¤Ï¡¢¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥ë¤Ë·ë¤ÓÉÕ¤±¤é¤ì¤¿ÆþÎÏ¥Õ¥¡¥¤¥ë¤«¤é¼¡¤Îʸ ¡¡ »ú¤òÊÖ¤¹´Ø¿ô¤Ç¤¹¡£ ¢¨ read ¤Ï¡¢»ØÄê¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥ë¤«¤é»ØÄꤵ¤ì¤¿¥Ð¥¤¥È¿ôʬ¤òÆɤ߹þ¤ß ¡¡ »ØÄê¤Î¥¹¥«¥é¡¼ÊÑ¿ô¤ËÂåÆþ¤¹¤ë´Ø¿ô¤Ç¤¹¡£ ¢¨ seek(¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥ë,°ÌÃÖ,µ¯ÅÀ) °ÌÃÖ¡¡¡¡µ¯ÅÀ¤«¤é¤Î°ÌÃÖ¡£µ¯ÅÀ¤ÎÃͤ¬1¤«2¤Î¤È¤­¤ÏÉé¤ÎÃͤǤâÎɤ¤ µ¯ÅÀ¡¡¡¡0:¥Õ¥¡¥¤¥ë¤ÎÀèƬ¡¤1:¸½ºß¤Î°ÌÃÖ¡¤2:¥Õ¥¡¥¤¥ë¤ÎËöÈø ¡¡ seek ¤Ï¡¢¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥ë¤Î¸½ºß°ÌÃÖ¤òÀßÄꤹ¤ë´Ø¿ô¤Ç¤¹¡£



¡¡4.¥Õ¥¡¥¤¥ë¤Î¸ºß¥Á¥§¥Ã¥¯

¥Õ¥¡¥¤¥ë¤Î¸ºß¥Á¥§¥Ã¥¯ µ­½ÒÎã
if (!open(fp,"infile")) { # ¥ª¡¼¥×¥ó¤Ç¤­¤Ê¤±¤ì¤Ð¤³¤³¤Ë¤­¤Þ¤¹ } if (!open(fp,"> outfile")) { # ¥ª¡¼¥×¥ó¤Ç¤­¤Ê¤±¤ì¤Ð¤³¤³¤Ë¤­¤Þ¤¹ } open(IN,"file.txt") || die "open error file:$!\n"; open¤Ç¤­¤Ê¤±¤ì¤Ð¡¢¼¡¤Î¥á¥Ã¥»¡¼¥¸¤òɽ¼¨ open error file:No such file or directory # ÆþÎÏ¥Õ¥¡¥¤¥ë # ½ÐÎÏ¥Õ¥¡¥¤¥ë
¥Õ¥¡¥¤¥ë¥Æ¥¹¥È±é»»»Ò¡¡µ­½ÒÎã
if (-e $file) { print "Í­¤ê\n"; } if (!(-e $file)) { print "̵¤·\n"; } if (-z $file) { print "¥µ¥¤¥º0\n"; } $s = -s $file; $u = -M $file; $r = -A $file ¥Õ¥¡¥¤¥ë¤¬¤¢¤ë¤È¤­¡¢Í­¤ê ¥Õ¥¡¥¤¥ë¤¬¤Ê¤¤¤È¤­¡¢Ìµ¤· ¥Õ¥¡¥¤¥ë¤ÎÂ礭¤µ¤¬ 0 ¤« ¥µ¥¤¥º Êѹ¹Æü¿ô ¥¢¥¯¥»¥¹Æü¿ô


±é»»»Ò ÆâÍÆ -e ¥Õ¥¡¥¤¥ë¤¬¤¢¤ë -z ¥Õ¥¡¥¤¥ë¤ÎÂ礭¤µ¤¬0¤Ç¤¢¤ë -s ¥Õ¥¡¥¤¥ë¤¬¤¢¤êÂ礭¤µ¤¬0¤Ç¤Ê¤¤¤È¤­¥µ¥¤¥º¤òÊÖ¤¹ -M ¥Õ¥¡¥¤¥ë¤ÎÆâÍƤ¬ºÇ¸å¤ËÊѹ¹¤µ¤ì¤Æ¤«¤é¤ÎÆü¿ô -A ¥Õ¥¡¥¤¥ë¤¬ºÇ¸å¤Ë¥¢¥¯¥»¥¹¤µ¤ì¤Æ¤«¤é¤ÎÆü¿ô
¥Õ¥¡¥¤¥ë¤ÎÊѹ¹Æü»þ¡¡µ­½ÒÎã
# unix ¤Î¾ì¹ç -rw-r--r-- 1 biglobe users 4219 Feb 8 17:05 test.out # Ms-dos ¤Î¾ì¹ç test.out 1 KB OUT ŽÌŽ§Ž²ŽÙ 14/02/08 17:03
$file = "test.out"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdat) = localtime(time - (-M $file) * 24 * 3600); $mon++; print "$year/$mon/$mday $hour:$min:$sec\n"; # unix ¢ª 02/2/8 17:5:51¡¡¡¡Ms-dos ¢ª 102/2/8 17:5:51






¡¡5.¥Õ¥¡¥¤¥ë¤ÎÇÓ¾

¥Õ¥¡¥¤¥ë¤ÎÇÓ¾ ¥«¥¦¥ó¥¿¡¼¤Îµ­½ÒÎã
open(IN,"+<count.txt"); flock(IN,2); read(IN,$x,4); $x++; if ($x > 9999) { $x = 1; } $x = substr("0000$x",-4); seek(IN,0,0); print IN "$x\n"; close(IN); # Æþ½ÐÎÏ¥ª¡¼¥×¥ó # ¥Õ¥¡¥¤¥ë¤Î¥í¥Ã¥¯ # ÆþÎϤÎ4ʸ»ú¤ò$x¤Ëset # ¥«¥¦¥ó¥È+1 # > 9999 ¤Î¤È¤­ 1 # ¸å¤«¤é4ʸ»ú # ÀèƬ¤Ë°ÌÃ֤Ť± # ÀèƬ4ʸ»ú¹¹¿·
¥Õ¥¡¥¤¥ë¥í¥Ã¥¯¡¡µ­½ÒÎã
$lockkey = 2; $lockfile = "./cupid.lock"; local($flag) = 0; foreach (1 .. 5) { if (-e $lockfile) { sleep(1); } else { open(LOCK,">$lockfile") || &error; close(LOCK); $flag = 1; last; } } if (!$flag) { &error; } if (-e $lockfile) { unlink($lockfile); } # 2=open # ¥í¥Ã¥¯¥Õ¥¡¥¤¥ë̾ # ¥í¥Ã¥¯³«»Ï # ¥í¥Ã¥¯²ò½ü





1.¥ª¡¼¥×¥ó¤È¥¯¥í¡¼¥º¡¡¡¡¡¡¡¡ 2.¥Õ¥¡¥¤¥ë¤ÎÆþ½ÐÎÏ¡¡¡¡ 3.¾¤ÎÆþ½ÐÎÏ´Ø¿ô
4.¥Õ¥¡¥¤¥ë¤Î¸ºß¥Á¥§¥Ã¥¯¡¡¡¡ 5.¥Õ¥¡¥¤¥ë¤ÎÇÓ¾ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ [ Top ]