views:

81

answers:

2

Hi. I'm trying to get Code Closure to work, but unfortunately, there's always an error thrown.

Here's the code:

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;
}

I get the following error:

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

Here's the api reference I used: http://code.google.com/intl/de-DE/closure/compiler/docs/api-ref.html

Hope anyone knows what's going wrong here. Thanks.

+1  A: 

Pass as js_code the actual code to compile. Try (removing the form-data content_type header):

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

I see you are trying to use POST's file upload feature; what in the API documentation do you see that makes you think that would work? If there is something there, I don't see it.

ysth
+1 Absolutely correct. In addition, `multipart/form-data` is incorrect.
Sinan Ünür
@Sinan Ünür: figured that out too. You don't need a content type at all; presumably it defaults correctly.
ysth
See HTTP::Request::Common for details of the [$filename] syntax. Sinan, absolutely correct in what way? Did either of you try using a different filename that didn't exist? Didn't you get an error message about your file name not being found?
MkV
Nice editing ysth, heh, you didn't see that just now. Previously this reply claimed that js_code => [$filename] would send the filename not the data. It does send the data, but in the format of a file upload, not as the google api requires it.
MkV
@MkV Absolutely correct in that `js_code => scalar( read_file($name) ),` is the right way to specify it. `read_file` will croak if the file in `$name` cannot be found. I put the file in the same directory as my example. BTW, the editing function on SO exists for a reason. The edit history is not secret.
Sinan Ünür
@MkV: yes, I first read the API doc, then looked at your code and misinterpreted it. But I still don't why you wanted to do a file upload. BTW, the HTTP::Request::Common doc could use some examples; it says the file upload feature is triggered by giving 'form-data' as the content type, not the typical 'multipart/form-data'. Do you know if the doc is wrong?
ysth
Thanks! Now it works. In fact, I've used $file->open instead of read_file, because the FileHandle opject is needed, anyway.
Vincent
+2  A: 
#!/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;
}

Output:

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");
Sinan Ünür