2010-08-14 36 views
0

我試圖讓代碼閉包工作,但不幸的是,總是有一個錯誤拋出。如何使用Perl的LWP :: UserAgent訪問Closure JavaScript minifier?

下面的代碼:

use LWP::UserAgent; 
use HTTP::Request::Common; 
use HTTP::Response; 

my $name = 'test.js'; 
my $agent = new LWP::UserAgent(); 
$agent->agent("curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18"); 

$res = $agent->request(POST 'http://closure-compiler.appspot.com/compile', 
      content_type => 'multipart/form-data', 
      content  => [ 
        output_info => 'compiled_code', 
          compilation_level => 'SIMPLE_OPTIMIZATIONS', 
        output_format => 'text', 
        js_code => [File::Spec->rel2abs($name)] 
         ]); 

if ($res->is_success) { 
    $minified = $res->decoded_content; 
    print $minified;die; 
} 

我收到以下錯誤:

Error(13): No output information to produce, yet compilation was requested.

下面是我用的API參考: http://code.google.com/intl/de-DE/closure/compiler/docs/api-ref.html

希望任何人知道發生了什麼事情錯在這裏。謝謝。

+2

你爲什麼要僞造'用戶agent'? – 2010-08-14 01:05:08

+1

如果您不反對使用更多模塊,請搜索http://search.cpan.org上的'Closure',此任務已經至少兩次製作爲模塊。 – MkV 2010-08-14 01:13:44

回答

1

作爲js_code傳遞要編譯的實際代碼。嘗試(刪除表單數據content_type頭):

use File::Slurp "read_file"; 
... 
    js_code => scalar(read_file($name)), 

我看到你正在嘗試使用POST的文件上傳功能;你看到的API文檔中的哪些內容會讓你認爲這會起作用?如果有什麼東西,我不會看到它。

+0

+1絕對正確。另外''multipart/form-data'不正確。 – 2010-08-14 01:01:42

+0

@SinanÜnür:也明白了。你根本不需要內容類型;據推測它默認正確。 – ysth 2010-08-14 01:07:38

+0

有關[$ filename]語法的詳細信息,請參閱HTTP :: Request :: Common。思南,以什麼方式絕對正確?你們有沒有嘗試使用不存在的不同文件名?您沒有收到有關您的文件名稱未找到的錯誤消息嗎? – MkV 2010-08-14 01:09:50

2
#!/usr/bin/perl 

use strict; use warnings; 

use File::Slurp; 
use LWP::UserAgent; 

my $agent = LWP::UserAgent->new; 
my $script = 'test.js'; 

my $response = $agent->post(
    'http://closure-compiler.appspot.com/compile', 
    content_type => 'application/x-www-form-urlencoded', 
    content => [ 
     compilation_level => 'SIMPLE_OPTIMIZATIONS', 
     output_info => 'compiled_code', 
     output_format => 'text', 
     js_code => scalar read_file($script), 
    ], 
); 

if ($response->is_success) { 
    my $minified = $response->decoded_content; 
    print $minified; 
} 

輸出:

C:\Temp> cat test.js 
// ADD YOUR CODE HERE 
function hello(name) { 
    alert('Hello, ' + name); 
} 
hello('New user'); 



C:\Temp> t 
function hello(a){alert("Hello, "+a)}hello("New user");
相關問題