perl file handling
DESCRIPTION
Perl File Handling. FileHandle : Name for I/O connection STDIN, STDOUT, STDERR are given Other Files Open(FILEHANDLE, “somename”); FILEHANDLE is a name being used in Perl “somename” is a file name in OS Open(OUT, “>outfile”); Open(LOGFILE, “>>mylogfile”); Close(LOGFILE);. - PowerPoint PPT PresentationTRANSCRIPT
Perl File Handling
• FileHandle : Name for I/O connection
• STDIN, STDOUT, STDERR are given
• Other Files– Open(FILEHANDLE, “somename”);– FILEHANDLE is a name being used in Perl– “somename” is a file name in OS– Open(OUT, “>outfile”);– Open(LOGFILE, “>>mylogfile”);– Close(LOGFILE);
Input/Output to File
open (FIL , “some-file”);
while ( $_ = <FIL> ) {
chop;
print STDOUT “some-file has $_ !\n”;
}
close(FIL);
Flat File Database
• A text file database• Each line of the file is a new record in DB• The fields are separated by pipe symbol(|)• guestbook.dat
Sunghun Park|[email protected]|Testing
Joe Smith|[email protected]|Hello Everybody
guestbrowse.cgi
#!/usr/local/bin/perlrequire 'cgi-lib.pl'; # Include library + use itprint "Content-type: text/html\n\n";print "<HTML><h1>The Contents of Guestbook</h1>";print "<TABLE BORDER=1>\n";print "<TR><TD>Name of Guest</TD><TD>E-Mail</TD><TD>Comments from Guest</TD>\n";open(GUESTBOOK,"guestbook.dat") || die "\n<H1>Can't Open Guestbook file!</H1>";while (<GUESTBOOK>) { chop($_); ($name, $email, $comment) = split(/\|/, $_); print "<TR><TD> $name </TD><TD> <A HREF=\"mailto:$email\">$email </A></TD><TD>$comment </TD></TR>\n";}print "</TABLE>\n";close(GUESTBOOK);
Split Function
• Split takes a regular expression and a string• Split returns a list of values• $line = “C:\\;;C:\\Windows\\;”;• @fields = split(/;/, $line);• now $fields is (“C:\”, “”, “C:\Windows”)• @fields 는 첨자형 변수 전체를 지칭• $fields[0], $fields[1], $fields[2] 는 각각의
첨자형변수
Perl 첨자형 변수 (2 가지 )
• 단순한 변수는 $x, $y• $days[28] 는 첨자형 변수
– $#days 는 @days 의 마지막 첨자 값을 표시함– $days[0], $days[1], $days[2], ….$days[$#days]
• $days{‘Feb’} 는 associate array 를 표시– 전체는 %days 로 표시한다– ( 키값 1, 값 1, 키값 2, 값 2, ….)– $days{‘Jan’} 등으로 사용한다– VB 의 ReQuest.QueryString(“name”) 등의 기능은 여기서
왔다– foreach $i (keys %ENV) print “$i = $ENV{$i}”;
guestentry.htm
<HTML> ….<HR><FORM ACTION="http://mis2.myngji.ac.kr/cgi-bin/cgiwrap/shpark/guestentry.cgi” METHOD=POST><CENTER><TABLE WIDTH=50%><TR><TD ALIGN=RIGHT><B>Name:</B></TD><TD><INPUT TYPE=textNAME=name></TD></TR><TR><TD ALIGN=RIGHT><B>E-Mail Address:</B></TD><TD><INPUT TYPE=textNAME=email></TD></TR><TR><TD ALIGN=RIGHT><B>Short Comment:</B></TD><TD><INPUT TYPE=textNAME=comment></TD></TR><TR><TD></TD><TD><INPUT TYPE=submit VALUE="Submit To Guestbook"></TD></TR></TABLE></CENTER></FORM>
…</HTML>
guestentry.cgi#!/usr/local/bin/perlrequire "cgi-lib.pl"; # Include library + use it&ReadParse(*input);print STDOUT "Content-type: text/html\n\n";print STDOUT "<HTML>\n";
open(GUESTBOOK,">>guestbook.dat") || die "<H2>Can't Open Guestbook file!</H2>";
if ( $input{'name'} NE "" ) { print GUESTBOOK "$input{'name'}|$input{'email'}|$input{comment}\n"; print STDOUT "<center>$input{'name'}<p>\n"; print STDOUT "$input{'email'}<p>\n"; print STDOUT "$input{'comment'}<p>\n"; print STDOUT "Added to the Guestbook<p>\n"; }else { print STDOUT "<H2>Please enter at least your name!!</h2>\n";}close(GUESTBOOK);
cgi-lib.pl• Old Perl module(library) for handling CGI stuffs• New version is CGI.pm• After calling &ReadParse(*input);
– &… 는 부프로그램 …를 호출하는 방식– *input 은 첨자형 변수 %input 을 인수로 넘겨줌– $ENV{…} 환경변수들이 제공된다
• “REQUEST_METHOD”, “CONTENT_LENGTH”• $ENV{‘REQUEST_METHOD’}
– $input{…} Form 변수들이 제공된다• “name”, “email” 등은 Form 에서 사용한 필드명들• $input{‘name’}, $input{‘email’} 등은 사용자가 입력한 값을
저장하고 있다
Searching in Perl I
• Powerful pattern matching with regular expression to a string
• More general than wild card file or dir name
• while (<>) {– if ( /ab*c/ ) {
• print $_;
– } }
• substitue operator– s/abc*c/def/;
Searching in Perl II
• If pattern to search is not in $_, use =~
• $a = “hello world”;
• $a =~ /^he/;
• $a =~ /$b/i
guestsearch.cgi#!/usr/local/bin/perlrequire 'cgi-lib.pl';# Include library + use it&ReadParse(*input);print "Content-type: text/html\n\n";print "<HTML><h1>The Messages from Guest $input{'name'}</h1>";print "<TABLE BORDER=1>\n";print "<TR><TD>Name of Guest</TD><TD>E-Mail</TD><TD>Comments from Guest</TD>\n";open(GUESTBOOK,"guestbook.dat") || die "\n<H1>Can't Open Guestbook file!</H1>";while (<GUESTBOOK>) { chop($_); ($name, $email, $comment) = split(/\|/, $_); if ( $name =~ /$input{'name'}/i ) { print "<TR><TD> $name </TD><TD> <A HREF=\"mailto:$email\">$email </A></TD><TD>$comment </TD></TR>\n"; }}print "</TABLE>\n";close(GUESTBOOK);