abstracts' comments are spot on -- I'd like to reiterate 2 things, however.
Firstly :
for $i (@tokens){$nn.=$tag->{$i}." $sep ";} my $z=rindex($nn,$sep); my $len=length $sep; substr($nn,$z,$len+1,''); chop $nn; $nn.='.mp3';
This is valid, but klunky perl.
As I just went through the process of working with my own MP3 collection, I recommend using the tag object in hash form (I eventually went to tagged because I was rewriting id3v2 tags, but that's another story entirely). This is probably frowned upon by OOP purists, but we're all friends here. Here's a short example comparing the two methods.
use MP3::Info; use strict; # # what's boo_radley listening to? # in CODE tag friendly format :) # my $fn = 'F:\Warlock Pinchers\Deadly Kung Fu Action '. '_ Pinch a Loaf\Warlock Pinchers - ' . 'Deadly Kung Fu Action _ Pinch a Loaf '. '- 06 - Black in Black.mp3'; my $tag=get_mp3tag($fn) || die "unable to get tag."; # tags to incorporate into new filename my @tokens = qw (ARTIST TITLE ALBUM GENRE); my $sep ="-"; my $nn; # from buu's script. # for my $i (@tokens){$nn.=$$tag{$i}." $sep "} my $z=rindex($nn,$sep); my $len=length $sep; substr($nn,$z,$len+1,''); chop $nn; $nn.='.mp3'; output the labors of our work print "\n$nn\n"; #using the $tag object as a hash (in hash slice form!) print join (" - ", @$tag{@tokens}),".mp3";
If this isn't clear, any time an object uses a hash to store data, you can (usually, unless the module author was clever) access that hash by typecasting the instantiation of that object into a hash.
Well, what does that mean? it means that if we have a $tag object, we can treat it like a simple hash like so : %$tag.
OK. We've just reduced 6 lines of code to 1; that's a pretty good deal. There's less to troubleshoot, comment and the meaning of the code becomes clearer.

Secondly : print "Renamed $_ to $nn \n" if rename $_,$nn; This is terrible! When renaming files, you should always check to see if the file exists beforehand!
Always! Well, usually, and certainly in this case. I don't want to have to re-rip a cd because, this script renamed 32 mp3s to "WARLOCK PINCHERS.mp3" after only inputting the artist on the command line!
I like to use unless in these cases, when I'm expecting (but still not sure!) that the file will not already exist.
unless (-e $fn) { print "Renamed $_ to $nn \n" if rename $_,$nn } else { # for example ... push(@errors,"Tried to rename $_ to $nn, file exists\n") }


Those are my concerns. Overall, I don't think this is entirely a bad script; it's probably one that you hacked together after saying "my mp3 collection, it's a damn mess!", and whipped together something immediately useful to you. Whenever you share things on perlmonks, it becomes a more public thing, though, and just as you pretty up to go out on the town, you should pretty up your code when you show it off on perlmonks. Comment things excessively. Your script requires command line parameters? Think about using Getopt::Long, or at least checking for a sane number of params. Did you examine a particular algorithm, found it lacking and developed a new strategy for the same task? Say so in your comments. When you're done with the script, go back and say,"What can I improve? What does not inherently make sense to me that I can change?".The quality and nature of your coding will improve when you start to actively think about it.

In reply to Re: mp3 collection renamer by boo_radley
in thread mp3 collection renamer by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.