Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

lwp

get image

#!/usr/bin/perl
use LWP::UserAgent;
use CGI qw(header -no_debug);
# this is an awesome picture, and you wanted to get it via script....
$URL = 'http://somesite.com/wp-content/uploads/jamesgrokit1.jpg';
my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
# binmode helps when printing data, perl defaults data type as text
binmode(STDOUT);
# if it's a picture, print the header type and then data.
# if it's not a valid picture, print text/html header type and then the status
# note: this won't work from the command line is meant to be a CGI script!
# words with image/gif only.   Use different headers for different types of image
# or alter code to account for different types.
print $res->is_success ? (header('image/gif'), $res->content)
                        : (header('text/html'), $res->status_line);

grab page

#!/usr/bin/perl
# pretty standard to have this at least in all scripts
# at least for development.  Use taint also for CGI.
use warnings;
use strict;
use LWP::UserAgent;
use CGI qw(header -no_debug);
# if we don't do this it will buffer and then print results
# I want to see results immediately as it happens.
# not every server configuration supports this
$|++;
# put a page that requires authentication here to see a 401 error
# put a normal URL here to see it pull text normally.
my $URL = 'http://somesite.com/';
# create a new object and request the GET method on $URL
my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);
# required for any type of CGI
print header;
# at this point you can either use the print statement,
# or assign the data and manipulate it
# you can can also just print the text:
# print $res->is_success ? $res->cotnent : $res->status_line;
# but lets assume we want to manipulate further before printing:
my $data = $res->is_success ? $res->content : $res->status_line;
# simple regex to grab a pattern match from the page:
if ($data =~ m/The quick brown(.*)jumped over the lazy dog/) {
  print "we found a ' $1 ' (should be ' fox ')";
} else {
  print "probably not a typing tutorial!\n";
}
# What did the full data contain?
print $data;
# not required, but a good habit.
exit;