2016-10-27 2 views
-1

특정 주소 (포트 번호, 10.xxx : portNumber)에 대한 웹 서버로 펄을 사용하려고합니다. 또한 기본적으로 내 index.html 파일에있는 내용을 보여줍니다. . 그러나 브라우저에서 10.x.x.x : portNumber를 실행할 때 perl은 index.html의 내용을 표시하지 않습니다. 펄에서 파일을 읽으려면 어떻게해야합니까?펄을 index.html 파일에서 읽음

이 코드는 제가이 문제를 해결하기 위해 노력하고 있습니다.

#!/usr/bin/perl 
{ 
package MyWebServer; 
use HTTP::Server::Simple::CGI; 
use base qw(HTTP::Server::Simple::CGI); 
my %dispatch = (
    '/' => \&resp_hello, 
); 


sub handle_request { 
    my $self = shift; 
    my $cgi = shift; 
    my $path = $cgi->path_info(); 
    my $handler = $dispatch{$path}; 
    if (ref($handler) eq "CODE") { 
     print "HTTP/1.0 200 OK\r\n"; 
     $handler->($cgi); 
    } else { 
     print "HTTP/1.0 404 Not found\r\n"; 
     print $cgi->header, 
     $cgi->start_html('Not found'), 
     $cgi->h1('Not found'), 
     $cgi->end_html; 
    } 
} 


sub resp_hello { 
    my $cgi = shift; # CGI.pm object 
    return if !ref $cgi; 
    my $who = $cgi->param('name'); 
    print $cgi->header, 
     $cgi->start_html("Hello"), 
     $cgi->h1("Hello Perl"), 
     $cgi->end_html; 
} 
} 


my $pid = MyWebServer->new(XXXX)->background(); 
print "Use 'kill $pid' to stop server.\n"; 

고맙습니다.

+0

브라우저를 통해 펄 스크립트를 어떻게 호출하고 있습니까? –

답변

0

서버의 루트 URL에 액세스하면 코드가 명시 적으로 구성되어 resp_hello()으로 실행됩니다.

my %dispatch = (
    '/' => \&resp_hello, 
); 

난 당신이 구현을 기대 정확히 URL 구조 모르겠지만, 당신이 //index.html을 차별화하려는 경우, 당신은 같은 것을 할 수있는 :

my %dispatch = (
    '/' => \&resp_hello, 
    '/index.html' => \&resp_index, 
); 

다음을 index.html 파일을 열고 해당 내용을 브라우저로 리턴하는 resp_index() 서브 루틴을 작성하십시오.

필자는 이것을 확장하여 파일 시스템에 직접 존재하는 파일을 직접 제공 할 수 있습니다.

그러나 저는 왜 당신이이 모든 일을하고 있으며 PSGI and Plack을 사용하는 솔루션에 도달하지 않고 있는지 궁금해하고 있습니다. HTTP :: Server :: Simple을 사용하는 아이디어는 어디서 얻었습니까?

그리고 나는 CGI.pm의 HTML 생성 기능을 사용하도록 권장하는 documentation을 보았습니다. 우리는 모두 상당한 시간 동안 그들이 끔찍한 생각 인 것을 알고 있습니다. they are now deprecated.

이 길을 너무 오래 지나기 전에 더 표준 Perl 웹 도구를 조사 할 것을 권합니다.

관련 문제