开发者

How do I write a Perl script to use curl to process a URL?

开发者 https://www.devze.com 2023-03-31 08:02 出处:网络
I have a very simple task. I have a crontab that will run a script every hour. The script is meant to simply process a URL.

I have a very simple task. I have a crontab that will run a script every hour. The script is meant to simply process a URL.

This is what I have. It doesn't work. I get a syntax error.

#!/usr/bin/perl
curl http://domain.com/page.html;

I haven't worked in Perl in years and wasn't very adept when I did.

SOLUT开发者_Python百科ION

Thanks, Alex for pointing me to the correct path!

crontab

*/30 * * * * curl http://domain.com/path.html


You can either use curl via backticks

my $curl=`curl http://whatever`

or you can use WWW::Curl.


To call any shell command from Perl, you can use system:

system "curl http://domain.com/page.html";

Just enclose the shell command in quotes.


If shying away from executing command line tools from with Perl, the library equivalent is

use WWW::Curl::Simple;
my $curl = WWW::Curl::Simple->new();
my $res = $curl->get("https://stackoverflow.com");

The content you then access as in

print $res->content;

You may want to read up on https://metacpan.org/pod/WWW::Curl::Simple and the get method returns a https://metacpan.org/pod/HTTP::Response.


Try this if you need to pass multiple parameters.

my $cc = 'curl -v -X GET https://base-template.squarespace.com/blog/\?field1=value1\&format=json-pretty\&field3=value3';
print `$cc`;
0

精彩评论

暂无评论...
验证码 换一张
取 消