Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Unable to write to log-file

by turbokwak (Novice)
on Jul 27, 2009 at 13:58 UTC ( #783545=perlquestion: print w/replies, xml ) Need Help??

turbokwak has asked for the wisdom of the Perl Monks concerning the following question:

I'm a newbie to perl and I'm currently breaking my head on an issue now for 4 hours. The following iteration will print to the screen (a directory-listing), however will not print to the file. What i'm I doing wrong?
#Setting the variables $AllDevices='All_Devices.txt'; # Listing the devices opendir (DIR,$_AllConfDir) || die "Error in opening dir $_AllConfDir\n +"; unless (open STRM_AllDevices, ">> $AllDevices") { die "Could not creat +e $AllDevices";} while( ($file = readdir(DIR))){ print STRM_ALLDevices ("$file\n"); print ("$file\n"); } #closedir(DIR); close STRM_AllDevices; close $ConfFile;
Thank you for the response

Replies are listed 'Best First'.
Re: Unable to write to log-file
by ikegami (Patriarch) on Jul 27, 2009 at 14:07 UTC
    Use use strict; use warnings;. You are hiding errors by not using them.
    print() on unopened filehandle STRM_ALLDevices at script.pl line 11. . print() on unopened filehandle STRM_ALLDevices at script.pl line 11. .. print() on unopened filehandle STRM_ALLDevices at script.pl line 11. [snip]

    You open STRM_AllDevices, but you print to STRM_ALLDevices.

Re: Unable to write to log-file
by moritz (Cardinal) on Jul 27, 2009 at 14:10 UTC
    perl -w $your_script.pl Name "main::ConfFile" used only once: possible typo at foo.pl line 16. Name "main::AllDevi" used only once: possible typo at foo.pl line 8. Name "main::STRM_ALLDevices" used only once: possible typo at foo.pl l +ine 11. print() on unopened filehandle STRM_ALLDevices at foo.pl line 11. . print() on unopened filehandle STRM_ALLDevices at foo.pl line 11. ...

    The reason is that you open STRM_AllDevices but write to STRM_ALLDevices, which has a different capitalization and is thus considered to be something else.

    To avoid such problems in future, use the pragmas strict and warnings and use lexical variables for your file handles.

    Example:

    use warnings; use strict; my $AllDevices='All_Devices.txt'; my $_AllConfDir = '.'; # Listing the devices opendir (my $dir ,$_AllConfDir) or die "Error in opening dir $_AllConf +Dir\n"; open my $a, ">>", $AllDevices or die "couldn't open file '$AllDevices for appending: $!"; while ( my $file = readdir($dir)) { print $a "$file\n"; print "$file\n"; } closedir $dir or warn $!; close $a or warn $!;

    Update: added tidied example

Re: Unable to write to log-file
by artist (Parson) on Jul 27, 2009 at 15:55 UTC

    This particular error was visible to me, but in many cases other tools may have helped. For example, with emacs I usually never type the entire big word again. The editor has facility to do that for me with few keystrokes.

    Using the basic tricks of Perl, such as 'use strict;' and 'use warnings;' can help to avoid such 'basic' errors. 'use diagnostics;' will help too.

    The frustration of code not being working take lots of hours for programmers. Getting the basics right at the beginning can make you more confident and productive in long runs.

    --Artist
      These things like "use strict;" and "use warnings;" aren't just "tricks", this is part and parcel of well written Perl code.

      I've used e-macs before but don't use it now. It appears you are using a Windows platform, but yes emacs could be used. Check into cygwin, http://www.cygwin.com/. This isn't Unix but it does a lot! This is how I installed my GNU C compilers. Familiar commands like ps, ls, cat will do things. like ps -h shown below......

      As with Unix, there are a number of good program editors for Windows. Notepad is definitely not the way! I use TextPad, but there are others.

      C:\TEMP>ps -h Usage: ps [-aefls] [-u UID] [-p PID] Report process status -a, --all show processes of all users -e, --everyone show processes of all users -f, --full show process uids, ppids -h, --help output usage information and exit -l, --long show process uids, ppids, pgids, winpids -p, --process show information for specified PID -s, --summary show process summary -u, --user list processes owned by UID -v, --version output version information and exit -W, --windows show windows as well as cygwin processes With no options, ps outputs the long format by default
Re: Unable to write to log-file
by Anonymous Monk on Jul 27, 2009 at 14:11 UTC
    What i'm I doing wrong?

    You're checking the wrong file :)

    open STRM_AllDevices, $AllDevices or die $!; print "READ $_" while <STRM_AllDevices>; close STRM_AllDevices;
      Solved. My hero's. Thanks for pointing this out to me.
Re: Unable to write to log-file
by fetusbear (Initiate) on Jul 27, 2009 at 19:26 UTC
    try using print (STRM_ALLDevices "$file\n"); instead of print STRM_ALLDevices ("$file\n");
      Hi , As you said you need the list of directories in to a log file, for this you can use the flie commands. here is the code which is working fine for me , as it will read all directories inside in the given path .
      ( path have to be given in command line for exampel perl <file name> <folder name> .
      use File::Path;
      use File::Find;
      use vars /@files $file $path/;
      $path = $ARGV[0];
      find sub{ push @files, "$File::Find::name" if(-d
      $File::Find::name); },$path;
      print @files;
      open FILE,">>direcotry.txt" or die $!;
      print FILE @files;

      (-d $File::Find::name) : this will give you the directories inside the path which you gave at command line . ( you can use -f to find the files)
Re: Unable to write to log-file
by dmlond (Acolyte) on Jul 29, 2009 at 17:08 UTC

    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.

Re: Unable to write to log-file
by Anonymous Monk on Jul 29, 2009 at 22:28 UTC
    printf to write to a file

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://783545]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-12-10 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?