Solved

How do I make this simple function work?

Posted on 2011-09-08
6
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…

733 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