Solved

How do I make this simple function work?

Posted on 2011-09-08
6
204 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 129
use google analytics code in perl script 2 80
Regular Expression question to filter with negation. 6 100
Perl Frameworks 1 96
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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…

791 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