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

I encountered a problem using File::Util.

I am using

My application has to be able to save files to arbitrary drives on my machine (no network drives). So far I can save files on the same drive the script is living in, but when it comes to saving on a different drive it fails:

File::Util can't use this string for »the name of a file or directory« +. »D:/projects/my_program/TestData/text.txt« It contains illegal characters.

I know that colons are illegal characters from the error message.
Is it possible to save files to drives other than the one my script is on an how do I specify the path?
Can someone please help me?

Here is an example of what I am trying to do. The test application consists of two buttons only. One for selecting the directory, the other for saving the file.

use strict; use warnings; use Tk; use File::Util; my $mw = MainWindow->new(); my $fh = File::Util->new(); my $directory = ""; my $browse_button = $mw->Button( -text => "Browse", -command => sub { $directory = $mw->chooseDirectory( -mustexist => "true", ), }, )->pack(); my $save_button = $mw->Button( -text => "Save", -command => sub { $fh->make_dir( $directory, qw /--if-not-exists/ ); $fh->write_file( file => sprintf( "%s/text.txt", $directory ), content => "hello world", ); }, )->pack(); MainLoop;

Replies are listed 'Best First'.
Re: Problem with saving files using File::Util on Windows
by kennethk (Abbot) on Dec 22, 2009 at 15:48 UTC
    You might do well to read How do I post a question effectively? and take to heart the part about smallest example, in that I think I can replicate your issue without Tk. It would also be good if the script replicated your issue exactly, rather than requiring the monks to input a directory name, i.e. my $directory     = 'C:\temp'; or some such.

    The issue with absolute file names under Windows is a known issue, and has an open and acknowledged bug report on CPAN: http://rt.cpan.org/Public/Bug/Display.html?id=46368. I would suggest working with relative paths for now. If this is not an option (you need it now), roll your own using core Perl modules such as File::Path and IO::File.

      Thank you for your reply. I read "How do I post a question effectively?" and please excuse me for bugging you all with Tk. I apparently had a different understanding of "minimal example". The script replicated my problem, but I see what you mean: "Make it as easy as possible to reproduce the problem using the least amount of lines."

      I used File::Path and IO::File as you suggested and everything is working by now. Thank you again!

        You did reasonable job minimizing the code, but the point I was making was that your issue did not require Tk. If I read a post and see use Tk; up top, I assume the issue pertains to GUI. As I am not familiar with the intricacies of Tk, I'm likely to skip it without much consideration since I figure there are monks more well suited to aid that OP. In addition, testing your code requires I have Tk installed on my machine, and I'm not going to install Tk just to run the code of an unknown netizen.

        In summary, your priorities in creating code to be posted should be first use strict; use warnings;, then the smallest number of dependencies, and finally short but clear code.

Re: Problem with saving files using File::Util on Windows
by ikegami (Patriarch) on Dec 22, 2009 at 16:04 UTC

    It seems the module doesn't allow two more or of the following in a row in a path: /, \ and :. That means it doesn't allow

    • Absolute paths on Windows
      • e.g. C:\...
      • e.g. \\server\share\...
    • Normally accepted redundant slashes on unix (e.g. /usr//bin)

    Buggy!