ãã¡ã¤ã«ãæ¸ãæããã¨ãã«ãåç´ã« open, write, close ã ã¨ããã®ãã¡ã¤ã«ã®æ§æ ¼ã«ãããã¾ãããåé¡ã«ãªãã±ã¼ã¹ãããã¾ãã
- æ¸ãè¾¼ã¿å¦çä¸ã«ç°å¸¸çµäºãã¡ãã£ã
ä¾ãã°
open my $fh, '>', '/foo/bar'; print $fh gen_header(); print $fh gen_section_1(); print $fh gen_section_2(); # â die print $fh "# EOF\n"; close $fh
ãªå¦çã§ãget_section_2 ãç°å¸¸çµäºãã¦ãã¾ãã¨ããã®å¾æ¸ãè¾¼ã¾ããã¯ãã®ãã®ãæ¸ãè¾¼ã¾ãããã¡ã¤ã«ã®å 容ãä¸éå端ã«ãªã£ã¦ãã¾ãã¾ãã(ã¾ãã§ããã®ã±ã¼ã¹ã¯ã¨ã©ã¼å¦çãã¡ããã¨ããã°åé¿ã§ãã¾ãã)
- æ¸ãã¦ããéä¸ã§èªã¾ãã¡ãã£ã
ãµã¼ãåä¸è¦§ã®ãããªå®ç¾©ãã¡ã¤ã«ããæ¯åèªã¾ããã¿ã¤ãã®è¨å®ãã¡ã¤ã«ã®å ´åãçæããã°ã©ã ãæ¸ãã¦ããæä¸ã§ãã ããã«(ä¸éå端ãªç¶æ ã§)èªã¾ãã¡ããå¯è½æ§ãããã¾ãã
open my $fh, '>', '/foo/bar'; for (@totemo_takusanno_data) { print $fh $_; # â ã¾ã èªãããã ãã¼ } close $fh
ãããã®åé¡ãåé¿ããã®ã¯ç¹ã«é£ãããã¨ã§ã¯ãªãã¦ããããã£ã±ã«ããã¨ãã¨ãããããã³ãã©ãªã®ãã¡ã¤ã«ã«æ¸ãã¦ããã¦ãæ¸ãçµãã£ãããç®å½ã¦ã®ãã¡ã¤ã«åã«rename(2)ãããã§OKã§ãã
OKãªãã§ãããæ¯åæ¯åæ¸ãã®ãé¢åãªã®ã§ãè£ã§ãããã£ããä»äºããã¦ããã便å©Perlã¢ã¸ã¥ã¼ã«ãæ¸ãã¾ããã
- http://search.cpan.org/dist/IO-File-AtomicChange/
- http://github.com/hirose31/p5-io-file-atomicchange/tree/master
使ãæ¹ã¯ IO::File ã¨åãæãã§:
use IO::File::AtomicChange; my $fh = IO::File::AtomicChange->new("/foo/bar", "w"); $fh->print("# create new file\n"); $fh->print("foo\n"); $fh->print("bar\n"); $fh->close; # MUST CALL close EXPLICITLY
ãªæãã§ãã¡ã¤ã«ã«æ¸ãè¾¼ãã¨ãå®ã¯è£ã§ã¯ãã³ãã©ãªã®ãã¡ã¤ã«ã«æ¸ãè¾¼ãã§ã$fh->close ããã¿ã¤ãã³ã°ã§rename(2)ãã¦ããã¾ãã
ãã¨ãªãã±æ©è½ã§ãbackup_dir ãæå®ããå ´åã¯ããªãªã¸ãã«ãã¡ã¤ã«ã®ããã¯ã¢ããã³ãã¼ãã¨ã£ã¦ããã¾ãã
ä¾ãã°ã
my $fh = IO::File::AtomicChange->new("/foo/bar", "a", { backup_dir => "/var/backup/" });
ã ã¨ãå¤æ´åã®ãã¡ã¤ã«ãã/var/backup/bar_YYYY-MM-DD_HHMMSS_PID ã«(ãã¼ããã·ã§ã³ãmtimeãä¿æãã¤ã¤)ã³ãã¼ãã¾ãã
ã¡ãªã¿ã«ãªãã§ãããªã¢ã¸ã¥ã¼ã«ãä½ã£ããã¨ããã¨ã9/10(æ¨)ã9/11(é)ã«éå¬ãããYAPC::Asia 2009ã®ãã¼ã¯ã®ããã«ã·ã¹ãã 管çç¨ã®ã³ãã³ããä½ã£ã¦ããéã«å¿ è¦ã«ãªã£ãã®ã§ããã
ã¤ãã³ãããªãã³ã§ããï¼ï¼¾ï½ï¼¾