Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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]
[ambrus]: Corion: that's not true. Actually for Christmas and Thanksgiving, a lot of people buy electronics such as cameras as present, then many of them figure out they don't need it,
[ambrus]: and the electronics gets reselled almost new, but it has to be sold at half price because otherwise everyone chooses to buy the new product which has fewer risk of selling damaged products labelled as almost new.
[ambrus]: You can actually get a lot of useful cheap really almost new products that way, with only a little risk of scams.
[ambrus]: That's what some of the "Black Friday" sales are about.
[Corion]: ambrus: Well, usually, these people don't have in their description "mail me at dodgy_reseller # g m a i l | co m" , replace the "#" by "@" :)
[Corion]: Oh, and the "o" in "com" is a zero
choroba orders a camera from Ole Scæmmer
[ambrus]: Corion: ah. that's different. the ones I mean are selling at reputable sites like ebay that usually filters scammers out pretty quickly (as well as filters a lot of legitimate users who then get annoyed that the biggest providers exclude them)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2017-11-21 15:01 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (304 votes). Check out past polls.