Comments on: Simple Recursion in Perl https://pthree.org/2007/01/18/simple-recursion-in-perl/ Linux. GNU. Freedom. Tue, 31 Oct 2017 18:00:46 +0000 hourly 1 https://wordpress.org/?v=5.0-alpha-42199 By: Chris Blanc https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-83357 Mon, 10 Dec 2007 19:45:16 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-83357 I've unleashed a script inspired by your approach to recursivity here:

http://www.chrisblanc.org/blog/?p=77

It's a simple linefeed filter. By the way, don't OGGs sound better than other file formats at the same bitrate? Amazing compression.

]]>
By: Aaron Toponce: Recursion In Python // The Linux Index https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-64581 Thu, 09 Aug 2007 18:24:55 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-64581 [...] took my Perl script that I wrote some time ago, and rewrote it in Python. The exercise of the initial Perl script, was to get a better handle on [...]

]]>
By: Aaron Toponce » Blog Archive » Recursion In Python https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-64554 Thu, 09 Aug 2007 12:59:09 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-64554 [...] took my Perl script that I wrote some time ago, and rewrote it in Python. The exercise of the initial Perl script, was to get a better handle on [...]

]]>
By: Sam https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-27074 Wed, 24 Jan 2007 02:00:34 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-27074 oh, you know you want to just use:

1
find . -name "*.ogg"

🙂

]]>
By: Aaron https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-26815 Sun, 21 Jan 2007 15:33:14 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-26815 I'll have to give that bit of code a look over when I get to work. File::Find looks like it will do exactly what I need.

I don't know why you were getting files that were everything but *.ogg files. Looking over the code, everything is perfect. Maybe I'm missing something from the copy/paste.

]]>
By: Harley Pig https://pthree.org/2007/01/18/simple-recursion-in-perl/#comment-26812 Sun, 21 Jan 2007 14:14:02 +0000 http://www.pthree.org/2007/01/18/simple-recursion-in-perl/#comment-26812 When I ran this I got a file full of data that did *not* include ogg files.

I spent more time than I should have trying to figure out why this was. I never did figure it out. I'm assuming you were able to make the code work the way you wanted it to. From what I can tell though, it looks ok. A global counter variable in this circumstance isn't really a bad thing.

I know you're doing this to learn recursion, at least a better understand of it in perl, but I can't just let this pass without pointing out File::Find.

File::Find will take care of most of the heavy lifting for you.

Some notes:

Let the shell do the hard work. Why take the trouble to open a file when
you can redirect it? Call it like

1
inventory.pl > oggfiles.txt

This bit of code prints out every single directory name, regardless of
whether or not there is an ogg file in it or its children. You'd need to
store the data and then filter it once all the data was gathered.

The 'preprocess' portion of the code is called after each readdir with the
file list passed in. This allows us to set up the indent variable and
filter out the 'hidden' directories.

The 'postprocess' portion of the code is called after each directory has
been processed (i.e., the list returned by preprocess has been iterated
through). This allows us to trim the indent variable.

The 'wanted' portion of the code is called for every single file found in
the tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl -w

use strict;

use File::Find;

my $indent = '';

find({
  'wanted' => sub { print "$indent$_\n" if /\.ogg$/i; },

  'preprocess' => sub {
    print "$indent$_\n";
    $indent = "$indent  ";
    sort grep { ! /^\./ } @_;
  },

  'postprocess' => sub { $indent = substr( $indent, 0, -2 ); },

}, '.');
]]>