Tag Archives: perl

perl password prompts

I spent a while trying to find a nice solution for a password prompt without writing to much code. This is the best solution I found, the IO::Prompt module! It’s awesome!

use IO::Prompt;
my $pswd = prompt("password for <your name here>: ", -e => '*');

Hopefully this can save someone some time.

shufflepaper – ‘wallpapershuffler’

What started out as a little script turned into a neat little daemon today. I’ve decided to start shuffling my wallpapers, because one my screens has developed “LCD image persistance”. While it’s a pretty mild problem (it only happens if bright whites are left on the screen for many hours, and fades after 10-20 minutes), I figured shuffling my wallpapers wouldn’t be to much of a bad idea.

I used a handy little Perl module I’ve never come across before called Proc::Daemon by Detlef Pilzecker. It really makes turning a Perl script into a daemon very easy. Here is the relevant snippet from shufflepaper. It’s pretty self-explanatory, so I won’t explain it that much.

my $daemon = Proc::Daemon->new(
   child_STDERR => '/tmp/shufflepaper.dbg',
   pid_file => '/tmp/shufflepaper.pid' );
 
if (defined $opt{s}) {
   print "stopping shufflepaper.\n";
   $daemon->Kill_Daemon();
   exit 0;
}
 
if ($daemon->Status() ne 0) {
   print "shufflepaper is already running!\n";
   exit 0;
}
 
$daemon->Init() if $fork;

I made a symlink to shufflepaper in ~/bin, and added “shufflepaper -w /mnt/data/gfx/wallpapers” to .xinitrc, and then made my WM execute “shufflepaper -n” if I press Ctrl+Alt+n (to pick another random wall). Works pretty well.

The source can be found here.

walld

A wallpaper daemon to switch wallpapers every 200 seconds. Nothing special. Have fun.

#!/usr/bin/perl
 
while(1) {
	my @walls = <~/gfx/wallpapers/*>;
	my $s = scalar @walls;
 
	my $f = @walls[rand($s)];
	print "Setting wallpaper -> $f\n";
	system("fbsetbg -a $f");
 
	sleep(200);
}
exit(0);

PhoneDaemon

My phone is capable of connecting to my wireless network. Here is a funky script I wrote in Perl while at uni. It turns my music on when my phone is connected to wireless, and off when it’s not. Static IP insures it never gets confused. In this script 192.168.1.102 is my phone.

#!/usr/bin/perl
 
use strict;
use warnings;
 
my $playing = 0;
 
while(1) {
 
	my $ping = `ping -c 3 -w 4 -q 192.168.1.102`;
 
	if ($ping =~ /[1-9] received/) {
		print "[-] Phone connected, playing music...\n" if $playing eq 0;
		system('mpc play') if $playing eq 0;
		$playing = 1;
	} else {
		print "[-] Phone disconnected, stopping music...\n" if $playing eq 1;
		system('mpc pause') if $playing eq 1;
		$playing = 0;
	}
 
	sleep(1);
}

A long awaited blog post…

It’s been a while since I made a post, but here I am. Back again. What’s new? Not a lot. Finished my A level exams (just got STEP to go), had prom the other day which was fun. But now, down to business.

I wanted to write about an awesome little script setup I’ve been working on over the last few days. Intended solely to make my life easier, I am very happy with how they turned out!

This system uses mpd, dmenu, xmonad.hs and a handy perl script. I have linked them all together in what I feel is an epic music setup.

I started by writing a script called plmpc. It’s written in Perl.

#!/usr/bin/perl
 
use strict;
use warnings;
 
use Switch;
 
## CONFIG
my $fadetime = 1;
## END CONFIG
 
my $dmp = `mpc`;
$dmp =~ /volume:([0-9]*)%/;
my $vol = $1;
 
my $playing;
if ($dmp =~ /playing/) {
	$playing = 1;
} else {
	$playing = 0;
}
 
 
if ($#ARGV+1 > 0) {
	my $arg = $ARGV[0];
 
	switch($arg) {
		case 'stop'	{ stop(); 	}
		case 'pause'	{ pause(); 	}
		case 'play'	{ play(); 	}
 
		case 'splay'	{
			my $term = '';
			foreach my $argnum (1 .. $#ARGV) {
				$term = $term." $ARGV[$argnum]";
			}
 
			$term =~ s/^ //;
 
			print "Searching: $term\n";
 
			my $tmp = `mpc playlist | wc -l`;
			$tmp =~ /([0-9]*)/;
			my $toplay = $1;
			$toplay++;
 
			print `mpc search any "$term" | mpc add`;
			`mpc play $toplay`;
		}
 
		case 'toggle'	{ ($playing eq 1) ? pause() : play(); }
 
		else  {
			my $cmds = '';
			my $c = 0;
			while($c < $#ARGV+1) {
				$cmds = $cmds.' '.$ARGV[$c];				
				$c += 1;
			}
 
			system("mpc$cmds");
		}
	}
} else {
	system("mpc");	
}
 
 
#####################
 
sub play {
	my $tmp = 0;
 
	`mpc volume 0`;
	system('mpc play');
	while($tmp < $vol) {
		$tmp += 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
}
 
sub stop {
	my $tmp = $vol;
 
	while ($tmp > 0) {
		$tmp -= 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
 
	system('mpc stop');
	`mpc volume $vol`;
}
 
 
sub pause {
	my $tmp = $vol;
 
	while ($tmp > 0) {
		$tmp -= 2;
		`mpc volume $tmp`;
		sleep($fadetime/50);
	}
 
	system('mpc pause');
	`mpc volume $vol`;
}

This script does exactly the same as mpc, but if you issue a stop, pause, play or toggle command it will fade the sound. Handy. It also has another trick up it’s sleeve. “splay” will search for items, add them to the playlist and start playing them.

Next I built some commands into xmonad:

, ((modMask .|. controlMask, xK_period), spawn "mpc next")
, ((modMask .|. controlMask, xK_comma ), spawn "mpc prev")
, ((modMask .|. controlMask, xK_m ), spawn "mpc toggle")
, ((modMask .|. controlMask, xK_n ), spawn "mpc stop")
, ((modMask .|. controlMask, xK_z ), spawn "mpc volume -2")
, ((modMask .|. controlMask, xK_x ), spawn "mpc volume +2")
, ((modMask .|. controlMask, xK_c ), spawn "mpd; mpc clear; mpc ls | mpc add; mpc random on; mpc play")
, ((modMask .|. controlMask, xK_v ), spawn "mpc random")
, ((modMask .|. controlMask, xK_b ), spawn "plmpc splay `echo | dmenu -nb '#222222' -nf '#999999' -sb '#5B7496' -fn '-*-montecarlo-medium-*-*-*-*-*-*-*-*-*-*-*'`")

I should note that the plmpc script should be in ~/bin/ and ~/bin/ should be in $PATH.

All of these (I should hope) are self explanatory – but the last line is the coolest of them all. I used dmenu as a simple input device for my “plmpc splay” script.

Sorry for the terrible formatting; WordPress’ fault not mine. ;)