Tag Archive for perl

Perl Reverse

perl -e 'print reverse <>' filename

source

0

find . -name "*.html" -print0 | xargs -0 perl -p -i -e 's|Qhttp://66.36.242.244/~olxhxlwq/E|http://www.johnclarkprose.com/|g'

source

Generate XHTML on the command line with XML::API::XHTML

perl -e "use XML::API::XHTML; my $d = new XML::API::XHTML(); $d->head_open(); $d->title('hello world!'); $d->script({type => 'text/javascript'}, '/* hello scripts! */'); $d->head_close(); $d->body_open(); $d->h1({style => 'color:red'}, 'Hi nerd!'); print $d;"  | tidy -q -o temp.html

source

Read file from STDIN and print random line.

print [<>]->[int rand $.]

source

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