Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Learning Perl

Name: Anonymous 2005-07-30 4:54

I'm learning perl right now and I'm having a bit of a problem. I'm making a function that returns a list of the files (no directories) in a dir. It also has an option do do it recursively so the files in the subdirs also get in the list. The problem is that it won't work as it should when it's being recursive. It will only return the files of it's "first stop".

sub getdir {
    my ($dirpath, $recurse) = @_;
    my ($file, @dirlist);

    if ($dirpath !~ /[\/\\]$/) {
        $dirpath .= "/";
    }
    if (!opendir(DIR, $dirpath)) {
        return 0;
    }
    while (defined($file = readdir(DIR))) {
        next if $file =~ /^\.\.?$/;
        $file = "$dirpath$file";
        if ($recurse && -d $file) {
            push (@dirlist, &getdir("$file/", 1));
        }
        elsif (! -d $file) {
            push(@dirlist, "$file");
        }
    }
    closedir(DIR);
    return @dirlist;
}

Name: Anonymous 2005-07-30 14:24

you can use globs to traverse dirs
also
use File::Basename; #see perldoc File::Basename


sub what {
for my $file (<$path/*>) {
   if ($file -d $file) {

   } else {

   }
}

Name: Anonymous 2005-07-30 15:44

>>1
DIR is a global variable
just make it `my $dir` and it will work

Name: Anonymous 2005-07-30 16:14

>>2
What is a glob? Sounds almost like blog backwards, which scares me.

>>3
I suspected something like that; the warning messages usually point that out. I read on some page that variables and handles work differently so I didn't think it would be possible.

Name: Anonymous 2005-07-30 19:27

>>4
OH. DEAR.

Name: Anonymous 2005-07-31 13:46

>>4

To glob is to expand a criteria (i.e. "subdir/*.jpg") into all matching files starting from the current directory.

Name: Anonymous 2005-08-04 0:58

Find a local perl mongers

pm.org

Name: Anonymous 2005-08-18 12:49

Fuck perl.

Learn a useful language like Python.

Name: Anonymous 2005-08-18 13:51

Fuck Python.

Learn a powerful language like Ruby.

Name: Anonymous 2005-08-18 13:59

Fuck Ruby.

Learn a powerful language like Perl.

Name: Anonymous 2005-08-18 14:05

Fuck Perl.

Learn a real language like C++.

Name: Anonymous 2005-08-18 15:31

Fuck C++.

Learn a real programmer's language like FORTRAN.

Name: Anonymous 2005-08-18 15:56

Fuck you guys.

That's right.

Name: Anonymous 2005-08-18 16:01

>>8
>>9
>>10
>>11
>>12
Damn, I didn't believe it when I read in /opdis that /prog is the new /vip; but I guess they're right.

Name: Anonymous 2005-08-19 6:40

>>14
NO U

Name: Anonymous 2005-08-19 12:29

>>8
>>9
>>10
>>11
>>12
MUPPET BABIES MOTHER FUCKERS

Name: Anonymous 2005-08-20 8:23

Fuck C++, C#, Java, Ruby, Python, Perl, PHP, C, Fortran, and Bash scripting.

Real programmers are so goth they shit machine code.

Name: Anonymous 2005-08-20 9:06

Real programmer = compiler?

Name: Anonymous 2005-08-20 9:35

>>18
Real programmers directly output signals to a processor.

Name: Anonymous 2005-08-20 18:04

Real programmers are sentient processors.

Name: Anonymous 2005-08-20 19:16

There was an autistic guy in my class that we used to call "the human compiler."

It wasn't as cool as it sounds though; it was a reference to his uncanny ability to complain loudly every time the lecturer made a trivial error such as forgetting a semicolon.

Name: Anonymous 2008-04-19 18:47

Fuck FORTRAN.

Learn a useful language like The Algorithmic Language Scheme.

Name: Anonymous 2013-01-01 0:16

this thread survived the end of the world

Name: Anonymous 2013-01-05 7:51

>>24
Too fucking bad

Don't change these.
Name: Email:
Entire Thread Thread List