Tag Archive for perl

Count directory entries in given directories

#!/usr/bin/perl
$ARGV[0] = '.' unless @ARGV;
for my $dir (@ARGV) {
opendir DIR, $dir or die "$dir: $!
";
$file =~ m:^.: or ++$count
while ($file = readdir DIR);
closedir DIR;
}
print "$count
";
exit 0;

source

Crazy Perl Code

$_ = shift;
s////g;
print;

################

while(<>) {
/^(w+)/;
print $_.',';
}

#############

if(($_ = 5)==5) {print;}

##############

$x = 7;
if (($x <=> 0)==($x=$x)) {print 'true';}
else{print 'false';}

#######################

print "Hello World" or die "trying";

######################

print eval qw(qw(qw()));

source

battery

#!/usr/bin/perl

# battery.pl
#
# Perl script that displays to the terminal the charge on your Mac Intel battery (and takes
# a guess as to how much time is left on the charge).
#
# Version 1.0 (010308) - Hey, it works!
#         1.1 (010408) - Added debugging code; added IsCharging test; changed symbols for TimeLeft.
#         1.2 (011208) - "Fixed" some weirdness with the battery being at less than 100% capacity,
#                        but still reading "FullyCharged" or reading "FullyCharged" while also
#                        reading "IsCharging." Wacky.
#
# Author: P. Ham - <a href="mailto:pham@sdf.lonestar.org">pham@sdf.lonestar.org</a>
#
# Cryptic Notes: 65535 may just be a kludge number to mean "infinity." See <a href="http://en.wikipedia.org/wiki/65535_" >http://en.wikipedia.org/wiki/65535_</a>(number).

use strict;
use Data::Dumper;
my $IOREG = "/usr/sbin/ioreg";

my $output = `$IOREG -nAppleSmartBattery`;
my @output = split("
", $output);
my $batteryHash;

foreach (@output) {
if (/"(MaxCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(DesignCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(CurrentCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(AvgTimeToEmpty)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(AvgTimeToFill)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(TimeRemaining)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(IsCharging)"s+=s+(w+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(FullyCharged)"s+=s+(w+)/) {
$batteryHash->{$1} = $2;
}
}

my $DEBUG = 0;
print Dumper $batteryHash if $DEBUG;

my $percentCapacity = ( $batteryHash->{"CurrentCapacity"} / $batteryHash->{"MaxCapacity"} ) * 100;
$percentCapacity = int($percentCapacity + .5 * ($percentCapacity <=> 0));

my $timeLeft;
if ($batteryHash->{"IsCharging"} eq "Yes" && $batteryHash->{"FullyCharged"} eq "No") {
# battery is charging so can't calculate time left (yet)
$timeLeft = "( + )";
# $timeLeft = "(~/~)";
} elsif ($batteryHash->{"FullyCharged"} eq "Yes") {
$timeLeft = "( f )";
} elsif ($percentCapacity != 100) {
# OK, calculate time left
$timeLeft = $batteryHash->{"TimeRemaining"} / 60;
my ($hours, $minutes) = split(/./, $timeLeft);
$minutes = 60 * ( "." . $minutes );
$minutes = int($minutes + .005 * ($minutes <=> 0));
$minutes = sprintf ("%02d", $minutes);
$timeLeft = "($hours:$minutes)";
} else {
# not charging and 100% capacity
# $timeLeft = "( - )";
$timeLeft = "( f )";
}

print "battery: $percentCapacity% $timeLeft
";

exit;

source

Batch rename files in directory

for F in * ; do NF=`echo $F | perl -lne "s/ /_/g; s/._/./g; s/[',-]//g; print lc"` ; mv "$F" "$NF" ; done

source

Perl Hello World

#!/usr/bin/perl

echo "Hello World
";

source

Scrape Google from the command line

perl -e "$i=0;while($i<1000){sleep 1; open(WGET,qq/|xargs lynx -dump/);printf WGET qq{http://www.google.com/search?q=site:onemorebug.com&hl=en&start=$i&sa=N},$i+=10}" | grep "//[^/]*onemorebug.com/"

source

One liner perl extract content with regular expression

curl -s <a href="http://checkip.dyndns.org" >http://checkip.dyndns.org</a> | perl -nle 'print "$1" if (/Current IP Address: ([d.]*)/)'

source

Backup a flickr photoset

#!/usr/bin/perl -w
# To install the Flickr perl library on OSX with macports I do:
# sudo port install p5-flickr-api
# On Linux there's a similar package.
#
# Then to get Flickr::Photoset I do:
# sudo perl -MCPAN -e 'install Flickr::Photoset'

use Data::Dumper;
use Flickr::Photoset;
use Flickr::Photo;
use LWP::Simple;
use strict;

my $params = { api_key => 'your api key'};
my $info = {};

my $photoset = Flickr::Photoset->new($params);

# specify a photoset
if ($photoset->id({id => '72057594072478931'})) {
my $title = $photoset->title;
my $owner = $photoset->owner->real_name;
my $photos = $photoset->photos;
foreach my $p ( @$photos ) {
my $id = $p->id;
my $sizes = $p->sizes;
foreach my $s (@$sizes) {
if ( $s->{'label'} eq 'Original') {
$info->{$id} = {
source => $s->{'source'},
title  => $p->title,
server => $p->server
};
my $ret = getstore(
$s->{'source'},
$p->title.'_'.$id.'.jpg'
);
print 'response was '.$ret.' for '.$p->title."/n";
}
}
}
}

source

check linked pages for Tidy validation errors, on the command line

lwp-request -o links <a href="file:///SAVED_GOOGLE_RESULTS.htm" >file:///SAVED_GOOGLE_RESULTS.htm</a>|grep -P "As*http://w*.MY_DOMAIN" | perl -pe "m#As*(.*)#; $notify = qq{
$1:
}; $_=qx{lwp-request $1|tidy -eq 2>&1|grep -e Error -e DOCTYPE}; $_ = $notify .$_ if $_" > report.txt

#Alternate: print out just the HTTP response code for linked pages that have my domain in the link

lwp-request -o links <a href="http://onemorebug.com" >http://onemorebug.com</a>|perl -pe "chomp; $_ =~ s#w*s*##; undef $_ unless m/onemorebug.com/; $_ .= qq{	} . qx{lwp-request -ds $_} if $_"

#old version

lwp-request -o links <a href="file:///C:/SAVED_GOOGLE_RESULTS.htm" >file:///C:/SAVED_GOOGLE_RESULTS.htm</a>|grep -P "As*http://w*.MY_DOMAIN" | perl -pe "m#As*(.*)#; $notify = qq{	$1: }; $_=qx{lwp-request $1|tidy -e 2>&1 | grep "DOCTYPE"}; print $notify if $_"

source

Validate a list of Web pages with Tidy, on the command line

lwp-request -o links <a href="file:///C:/some_page_from_google.htm" >file:///C:/some_page_from_google.htm</a> | grep -P "As*http://w*.mydomain" | perl -pe "m#As*(.*)#; $_=qx{lwp-request $1|tidy -e}; $_ = qq{

- Tidy report for:
$1

$_};"

source