Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Broken Function

by choroba (Abbot)
on Apr 04, 2014 at 13:21 UTC ( #1081127=note: print w/ replies, xml ) Need Help??


in reply to Broken Function

No need to call test to see if a file exists. We have -e in Perl, too. We even have File::Copy, so you don't need to cat a file. Path::Tiny wraps all the path related operations nicely:

#!/usr/bin/perl # Untested! use Path::Tiny; my @files = ( ... ); sub find_files { my $filename = shift; my $path = 'Path::Tiny'->cwd; my $file = path($filename)->basename; if (-e $filename) { path($filename)->copy("$path/$file"); } else { open my $OUT, '>', "$path/$file" or die $!; print {$OUT} "Unable to find $filename\n"; } }

Note that -e returns true for directories, too, so you might need a different test (-f maybe?). Also, you should check the return value of copy.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Broken Function
Select or Download Code
Re^2: Broken Function ( Path::Tiny spew )
by Anonymous Monk on Apr 05, 2014 at 08:23 UTC
    When you Path::Tiny, to spew is lovely ,fiend :)
    else { path( "$path/$file" )->spew_utf8("Unable to find $filename\n"); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-12-29 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls