in reply to TIMTOWTDI Challenge: Create a filename

Once we have the filename -- how do we create it? I think this way is a pretty awesome yet stupid way of doing just that. It's not even platform independent!

use File::Temp qw(tempfile); my (undef,$file) = tempfile( DIR => '.' ); `echo "#!/bin/touch $file" > tmp.sh`; chmod 0755, 'tmp.sh'; system './tmp.sh';

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: TIMTOWTDI Challenge: Create a filename
by fullermd (Vicar) on Jan 09, 2009 at 02:32 UTC

    Hey!

    % which touch /usr/bin/touch

    Portability counts! You should make it more robust. Maybe with env...

    `echo "#!/usr/bin/env touch $file" > tmp.sh`;

    Of course, that's slower at runtime. Extra forks and all. Maybe precalculate it...

    `echo "#!\$(which touch) $file" > tmp.sh`;

    But really, sh is pretty weak. And we've already got perl anyway, right? So why not use it to its fullest incapacity?

    `echo "#!/usr/bin/env perl -e 'system \"\$(which touch) $file\"'" > tm +p.pl`; system './tmp.pl';

    Of course, that's getting pretty silly. And tedious. Obviously a little metaprogramming is called for to generate those expressions...