Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: subroutine array empty

by mwah (Hermit)
on Dec 08, 2007 at 22:50 UTC ( [id://655895]=note: print w/replies, xml ) Need Help??


in reply to subroutine array empty

The error in your code has been already pointed out by merlyn and somebody else (how fast are these people ;-), but I'd like to add a more "idiomatic" version of your code. You should return mp3 files only (if you are after them ;-) ...

#!/usr/bin/perl use strict; use warnings; use Carp; my $dir = '/home/musc'; my @mp3 = getSongs( $dir ); for my $song (@mp3) { print "$song\n"; } sub getSongs { my $dir = shift; my @files; if( -d $dir ) { opendir(my $dirhandle, $dir) or croak "$dir $!"; @files = grep /\.mp3$/, readdir $dirhandle; closedir $dirhandle } else { carp "don't know about $dir\n" } return @files; }

Regards

mwa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found