|
ÆÄÀÏ ´Ù·ç±â
°æ¿ì¿¡ µû¶ó ¿©·¯ºÐÀº ÆÄÀÏ°ú µð·ºÅ丮¸¦ ´Ù·ç¾î¾ß ÇÒ ¶§°¡ ÀÖÀ¸¸®¶ó »ý°¢µË´Ï´Ù.
PERLÀÇ ÆÄÀÏ ´Ù·ç´Â ¼Ø¾¾´Â ¸Å¿ì ÈǸ¢ÇÕ´Ï´Ù. ÆÄÀÏÀÇ »óÅ ¶Ç´Â ÀÌ°ÍÀÌ ÆÄÀÏÀÎÁö µð·ºÅ丮ÀÎÁö ¾Ë¾Æ³»±â À§Çؼ C¿Í °°Àº ¾ð¾î¿¡¼´Â ´ë°³ int86x, fstat °°Àº ½Ã½ºÅÛ ÀÇÁ¸ÀûÀÎ ÇÔ¼ö¸¦ ÀÌ¿ëÇØ¾ß ÇÕ´Ï´Ù. ±×·¯³ª PERL¿¡¼´Â ¾î¶² ¿î¿µÃ¼Á¦¿¡¼µµ °°Àº ¸í·ÉÀ» »ç¿ëÇÕ´Ï´Ù. ´ÜÁö ÂüÁ¶ ¸Å´º¾óÀÌ ¿·¿¡ ÀÖ¾î¾ß ÇÑ´Ù´Â °Í »ÓÀÌÁÒ.
ÆÄÀÏ ¹× µð·ºÅ丮¿¡ ´ëÇØ ¾Ë°í ½ÍÀº °æ¿ì ´ÙÀ½°ú °°Àº ÇÔ¼öµéÀ» »ç¿ëÇÕ´Ï´Ù. ±âº»ÀûÀÎ °Íµé¸¸ ³ª¿ÇÕ´Ï´Ù.(perlfall.htmÀÇ -X FILEHANDLEÀ» º¸½Ê½Ã¿À.)
¹®¹ý: -X FILEHANDLE ¶Ç´Â -X FILE_OR_DIR_NAME
-e ÆÄÀÏÀÌ Á¸ÀçÇϴ°¡ ? (ÂüÀ̸é 1, °ÅÁþÀ̸é 0À» ¸®ÅÏÇÕ´Ï´Ù.)
-z ÆÄÀÏ Å©±â°¡ 0Àΰ¡ ?
-s ÆÄÀÏÀÌ 0 ¾Æ´Ñ Å©±â¸¦ °®´Â°¡ ? (ÆÄÀÏÀÇ Å©±â¸¦ ¸®ÅÏÇÕ´Ï´Ù.)
-f ÆÄÀÏÀÌ Æò¹üÇÑ ÆÄÀÏÀΰ¡ ?
-d ÆÄÀÏÀ̸§ÀÌ µð·ºÅ丮Àΰ¡ ?
-T ÆÄÀÏÀÌ ÅؽºÆ® ÆÄÀÏÀΰ¡ ?
-B ÆÄÀÏÀÌ ÀÌÁø ÆÄÀÏÀΰ¡ ?
-M ÆÄÀÏÀÌ »ý¼ºµÈÁö ¸îÀÏÀÌ µÇ¾ú´Â°¡ ? ( 0ºÎÅÍ ½ÃÀÛ )
±×·³ ÆÄÀÏÀ» ÇÑ ¹ø ÀÐ¾î º¾½Ã´Ù. ÆÄÀÏÀ» Àаųª ¾²±â Àü¿¡´Â ¹Ýµå½Ã ÆÄÀÏ ÇÚµéÀ» »ý¼º, ÆÄÀÏÀ» '¿¾î¾ß' ÇÏ°í, ´Ù ÀÐÀº ÈÄ¿¡´Â '´Ý¾Æ¾ß' ÇÕ´Ï´Ù. ÆÄÀÏ Çڵ鿡´Â º¯¼öµé¿¡ ºÙ´Â $, @, %°°Àº °ÍµéÀÌ ºÙÁö ¾Ê½À´Ï´Ù.
# mytype1.pl
# µµ½ºÀÇ type, À¯´Ð½ºÀÇ cat ¸í·ÉÀ» Èä³»³»¾î º¾½Ã´Ù.
$fileName = "hello.pl";
if( -d $fileName ) # -d $fileName && die "$fileName ...";¿Í °°À½.
{ die "$fileName is a directory.\n"; }
# µð·ºÅ丮¸¦ typeÇÒ ¼ö´Â ¾øÁÒ.
# die ÇÔ¼ö´Â ¸Þ¼¼Áö¸¦ Ãâ·ÂÇÏ°í ÇÁ·Î±×·¥ ½ÇÇàÀ» ÁߴܽÃŵ´Ï´Ù.
-e $fileName || die "$fileName is not exist.\n";
-T $fileName || die "$fileName is not a text file.\n";
open( fileHandle, $fileName ) || die "Cannot open $fileName.\n";
@allLines = <fileHandle>; # ÇÚµéÀ» ÅëÇÏ¿© ¸ðµç ¹®ÀÚ¿À» ¹è¿·Î ÀÐ¾î µéÀÔ´Ï´Ù. ÆÄÀÏÀÌ ³Ê¹« Å©¸é Á» ¹®Á¦°¡ ÀÖ°ÚÁÒ.
close( fileHandle ); # ´Ù ÀоúÀ¸´Ï ²À ´Ý½À´Ï´Ù. !!!!!
print @allLines; # ȸ鿡 Ãâ·ÂÇÕ´Ï´Ù.
¾ÆÁÖ °£´ÜÇÏ°Ô ¸¸µé¾úÁÒ ? ¹®ÀÚ¿À» ÇÑ ÁÙ¾¿ ÀÐÀ» ¼öµµ ÀÖ½À´Ï´Ù. ÇÚµéÀ» ¹è¿¿¡ ÇÒ´ç(=)½ÃÅ°¸é ¸ðµç ¹®ÀÚ¿ÀÌ ´Ù ÀÐÈ÷Áö¸¸ ÀÏ¹Ý º¯¼ö(scalar)¿¡ ÇÒ´ç½ÃÅ°¸é ÇÑ ÁÙ¸¸À» Àаí, ÆÄÀÏÀÇ ³¡¿¡ µµ´ÞÇßÀ» ¶§¿¡´Â "undefined value"(Á¤ÇØÁöÁö ¾ÊÀº °ª ?)À» ¸®ÅÏ ÇÕ´Ï´Ù. ÀÌ undefined value´Â Á¶°Ç½Ä¿¡¼ »ç¿ëÇÏ¸é °ÅÁþÀ¸·Î Àνĵ˴ϴÙ. µû¶ó¼,
open( fileHandle, $fileName ) || die "Cannot open $fileName.\n";
while( $aLine = )
{
print $aLine;
}
close( fileHandle ); # ´Ù ÀоúÀ¸´Ï ²À ´Ý½À´Ï´Ù. !!!!!
¿Í °°ÀÌ ÇÏ¸é ¾ÈÀüÇÏ°Ô ÆÄÀÏÀÇ ³¡±îÁö ÇÑ ÁÙ¾¿ ÀÐÀ¸¸é¼ printÇÒ ¼ö ÀÖ´Â °ÍÀÌÁö¿ä. ÆÄÀÏÀÇ Å©±â°¡ ³Ê¹« Ä¿¼ ¸Þ¸ð¸® »ç¿ëÀÌ °ú´ÙÇÒ °Í °°Àº °æ¿ì¿¡´Â ÀÌ·¸°Ô ÇÑ ÁÙ¾¿ ÀоîµéÀ̴°͵µ ÁÁÀº ¹æ¹ýÀÌ°ÚÁö¿ä.
openÀº ÆÄÀÏÀ» ¿°í close´Â ´Ý´Â ÇÔ¼öÀε¥, openÀÇ ¿©´Â µ¿ÀÛÀº ´Ù½Ã ºÐ·ùÇÏ¿© »ý°¢ÇÒ ÇÊ¿ä°¡ ÀÖ½À´Ï´Ù.
open( fileHandle, "<hello.pl" ); # hello.plÀ» Àб⠸ðµå·Î ¿±´Ï´Ù.
open( fileHandle, "hello.pl" ); # hello.plÀ» Àб⠸ðµå·Î ¿±´Ï´Ù.
open( fileHandle, ">hello.pl" ); # hello.plÀ» ¾²±â ¸ðµå·Î ¿±´Ï´Ù.
open( fileHandle, ">>hello.pl" ); # hello.plÀÇ ³¡¿¡¼ºÎÅÍ Ãß°¡ÇÏ¿©
# ¾²±â ¸ðµå·Î ¿±´Ï´Ù.
open( fileHandle, "+<hello.pl" ); # Àбâ¿Í ¾²±â ¸ðµå·Î ¿±´Ï´Ù.
±×·³ ÆÄÀÏ¿¡ ¾²±âµµ ÇØ º¾½Ã´Ù.
¾²±â¶ó°í °Ì¸ÔÀ» ÇÊ¿ä°¡ ¾ø½À´Ï´Ù. ¸ðµç °ÍÀ» ³í¸®ÀûÀ¸·Î, Á¤È®È÷ ÇØÁÖ¸é »ç°í´Â ¾øÀ¸´Ï±î¿ä. ±×·¯³ª È®½ÇÈ÷ ÀÌÇØÇÏÁö ¸øÇÑ °æ¿ì¿¡´Â ¶Ç »ï°¡ ÇØ¾ß ÇÒ °ÍÀÌ ÆÄÀÏ ¾²±âÀÔ´Ï´Ù.
ÆÄÀÏ¿¡ ¾²±â¸¦ ÇÒ ¶§µµ ±×Àú printÇÔ¼ö¸¦ »ç¿ëÇÏ¸é µË´Ï´Ù. ´ÜÁö ÆÄÀÏ ÇÚµéÀ» ¸í½ÃÇØÁÖ´Â °ÍÀ¸·Î Á·ÇÕ´Ï´Ù. »ç½Ç ÆÄÀÏ ÇÚµéÀÌ ¸í½ÃµÇÁö ¾ÊÀ» °æ¿ì¿¡´Â consolÀÎ STDOUTÀ̶ó´Â Çڵ鿡 print°¡ µÇ´Â °ÍÀÌÁö¿ä.
# write.pl
# perltest.htmÀ̶ó´Â ÆÄÀÏÀÌ ¾ø´Ù°í °¡Á¤ÇÏ°í ÇÁ·Î±×·¥À» ÇÕ´Ï´Ù.
# ÀÖ´Â °æ¿ì¿¡´Â µ¤¾î¾¹´Ï´Ù.
open( fileHandle, ">perltest.html" ) || die "Failed opening.\n";
print fileHandle <<END_HTML; # ´Ù¸¦ ¹Ù°¡ ¾øÁö¿ä ?
<html><head><title>test of writing</title></head>
<body>
<h1>Test of writing file in PERL</h1>
Hello. I am writing something to a file.<P>
Be careful to close the file after completion of writing.<P>
</body>
</html>
END_HTML
close( fileHandle );
À§¿¡¼ print ´ÙÀ½ÀÇ fileHandle´ÙÀ½¿¡ comma°¡ ¾øÀ½À» À¯ÀÇÇØ ÁÖ¼¼¿ä. |
|