in reply to Re: Creating table from file lists
in thread Creating table from file lists

I re-kid you, Almut: eval, untainted, unrestricted??

Could you do a quick replace of <DATA> with <>, with piped input from find for me, and then permit me a run of a single perl -e. Just before you execute your script?

:>>

(ok, it's _DATA_ and thus controlled, but it still hurts on reading. And worse, that one is pure evil to both speakers of Perl and non-speakers alike)

Update: to restrict the hurt to mere aesthtics, consider this safer version of the input loop, which will also survive pasted arbitrary absolute pathes from DATA/or a switch from DATA to STDIN.

my @f=(); # PJ while (<DATA>) { chomp; s|([^/]+)|do{$f[$#f+1]=$1,'$f['.$#f.']'}|ge; # PJ s|^/|\$h->{|; s|/|}{|g; s|$|} = 1;|; # PJ #print; print "\n"; eval; }

food for thought

  1. make 'tokenization' of filenames reuse 'tokens'
  2. fix the loop to allow relative filenames (too trivial)
  3. portability: also allow c:\data\no-such.fil
  4. not a task, but an observation: use of Data::Dumper shows nicely the data structure of the recursive anonymous hash almut uses. This also demonstrates Perl's auto-vivication of the anonymous hashes we use hre: there's not one assignment of {}:
    $h->{$f[19]}{$f[20]}{$f[21]}{$f[22]} = 1;
  5. a remark: normally you'd go for recursive functions when parsing a single path into a list of -> tree nodes. Or for a slightly less readable iterative 'unrolled' version of the same (keywords tail-recursion, loop-unrolling). The above code actually can be seen as such: everything but the s|/|}{|g; has been moved outside the loop, with the loop being reduced to just a looping regex substitution (/g modifier). Not much of a loop, not much for readability, and definitely not a sane nor a correct answer to give :)

Replies are listed 'Best First'.
Re^3: Creating table from file lists
by almut (Canon) on Oct 05, 2009 at 22:17 UTC
    ...definitely not a sane nor a correct answer to give :)

    Heh ;)

    To my excuse I'd like to say that after a couple of hours Java coding this afternoon, I just had to do something real ugly for a change... to wake up my brain cells.  I hope you can forgive my sins...

      Don't worry about me, I didn't test-run your code using live pathological filename specimens :).

      But I'm slightly more worried about the opener.

      Maybe we were enjoying Perl a bit too much: While there certainly is enough stuff for meditation and keywords to lookup to answer to the faithful, we have buried the answers a bit deeper than necessary for mere homework-copy-paste-reply evasion.

      But then I'm telling myself that the affc will ask more specific questions and show his own code, after he _did_ meditate and check the keywords from mickep76's recursion hint onward.

      cu
      Peter

      Update: Just found this one via RAT: id:/501932, which is a nice variation of the topic.