PDA

View Full Version : Finally made something.


recluse
06-12-2002, 03:27 AM
It was both a learning experience and also had some usefulness.


#!/usr/bin/env perl

# recluse
# 20020610
# Gets a list of all my .m3u files and creates a #submenu for
# fluxbox listing them, allowing easy playlist #switching :)

use strict;
use warnings;
use File::Copy;

# Get all of the playlist files
opendir(M3UDIR, "\/home\/recluse\/audio") or die "Can't open M3UDIR\n";
my @m3uList;
foreach((readdir M3UDIR)){
if($_ =~ m/\w+\.m3u/){
push @m3uList, $_;
}
}
close M3UDIR;

# Open the fluxbox menu to read from and a tmp file to write to
my $readMenu = "/home/recluse/.fluxbox/menu";
my $writeMenu = "/home/recluse/.fluxbox/tmpMenu";

open (READMENU, "<$readMenu") or die "Can't open $readMenu for reading\n";
open (WRITEMENU, ">$writeMenu") or die "Can't open $writeMenu for reading\n";

# Remove all the old m3u entries from the menu, no need for repeats
while(<READMENU>){
if($_ !~ m/m3u/){
print WRITEMENU;
}
}
move ($writeMenu, $readMenu) or die "Move failed!$!\n";
close WRITEMENU;
close READMENU;



open (READMENU, "$readMenu") or die "Can't open $readMenu for reading\n";
open (WRITEMENU, ">$writeMenu") or die "Can't open $writeMenu for reading\n";

while(<READMENU>){
if ($_ =~ m/\[submenu\] \(music\) \{\}/){ # Write the playlists
print WRITEMENU;
foreach my $m3u (@m3uList){
print WRITEMENU " " x 12 . "\[exec\] \($m3u\) {xmms -p ~/audio/$m3u}\n";
}
}
else {print WRITEMENU "$_";} # Print the old portions of the menu
}

move ($writeMenu, $readMenu) or die "Move failed $!\n";

close READMENU;
close WRITEMENU;

inkedmn
06-12-2002, 11:59 AM
w00t!

p3r1 h4><>0r!!!1

recluse
06-13-2002, 12:10 AM
Heh, thanks inkedmn. :tu:

iDxMan
06-13-2002, 02:39 AM
Nice one recluse! Its always fun to create something useful..

r

unruly
07-19-2002, 01:29 PM
good, but could use a bit of tweaking
Originally posted by recluse
# Get all of the playlist files
opendir(M3UDIR, "\/home\/recluse\/audio") or die "Can't open M3UDIR\n";



first, you could use an $ARGV for the M3U dir, or, if not specified, have it default to ~ ... something like this:

if (defined($ARGV[1]) {
opendir(M3UDIR, $ARGV[1]) or die "Can't open M3UDIR\n";
} else {
opendir(M3UDIR, "~") or die "Can't open M3UDIR\n";
}


everything else looks great :)

recluse
07-20-2002, 03:36 PM
Cool, I was thinking about doing that sometime. I just needed to get off my ass and read about it / go to codeexamples. But you saved me the trouble. Thanks!

tayl0r
10-27-2002, 04:08 AM
so the variable $ARGV[1] is how you pass arguments into a perl script?

i was wondering how to do this because i wrote an everquest log parsing tool and didn't know how to specify what log file to parse without changing it in the script.

i'm assuming that $ARGV[1] is the first argument, then $ARGV[2] and so on?

do you run the script like:
perl scriptname.pl argument1 argument2
or do you need to use a - or / to specify an argument?

inkedmn
10-27-2002, 05:42 AM
i BELIEVE that you just pass it args (the way you have there) after the name of the program...

if i'm wrong, please correct me

recluse
10-27-2002, 12:41 PM
Yah if one was to:

r00t@urb0xen:./script.pl inkedmn king

$ARGV[0] would hold inkedmn
$ARGV[1] would hold king

Just remember that arrays begin counting at zero, that's messed me up before. ;)

GnuVince
10-27-2002, 12:48 PM
Hmmmr... that's quite useful indeed

unruly
11-04-2002, 01:03 AM
yeah, the cool thing is that it's just an array, so you can do the ultra-effing-spiffy 'foreach()' on it.

phubuh
11-04-2002, 07:44 AM
Since foreach doesn't take a statement, but a block (Why does Perl even make the distinction?), the construct should logically be called 'foreach(){}'.

:p