Here is a 'straight script' version. Personally, for scripts this small, i really prefer this style (Perl Cookbook recipe 9.7.)
use strict; use File::Find; my $cod = shift or die 'some error message'; my $dbpath = '/path/to/somewhere'; my @files; @ARGV = '.' unless @ARGV; find sub { push(@files, $_) if /\.mp3\z/; }, @ARGV; open(DB, ">$dbpath/$cod.txt") || die $!; print DB map { s/\.mp3\z/\n/; $_ } sort @files;
The need for our has been eliminated because there are no subroutines to share variables across scope (which, IMHO, is a bad idea and an abuse of our).

Looks like the original author was trying to encapsulate some functionality. I question this approach, because the subroutine indexfiles handles too many tasks and while it does accept arguments, indexfiles does not return anything and instead relies on a shared variable. (peruse the search results at google.com for tight coupling loose cohesion for some good reading.)

Like i said before, i personally prefer a script like this to be consise and without unnecessary subroutines. But, since we are on the topic, here is another version that uses two subroutines (not including the File::Find magic):

use strict; use File::Find; my $cod = shift or die 'some error message'; my $dbpath = '/path/to/somewhere'; @ARGV = '.' unless @ARGV; my @files = get_files(@ARGV); open(DB, ">$dbpath/$cod.txt") || die $!; print DB trim(@files); sub get_files { my @file; find sub { push(@file, $_) if /\.mp3\z/; }, @_; return @file; } sub trim { return map { s/\.mp3\z/\n/; $_ } sort @_; }
Add some error checking and you have some nice subroutines. The important thing to remember here is that a subroutine should be as general as possible, only handle as small of task as possible, and act like a mathematical function: input and output.

Good luck :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: Using File::Find with a bit of elegance by jeffa
in thread Using File::Find with a bit of elegance by zjunior

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.