Solved

How do I make this simple function work?

Posted on 2011-09-08
6
199 Views
Last Modified: 2012-05-12
The goal is to pass the name of a directory to a function which opens the directory, creates a list of files and returns that list back where I print the list.  The function works just fine if I change $dir to directly use $MyDir so for some reason the directory name isn't being passed to the function and it can't open the directory.

Not sure what I'm doing wrong but I'm sure it's simple. Help greatly appreciated!

$MyDir = "c:\\";
(@files2) = OPENDIR ($MyDir);
print join("\n",@files2);

sub OPENDIR ($dir){
opendir (THEDIR, $dir) or die "can't open directory $dir";
while (defined(my $filelist = readdir(THEDIR))) {
      push (@files,$filelist);
}
closedir (THEDIR);
return (@files);
}
0
Comment
Question by:mikebernhardt
6 Comments
 
LVL 8

Accepted Solution

by:
pwust earned 500 total points
ID: 36505903
The way parameters is passed to Perl functions is different:

$MyDir = "c:\\";
(@files2) = OPENDIR ($MyDir);
print join("\n",@files2);

sub OPENDIR {
$dir = $_[0];
opendir (THEDIR, $dir) or die "can't open directory $dir";
while (defined(my $filelist = readdir(THEDIR))) {
      push (@files,$filelist);
}
closedir (THEDIR);
return (@files);
}

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 36506093
easier to just do

my @files2 = <C:/*.*>;

Open in new window


0
 
LVL 28

Expert Comment

by:FishMonger
ID: 36506241
Or you can use the glob function.

my @files2 = glob('c:/*');

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:FishMonger
ID: 36506656
If you wanted/needed to use a subroutine for this, then here's a minimal example.
my $dir = 'C:/';
my @files2 = get_files($dir);

print "$_\n" for @files2;

sub get_files {
    return glob("$_[0]*");
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 36506993
the difference between
sub get_files {
    return glob("$_[0]/*");
}
and
sub get_files {
    my $dir = shift;
    opendir (THEDIR, $dir) or die "can't open directory $dir";
    my @files= readdir(THEDIR);
    closedir (THEDIR);
    return @files;
}

is that the glob will include the $_[0] path in the returned list, while the readdir will not
0
 
LVL 28

Author Closing Comment

by:mikebernhardt
ID: 36507028
Thanks guys. The example script I posted was part of something much bigger. Although you all had good ideas, passing the reference properly was what I needed to know to move on with my project.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now