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, { verbose => 1 })
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.use File::Path; use Path::Class::File; my $pcd = Path::Class::Dir->new('test_dir', 'test_subdir'); make_path($pcd)
Giving the following output:#!/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;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |