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

Hi,
I want to do some processing on set of files and write the $result into a ".html" file. But this result need to be stored into "result_dir" directory.

What's wrong with my code below, such that it doesn't write the result into the desired directory?
foreach my $file (@files) { my $OUTFILE_fname = $file.".html"; # output file name open ( OUTFILE, '>', result_dir/$OUTFILE_fname ) or croak "$0 : failed this file $OUTFILE_fname : $!\n"; my $result = process($file); print OUTFILE $result; close ( OUTFILE ); # close output file }
Update: Correction made, from $OUTFILE_file_name into $OUTFILE_fname. Thanks to Ultra's suggestion. But problem remains.
Update2: Problem solved. Thanks to all.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Howto direct an output filehandle into directory?
by g0n (Priest) on Dec 06, 2005 at 08:39 UTC
    You need quotes around result_dir/$OUTFILE_fname

    When I attempt to run your code, I get:

    Illegal division by zero at neversaint.pl line 7
    as the text directory name 'result_dir' is being divided by the scalar $OUTFILE_fname.

    Using use strict gives a 'bareword' error.

    --------------------------------------------------------------

    "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

    John Brunner, "The Shockwave Rider".

Re: Howto direct an output filehandle into directory?
by tirwhan (Abbot) on Dec 06, 2005 at 08:57 UTC

    This is not in direct response to your question (g0n already answered that), but it is better practice to use lexical variables instead of barewords for filehandles. This avoids confusion with differently scoped file handles, especially with common names such as "OUTFILE".

    Also, variable names in CAPS are generally frowned upon (in this case I find it particularly confusing that you use a variable name that consists of one word in caps and one in lowercase). So I'd suggest you change your code like this:

    foreach my $file (@files) { my $outfile_fname = $file.".html"; # output file name open ( my $out_handle, '>', "result_dir/$outfile_fname" ) or croak "$0 : failed this file $outfile_fname : $!\n"; my $result = process($file); # You should check the return value on any file I/O operation print $out_handle $result or croak "Failed to print to $outfile_fnam +e"; close ( $out_handle ) or croak "Failed to close $outfile_fname"; }

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Howto direct an output filehandle into directory?
by Ultra (Hermit) on Dec 06, 2005 at 08:37 UTC

    Quickly looking at your code:

  • You use different variable names for something that I think is the same thing: my  $OUTFILE_fname = $file.".html"; and result_dir/$OUTFILE_file_name
  • Maybe result_dir should be $result_dir?



    I think you should:

  • use warnings
  • use strict

    Dodge This!
Re: Howto direct an output filehandle into directory?
by Anonymous Monk on Dec 06, 2005 at 09:16 UTC

    Hi,

    There is a little typos in your code

    open ( OUTFILE, '>', result_dir/$OUTFILE_fname )

    should be

    open ( OUTFILE, '>', "result_dir/" . $OUTFILE_fname )

    Regards
    Franklin

A reply falls below the community's threshold of quality. You may see it by logging in.