Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 ( #798665=note: print w/replies, xml ) Need Help??

in reply to Should I ask this question?

Ok, let's see. Kudos for using strict and warnings!

# Your code: print "\nWorking on ".$dir."\n\n";

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:

# print a list print "\nWorking on ", $dir, "\n\n"; # Interpolate print "\nWorking on $dir\n\n";

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):

print q{Select an option: 1) Create new XSPFs 2) Delete all XSPFs in dir and create new ones };

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!

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Replies are listed 'Best First'.
Re^2: Should I ask this question?
by thecryoflove (Initiate) on Oct 01, 2009 at 18:16 UTC
    Thanks for the input bv. Just the type of stuff I was looking for!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://798665]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2023-12-06 17:18 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (31 votes). Check out past polls.