in reply to Re: creating valid paths for Path::Tiny
in thread creating valid paths for Path::Tiny
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.
$ cat 1.manifest 2.create.bash 11.clone.pl 1.initialize.pl 1.manifest 1.med 1.meditation 1.pop 1.qy 1.rings 2.med 5.create.sh 5.unicode 3.create.bash $
Caller is getting more complicated, and I send a reference of the main data hash to archive1() to embellish on:
$ cat 4.archive.pl #!/usr/bin/perl -w use 5.011; use lib "template_stuff"; use utils2; use Path::Tiny; use utf8; use open qw/:std :utf8/; use Data::Dumper; # initializations that must precede main data structure my $fspecfile = File::Spec->rel2abs(__FILE__); ### does this^^^ have a Path::Tiny equivalent? ## turning things to Path::Tiny my $path1 = Path::Tiny->cwd; say "path1 is $path1"; my $title = $path1->basename; say "base is $title"; # script parameters my %vars = ( init_dir => $path1, title => $title, script_file => $fspecfile, ); # caller my $rvars = \%vars; my $return1 = archive1( $rvars ); # returns created dir chdir( $return1 ); system("pwd"); system("find ."); __END__ $
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:
$ cat utils2.pm package utils2; require Exporter; use utils1; use utf8; use open qw/:std :utf8/; use Data::Dumper; our @ISA = qw(Exporter); our @EXPORT = qw( archive1 ); sub archive1 { use warnings; use 5.011; use Path::Tiny; my $rvars = shift; my %vars = %$rvars; say Dumper $rvars; $vars{"grandfather"} = $vars{"init_dir"}->parent(); my $file1 = "1.manifest"; my $from = path( $vars{"grandfather"}, $file1 ); say "from is $from"; my @files = $from->lines_utf8( { chomp => 1 } ); say Dumper $rvars; say "files are @files"; my $tempdir = Path::Tiny->tempdir('backup_XXXXXX'); say "temp dir is $tempdir"; my $readme = $tempdir->child( 'grandmother', 'README.txt' )->touchpa +th; say "read me is $readme"; my $grand_dir = $readme->parent; chdir $grand_dir; foreach my $item (@files) { say "item is <<$item>>"; next if ($item eq ""); my $abs = path( $vars{"grandfather"}, $item ); say "abs is <$abs>"; if ( -d $abs ) { say "$abs is a directory"; } if ( -f $abs ) { say "$item is a plain file"; #syntax is from -> to my $return = path($abs)->copy( $grand_dir, $item ); if ( $item =~ m/\.(pl|sh)$/ ) { $return->chmod(0755); } say "return is $return"; } } my $b = $tempdir; return $b; } 1; $
Output:
$ ./4.archive.pl path1 is /home/bob/1.scripts/pages/1.qy base is 1.qy $VAR1 = { 'init_dir' => bless( [ '/home/bob/1.scripts/pages/1.qy', '/home/bob/1.scripts/pages/1.qy', '', '/home/bob/1.scripts/pages/', '1.qy' ], 'Path::Tiny' ), 'title' => '1.qy', 'script_file' => '/home/bob/1.scripts/pages/1.qy/4.archive.p +l' }; from is /home/bob/1.scripts/pages/1.manifest $VAR1 = { 'init_dir' => bless( [ '/home/bob/1.scripts/pages/1.qy', '/home/bob/1.scripts/pages/1.qy', '', '/home/bob/1.scripts/pages/', '1.qy' ], 'Path::Tiny' ), 'title' => '1.qy', 'script_file' => '/home/bob/1.scripts/pages/1.qy/4.archive.p +l' }; files are 2.create.bash ... 5.unicode 3.create.bash temp dir is /tmp/backup_MKb3nU read me is /tmp/backup_MKb3nU/grandmother/README.txt item is <<2.create.bash>> abs is </home/bob/1.scripts/pages/2.create.bash> 2.create.bash is a plain file return is /tmp/backup_MKb3nU/grandmother/2.create.bash ... item is <<5.unicode>> abs is </home/bob/1.scripts/pages/5.unicode> /home/bob/1.scripts/pages/5.unicode is a directory item is <<>> item is <<3.create.bash>> abs is </home/bob/1.scripts/pages/3.create.bash> 3.create.bash is a plain file return is /tmp/backup_MKb3nU/grandmother/3.create.bash /tmp/backup_MKb3nU . ./grandmother ./grandmother/1.initialize.pl ./grandmother/3.create.bash ./grandmother/5.create.sh ./grandmother/1.manifest ./grandmother/2.create.bash ./grandmother/README.txt ./grandmother/11.clone.pl cannot remove path when cwd is /tmp/backup_MKb3nU for /tmp/backup_MKb3 +nU: at /usr/share/perl/5.26/File/Temp.pm line 1583. $
Fishing for tips....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: creating valid paths for Path::Tiny
by hippo (Archbishop) on Aug 14, 2018 at 22:02 UTC | |
|
Re^3: creating valid paths for Path::Tiny
by Lotus1 (Vicar) on Aug 15, 2018 at 01:47 UTC |