Skip to content

Commit a38cb73

Browse files
committed
First commit of Perl5 LWP target
1 parent 1855a74 commit a38cb73

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"java",
2020
"javascript",
2121
"jquery",
22+
"lwp",
2223
"objc",
2324
"objective-c",
2425
"ocaml",
26+
"perl5",
2527
"php",
2628
"python",
2729
"request",

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
node: require('./node'),
1111
objc: require('./objc'),
1212
ocaml: require('./ocaml'),
13+
perl5: require('./perl5'),
1314
php: require('./php'),
1415
python: require('./python'),
1516
ruby: require('./ruby'),

src/targets/perl5/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'perl5',
6+
title: 'Perl 5',
7+
extname: '.pl',
8+
default: 'perl5'
9+
},
10+
11+
lwp: require('./lwp')
12+
}

src/targets/perl5/lwp.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Perl using LWP
4+
*
5+
* @author
6+
* @ashleyhindmarsh
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
15+
16+
module.exports = function (source, options) {
17+
// Start snippet
18+
var code = new CodeBuilder(' ')
19+
20+
// Dependencies
21+
code.push('use strict; use warnings;')
22+
.push('use LWP::UserAgent;')
23+
.push('use HTTP::Request;')
24+
.push('use URI;')
25+
.blank()
26+
27+
code.push('my $ua = LWP::UserAgent->new;');
28+
29+
// Set URL
30+
code.push('my $url = URI->new("%s");', source.url)
31+
.blank()
32+
33+
// Construct query string
34+
if (source.queryString.length) {
35+
code.push( '$url->query(%s);', JSON.stringify(source.queryObj) )
36+
.blank
37+
}
38+
39+
// Construct payload
40+
var payload = JSON.stringify(source.postData.text)
41+
42+
if (payload) {
43+
code.push('my $payload = %s;', payload)
44+
}
45+
46+
// Construct request
47+
var method = source.method
48+
var request = util.format('my $request = HTTP::Request->new( %s => $url );', method);
49+
code.push(request)
50+
.blank()
51+
52+
if (payload) {
53+
code.push( '$request->content( $payload );' )
54+
}
55+
56+
// Construct headers
57+
var header
58+
var headers = source.allHeaders
59+
for (header in headers) {
60+
code.push( '$request->header( \'%s\' => \'%s\' );', header, headers[header] )
61+
.blank;
62+
}
63+
64+
// execute request
65+
code.push('my $response = $ua->request($request);')
66+
67+
// print response
68+
code.push('print($response->decoded_content);')
69+
70+
return code.join()
71+
}
72+
73+
module.exports.info = {
74+
key: 'lwp',
75+
title: 'LWP',
76+
link: 'https://metacpan.org/pod/LWP',
77+
description: 'LWP library'
78+
}
79+

0 commit comments

Comments
 (0)