#!/usr/bin/env perl
#ABSTRACT: Get the content of a URL using LWP::UserAgent or system curl
#PODNAME: curly
use strict;
use warnings;
use Getopt::Long;
sub curl {
    my ($url) = @_;
    
    eval {
        require LWP::UserAgent;
        
        # Create a UserAgent object
        my $ua = LWP::UserAgent->new;
        $ua->ssl_opts(verify_hostname => 0);  # Disable SSL verification (optional)
        
        # Send the initial GET request
        my $response = $ua->get($url);
        
        # Follow redirects if any
        while ($response->is_redirect) {
            my $redirect_url = $response->header('Location');
            $response = $ua->get($redirect_url);
        }
        
        return $response->decoded_content;
    };
    
    if ($@) {
        # Fallback to system curl command
        eval {
            my $output = `curl --silent -L $url`;
            return $output;
        };
        if ($@) {
            die "Can't get content of $url: $@";
        }
    }
}

# Usage example
my $url;
GetOptions(
    'u|url=s' => \$url,
);
my @URLS = ();
push(@URLS, @ARGV);
push(@URLS, $url) if $url;
foreach my $u (@URLS) {
    my $content = curl($u);
    print "## ", $u, "\n";
    print $content, "\n";
}

__END__

=pod

=encoding UTF-8

=head1 NAME

curly - Get the content of a URL using LWP::UserAgent or system curl

=head1 VERSION

version 0.0.1

=head1 AUTHOR

Andrea Telatin <proch@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2023 by Andrea Telatin.

This is free software, licensed under:

  The MIT (X11) License

=cut
