#!/usr/bin/perl
# PERL5 CODE SNIPPET
# Get the full HTML of an url and strip all urls in it
use strict;
use warnings;
use LWP::Simple;
# The array that will hold all found urls
my @urls = ();
# the url to scan
my $url = 'http://www.devjockeys.com';
# get the content
my $content = get $url;
die "Couldn't get $url" unless defined $content;
#convert to lowercase
$content = lc($content);
# replace all " to '
$content =~ s/\"/\'/ig;
# search for "a href" tags and strip the urls.
while (index($content, "<a href") != -1) {
my $i = index($content, "<a href");
my $tmp = substr($content, $i );
$content = substr($content, $i + 1 );
my $t = index($tmp,">");
if (index(substr($tmp,0, $t+1),"http") != -1){
my $tmp2 = substr($tmp,0, $t+1);
my $c1 = index($tmp2,"\'");
$tmp2 = substr($tmp2,$c1+1);
$c1 = index($tmp2,"\'");
$tmp2 = substr($tmp2,0,$c1);
# add the url to the array
push @urls, $tmp2;
}
}
# do what ever you like with the found url's. E.g. print them to your console.
foreach(@urls){
print "$_\n";
}
Tag: Perl
Perl – Labels make your code look even more beautiful
Just some fun code, because I keep on being amazed about how effective, straight forward and especially beautiful Perl can be!
#!/usr/bin/perl -w
use warnings;
use strict;
QUESTION:{
print 'Type any text, enter "q" to quit: ';
chomp (my $input = <STDIN>);
last if ($input eq "q");
my @arry = split //, $input;
print "Reversed: ". join("",reverse @arry)."\n";
redo;
}
print "Bye!\n";