2014-07-09 5 views
0

자바 스크립트에서 펄 스크립트를 호출하고 싶습니다. Perl 스크립트는 한 폴더에서 다른 폴더로 파일을 이동/복사합니다. 그러나 내가 그것을 호출하려고하면 실행되지 않습니다.자바 스크립트에서 펄 스크립트 호출하기

저는이 분야에서 새로운 편이어서 약간의 도움이 많은 도움이 될 것입니다.

copy_file.pl 당신이 당신의 자바 스크립트 오류 콘솔을 보면 당신이 $이 정의되어 있지 않은 것을 불평 것을 볼 수

<!DOCTYPE html> 
<html> 
    <body> 
    <h1>My First JavaScript</h1> 
    <p>Click Date to display current day, date, and time.</p> 
    <button type="button" onclick="myFunction()">Date</button> 
    <p id="demo"></p> 
    <script> 
     function myFunction() { 
     document.getElementById("demo").innerHTML = Date(); 
     $.get("copy_file.pl"); 
     } 
    </script> 
    </body> 
</html> 

답변

1

#!/usr/bin/env perl 
use strict; 
use warnings; 

use File::Copy; 

my $source_dir = "/home/Desktop/file"; 
my $target_dir = "/home/Desktop/Perl_script"; 

opendir(my $DIR, $source_dir) || die "can't opendir $source_dir: $!"; 
my @files = readdir($DIR); 

foreach my $t (@files) { 
    if (-f "$source_dir/$t") { 
    # Check with -f only for files (no directories) 
    copy "$source_dir/$t", "$target_dir/$t"; 
    } 
} 

closedir($DIR); 

home.html을.

jQuery을 사용하려는 것 같습니다. 제공하는 기능을 사용하려면 먼저 페이지에 라이브러리를 포함해야합니다.

<script src="path/to/where/you/put/jquery.js"></script> 
+0

여전히 작동하지 않습니다. – user3003367

+1

그게 유일한 명백한 문제입니다. 디버깅을하십시오. 브라우저의 개발자 도구에서 Net 탭을 확인하십시오. HTTP 요청이 전송되고 있습니까? 콘솔에 오류 메시지가 표시됩니까? copy_file.pl에 대한 URL을 직접 요청하면 어떻게됩니까? 서버 로그는 무엇을 말합니까? – Quentin

2

이것은 CGI/Apache 문제와 유사합니다. Apache 환경에서 Perl 코드가 올바르게 실행 되려면 코드가 출력하는 첫 번째 항목 중 하나 인 Content Type 헤더를 반환해야합니다. 또한

#!/usr/bin/env perl 

use strict; 
use warnings; 

print "Content-Type: text/html\n\n"; 

use File::Copy; 
use CGI::Carp qw(fatalsToBrowser); #nice error handling, assuming there's no major syntax issues that prevent the script from running 

my $source_dir = "/home/Desktop/file"; 
my $target_dir = "/home/Desktop/Perl_script"; 

opendir(my $DIR, $source_dir) || die "can't opendir $source_dir: $!"; 
my @files = readdir($DIR); 

foreach my $t (@files) { 
    if (-f "$source_dir/$t") { 
    # Check with -f only for files (no directories) 
    copy "$source_dir/$t", "$target_dir/$t"; 
    } 
} 

closedir($DIR); 

print "<h1>OK</h1>\n"; 
print "<p>Print</p>\n"; 

__END__ 

...이 같은 더 보이는 코드를 사용해보십시오, 당신이 스크립트는 파일 시스템에서 실행으로 표시 할 필요가 있는지 확인해야하고 아파치가 있어야되는 것은 물론 그것을 실행하려면 privs. 이 모든 것을 점검했으면 URL 행에서 스크립트를 실행하여 JavaScript에서 스크립트를 호출하기 전에 출력을 얻는 지 확인하십시오.

JavaScript 관점에서 보면, Quentin이 올바르게 지적했듯이 JavaScript 코드가 올바르게 작동하게하려면 jQuery에 대한 링크도 포함시켜야합니다. 본문 섹션 위에 다음 헤더 섹션 (및 포함)을 추가하십시오.

<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
</head> 
관련 문제