Loading ...
Sorry, an error occurred while loading the content.

glob, paging merlyn

Expand Messages
  • da5id
    Hey all, I wrote a script that counts how much code I ve written in numbers of lines - just an excercise in recursion and perl - anyway I was wondering the
    Message 1 of 2 , Apr 5, 2007
    • 0 Attachment
      Hey all,

      I wrote a script that counts how much code I've written in numbers of
      lines - just an excercise in recursion and perl - anyway I was wondering
      the following about glob:
      1. Is there a recursive version of glob? So if I went glob("*.pl") it
      would traverse subdirectories as well? (This is where I used my
      recursion originally)
      2. Does glob cough in certain situations? When I did a glob on a folder
      with subdirectories that had spaces in the name, it seemed to return the
      first part of a subfolder name without the space.
      3. Is glob an older way of doing things? I see equivalents of IO::File
      and so on and wonder if I can still use the examples from my old copy of
      Learning Perl. I'd love to get a new one but because I'm always short
      on book money I'd like as much mileage out of my old edition as I can get.

      Thanks much,

      David

      ps. Here's the sub, it's messy and any pointers are appreciated.

      sub getCount{
      my $dirPath = $_[0] . "/";
      my $filename;
      my $filelines;
      while(defined($filename = glob("$dirPath*.pl")))
      {
      next unless($filename =~ /\.pl/); #this shouldn't be necessary,
      should it?

      open CODE, $filename or die "$filename cause $!";
      my @len = <CODE>;
      $filelines = @len;
      $lineCount += @len;
      print "\n$filename -- $filelines\n";
      #write;
      }
      my $d;
      opendir(CODEDIR, $dirPath) || die "sorry bad path: $dirPath.\n$!";
      foreach $d (readdir(CODEDIR))
      {
      unless($d =~ /\.?(\.)/){
      getCount("$dirPath" . "$d");
      }
      }

      }
    • merlyn@stonehenge.com
      ... da5id 1. Is there a recursive version of glob? So if I went glob( *.pl ) it da5id would traverse subdirectories as well? (This is where I used my da5id
      Message 2 of 2 , Apr 5, 2007
      • 0 Attachment
        >>>>> "da5id" == da5id <da5id@...> writes:

        da5id> 1. Is there a recursive version of glob? So if I went glob("*.pl") it
        da5id> would traverse subdirectories as well? (This is where I used my
        da5id> recursion originally)

        File::Find does that. Also see my File::Finder in the CPAN for
        (what I think is) a nicer interface.

        da5id> 2. Does glob cough in certain situations? When I did a glob on a folder
        da5id> with subdirectories that had spaces in the name, it seemed to return the
        da5id> first part of a subfolder name without the space.

        The glob input has to be quoted, but the responses will be ready-to-use.

        So to glob the folder named "Foo Bar" for files that end in ".pm", use glob
        'Foo\ Bar/*.pm'. Note the single quotes, because you want a backslash
        followed by a space in the argument.

        da5id> 3. Is glob an older way of doing things? I see equivalents of IO::File
        da5id> and so on and wonder if I can still use the examples from my old copy of
        da5id> Learning Perl. I'd love to get a new one but because I'm always short
        da5id> on book money I'd like as much mileage out of my old edition as I can get.

        Glob is modern. I use it every day.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      Your message has been successfully submitted and would be delivered to recipients shortly.