Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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:


Hope this helps.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://784295]
[Corion]: Discipulus: Yes, very good spot!
[Discipulus]: a lucky programmer.. as napoleon used to say..
erix wonders... how is the neurocent doing these days?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-05-25 15:46 GMT
Find Nodes?
    Voting Booth?