1 / 12

Perl File Handling

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);.

Download Presentation

Perl File Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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);

  2. Input/Output to File open (FIL , “some-file”); while ( $_ = <FIL> ) { chop; print STDOUT “some-file has $_ !\n”; } close(FIL);

  3. 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|sunghun.park@mail.utexas.edu|Testing Joe Smith|jsmith@mail.utexas.edu|Hello Everybody

  4. guestbrowse.cgi #!/usr/local/bin/perl require 'cgi-lib.pl'; # Include library + use it print "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);

  5. 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]는 각각의 첨자형변수

  6. 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}”;

  7. 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=text NAME=name></TD></TR> <TR><TD ALIGN=RIGHT><B>E-Mail Address:</B></TD><TD><INPUT TYPE=text NAME=email></TD></TR> <TR><TD ALIGN=RIGHT><B>Short Comment:</B></TD><TD><INPUT TYPE=text NAME=comment></TD></TR> <TR><TD></TD><TD><INPUT TYPE=submit VALUE="Submit To Guestbook"></TD></TR> </TABLE> </CENTER> </FORM> …</HTML>

  8. guestentry.cgi #!/usr/local/bin/perl require "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);

  9. 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’}등은 사용자가 입력한 값을 저장하고 있다

  10. 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/;

  11. Searching in Perl II • If pattern to search is not in $_, use =~ • $a = “hello world”; • $a =~ /^he/; • $a =~ /$b/i

  12. guestsearch.cgi #!/usr/local/bin/perl require '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);

More Related