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

Re: Unable to write to log-file

by dmlond (Acolyte)
on Jul 29, 2009 at 17:08 UTC ( #784295=note: print w/ replies, xml ) Need Help??


in reply to Unable to write to log-file

Using strict and warnings is critical to writing working code. It will save you alot of headaches. Another thing you might want to do is get into the habit of using the <> operator to read in the contents of a directory (available, I believe, since perl 5.6). This makes reading the contents from a directory much more idiomatically similar to reading in the contents of a file. Also, the perl open function can now open handles into an IO::Handle object, which, again, allows you to make greater use of the file handles as references passed to subroutines, etc. Finally, I always use the 3 argument version of open. It makes it much more intuitive what you are doing, and prevents whitespace mistakes that can occur when trying to pass a string in with the handle type and the path in the same string. Here is a bit of code that can do what you want:

use strict; use warnings; my $allDevices = 'All_Devices.txt'; open (my $device_h, '>>', $allDevices) or die "Could not create $allDe +vices $!\n"; while (my $file = <$_allConfDir/*>) { print $device_h $file; } close $device_h;

Another advantage of the <> usage is that it accepts unix regular expressions, so instead of doing:

next uness ($file =~ m/\.txt$/);

You can just use:

<$_allConfDir/*txt>

Hope this helps.


Comment on Re: Unable to write to log-file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-04-20 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls