in reply to creating valid paths for Path::Tiny
In addition to what soonix posted about chomping newlines a recommendation for troubleshooting and general validation is to use file tests to check for the existence of files. They can save you a lot of hair pulling in this situation.
foreach my $file (@files) { if( -f $file) { print "<$file> is a plain file.\n"; } else { print "<$file> is not a plain file.\n"; } }
This can be shortened with the conditional operator:
use strict; use warnings; my @files = qw( file1 test.pl.txt file3 4); push @files, "file5 "; foreach my $file (@files){ print "<$file> is ", -f $file ? '' : 'not ', "a plain file.\n"; }
I encourage you to consider using strict also.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: creating valid paths for Path::Tiny
by soonix (Chancellor) on Aug 14, 2018 at 17:38 UTC | |
Use -f instead if you really mean to check for a plain file. Another ++ I'd give for your example of enclosing the file name in visible delimiters like "<$file>". However, use 5.011 implies strict (although use states 5.012 as the first with this property) so OP already is "strict-compliant" ;-) | [reply] [d/l] [select] |
by Lotus1 (Vicar) on Aug 14, 2018 at 19:27 UTC | |
Path::Tiny seems like a really useful module. Thanks for pointing out that it includes file tests. Your observation about 'use' seems like it would be a good bug report for Perldocs. | [reply] |
|
Re^2: creating valid paths for Path::Tiny
by Aldebaran (Curate) on Aug 14, 2018 at 21:33 UTC | |
Thanks all for comments. I'm getting decent partial results, with a manifest that got a newline in it as other processes added filenames to it. Instead of editing out the blank line, I tried to code around it.
Caller is getting more complicated, and I send a reference of the main data hash to archive1() to embellish on:
I highlighted a code question in the above listing and am puzzled that in the following, a key/value pair for $vars{"grandfather"} seems not to be created. In other words, the hash does not seem to be extensible in the way I thought all hashes in perl were:
Output:
Fishing for tips.... | [reply] [d/l] [select] |
by hippo (Archbishop) on Aug 14, 2018 at 22:02 UTC | |
### does this^^^ have a Path::Tiny equivalent? Yes. It is the absolute method.
| [reply] [d/l] [select] |
by Lotus1 (Vicar) on Aug 15, 2018 at 01:47 UTC | |
Datz_cozee75 wrote:
I highlighted a code question in the above listing and am puzzled that in the following, a key/value pair for $vars{"grandfather"} seems not to be created. In other words, the hash does not seem to be extensible in the way I thought all hashes in perl were: Hi Datz_cozee75. If you have a look at perldata you can read about how hashes work. A hash is an associative array of scalar values. That means the only thing you can store in a hash is a string, a number or a reference. In the code section above you seem to be trying to call the parent() method against the value of $vars{"init_dir"}. Did you get any warnings when you ran this program? Update: I tried a small test of your code above and it works. I realized after I posted and looked more closely at the output you posted that the hash value is holding a reference to the path object. Why do you say that a key/value pair for $vars{"grandfather"} is not created? It looks like the following line in your output shows that the key value pair was created. from is /home/bob/1.scripts/pages/1.manifest
Since you asked for tips I suggest starting with a much smaller program and build functionality until you have your final result. Smaller, simpler functions allow you to test and get it working before adding functionality. Also, your tested code can be kept separate from new code that way. Here is my test script:
| [reply] [d/l] [select] |