Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Broken Function

by choroba (Canon)
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
Replies are listed 'Best First'.
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 studying the Monastery: (7)
As of 2015-07-08 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls