Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Broken Function

by choroba (Bishop)
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.

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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081127]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-05-21 00:00 GMT
Find Nodes?
    Voting Booth?