Hello Monks, I encountered a problem (I have a solution), but I thought that someone may be able to explain exactly what is going on. I was using Path::Class::Dir with the make_path method from the File::Path module. I had started with code like this:
use File::Path; use Path::Class::File; my $pcd = Path::Class::Dir->new('test_dir', 'test_subdir'); make_path($pcd, { verbose => 1 })
With the above code, the path would get created and my script would succeed. When I no longer wanted verbose output, I deleted the { verbose => 1 } hashref.
use File::Path; use Path::Class::File; my $pcd = Path::Class::Dir->new('test_dir', 'test_subdir'); make_path($pcd)
This code would fail (the path would not be created). I assume this has something to do with the context of $pcd in the make_path call, but I would be interested in a more precise explanation. If I force the stringification of $pcd (with $pcd->stringify or "$pcf" then it works again). Here is a more extensive lists of tests.
#!/usr/bin/env perl use strict; use warnings; use File::Path qw/make_path remove_tree/; use Path::Class; my $t_dir = 'test_dir'; my $t_subdir = 'test_subdir'; my $pcd = Path::Class::Dir->new($t_dir, $t_subdir); # Test1 make_path($pcd); if ( -e $pcd ) { print "make_path(\$pcd) worked!\n"; } else { print "make_path(\$pcd) did NOT work\n"; } remove_tree($t_dir); # I know this works, use it for cleanup # Test2 make_path($pcd, {}); if ( -e $pcd ) { print "make_path(\$pcd, {}) worked.\n"; } else { print "make_path(\$pcd, {}) did NOT work.\n"; } remove_tree($t_dir); # Test3 my $t_dir_pcd = Path::Class::Dir->new($t_dir); make_path($pcd, {}); # I know this works, use it to create path before + removal attempt remove_tree($t_dir_pcd); if ( -e $t_dir_pcd ) { print "remove_tree(\$pcd) did NOT work.\n"; } else { print "remove_tree(\$pcd) worked.\n"; } remove_tree($t_dir); # Actually remove the tree # Test4 make_path($pcd, {}); remove_tree($t_dir_pcd, {}); if ( -e $t_dir_pcd ) { print "remove_tree(\$pcd, {}) did NOT work.\n"; } else { print "remove_tree(\$pcd, {}) worked.\n"; } remove_tree($t_dir); # Actually remove the tree # Test5 make_path($pcd->stringify); if ( -e $pcd ) { print "make_path(\$pcd->stringify) worked\n"; } else { print "make_path(\$pcd->stringify) did NOT work\n"; } remove_tree($t_dir); # Actually remove the tree #Test6 make_path($pcd, {}); remove_tree($t_dir_pcd->stringify); if ( -e $t_dir_pcd ) { print "remove_tree(\$t_dir_pcd->stringify) did NOT work.\n"; } else { print "remove_tree(\$t_dir_pcd->stringify) worked.\n"; } remove_tree($t_dir); # Actually remove the tree # Test7 make_path("$pcd"); if ( -e $pcd ) { print "make_path(\"\$pcd\") worked\n"; } else { print "make_path(\"\$pcd\") did NOT work\n"; } remove_tree($t_dir); # Actually remove the tree #Test8 make_path($pcd, {}); remove_tree("$t_dir_pcd"); if ( -e $t_dir_pcd ) { print "remove_tree(\"\$t_dir_pcd)\" did NOT work.\n"; } else { print "remove_tree(\"\$t_dir_pcd\") worked.\n"; } remove_tree($t_dir); # Actually remove the tree exit;
Giving the following output:
make_path($pcd) did NOT work make_path($pcd, {}) worked. remove_tree($pcd) did NOT work. remove_tree($pcd, {}) worked. make_path($pcd->stringify) worked remove_tree($t_dir_pcd->stringify) worked. make_path("$pcd") worked remove_tree("$t_dir_pcd") worked.

In reply to Problem when using Path::Class::Dir with File::Path by kevbot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.