Clear questions and runnable code
get the best and fastest answer
Re: Should I ask this question?by bv (Friar)
|on Oct 01, 2009 at 14:55 UTC||Need Help??|
Ok, let's see. Kudos for using strict and warnings!
When printing, concatenating strings with . is probably not what you want. It's more expensive than simply printing a list or interpolating values into a string. The following two statements are equivalent, but better:
When you are printing blocks of text with lots of newlines, use the q and qq operators, so that this:print "Select an option: \n\n 1\) Create new XSPFs\n2\) Delete all XSPFs in dir and create new ones\n";
becomes this (notice that parentheses do not need to be escaped in strings):
Here's somewhere you can really improve your code:open(XSPF, ">$playlist");
There are a few things you should know about using open. First, whenever possible, use the 3-argument version. This protects you against subtle security bugs if your code ever gets used suid, and probably some other bugs. Second, filehandles are variables, too! You should use my to make them lexical. Last, and to some people most importantly, you should always check the success of system calls like open. This is the "or die" part of "use strict and warnings or die." Put it all together with the $! variable to tel you why you failed and you get something like this:open my $XSPF, '>', $playlist or die "Can't open $playlist: $!";
Along with the q and qq operators, you should understand the difference between single and double quotes and how to use them. This line:print XSPF "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
could be written with single quotes as such:print XSPF '<?xml version="1.0" encoding="UTF-8"?>',"\n";
but you could really just use the q operator for that and the next 2 lines put together.
Finally, a few nitpicky things that aren't so important. I prefer to use parentheses only when necessary, or when it's not clear what arguments go with what functions. So I never use parens with close, rarely with open, and also rarely with compound conditionals. I also prefer to use and, or and not to &&, ||, and !, but there are differences in precedence, so be careful. Lastly, I prefer to call subroutines with func() rather than &func, unless I specifically need to pass the current @_ to the sub. See perlsub for more on that.
I hope that helped, and good luck!