3

I am new to perl and any help will be appreciated!!

I have to invoke some URLs through perl (On unix machine).URLs are both http and https

if URL gets invoked successfully,then its fine else create a log file stating that unable to invoke a URL.

For invoking the URL,I am thinking to use for e.g.

  exec 'firefox http://www.yahoo.com';

But how to get http and https request status code? Something like if status is 200,then ok else error..

Kindly help!!

3
  • 6
    This will not work. You should be using the Perl HTTP::Client module (or LWP) instead. That will allow you to check the return codes but will not display any UI to the user. Do you really need the entire browser to be launched? Please explain more clearly what you're trying to accomplish. Commented Jun 2, 2014 at 17:43
  • 1
    It sounds like you're trying to recreate checkbot Commented Jun 2, 2014 at 18:08
  • Thanks Jim. I don't want any browser to be launched..Just want to check if a list of URLS gets invoked or not..it can happen in the background..I just need the return code..i.e. if return code=200, then success else failure. Commented Jun 3, 2014 at 1:38

1 Answer 1

6

Rather than using a browser such a Firefox you should use an HTTP client library such as HTTP::Tiny or LWP::UserAgent.

For exmaple:

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

use HTTP::Tiny;

my $Client = HTTP::Tiny->new();

my @urls = (
    'http://www.yahoo.com',
    'https://www.google.com',
    'http://nosuchsiteexists.com',
);

for my $url (@urls) {
    my $response = $Client->get($url);
    say $url, ": ", $response->{status};
}

Which outputs:

alex@yuzu:~$ ./return_status.pl 
http://www.yahoo.com: 200
https://www.google.com: 200
http://nosuchsiteexists.com: 599

If you want to correctly recognise redirect status codes (3XX) you would have to set the max_redirect parameter to 0.

alex@yuzu:~$ perl -MHTTP::Tiny -E 'say HTTP::Tiny->new(max_redirect => 0)->get("http://www.nestoria.co.uk/soho")->{status};'
301

If all you care about is success then the response hashref contains a 'success' field which will be true on success and false on failure.

alex@yuzu:~$ perl -MHTTP::Tiny -E 'say HTTP::Tiny->new()->get("http://www.google.com")->{success};'
1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kaoru..I am completely new to perl.I have to download the tarball of HTTP::Tiny and extract it?Can you give me the location from where I can download it?
Hi @user2150950 - I chose HTTP::Tiny because it's been a "core" Perl module since Perl 5.13.9 so if you are on a recent version of your operating system I would expect you to have it available already. If your Linux distribution of choice doesn't ship with HTTP::Tiny I'm sure they will have it in their repositories. For example: "# yum install perl-HTML-Tiny" or "# apt-get install libhttp-tiny-perl"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.