in reply to Re^2: creating valid paths for Path::Tiny
in thread creating valid paths for Path::Tiny

Datz_cozee75 wrote:

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, ); ... $vars{"grandfather"} = $vars{"init_dir"}->parent();
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:

use warnings; use strict; use Path::Tiny; use feature 'say'; use Data::Dumper; 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, ); $vars{"grandfather"} = $vars{"init_dir"}->parent(); print Dumper(\%vars); print "vars-grandfather : ", $vars{"grandfather"}->basename, "\n"; print "vars-grandfather : ", $vars{"grandfather"}; __DATA__ path1 is C:/usr/pm base is pm $VAR1 = { 'init_dir' => bless( [ 'C:/usr/pm', 'C:\\usr\\pm', 'C:', '/usr/', 'pm' ], 'Path::Tiny' ), 'title' => 'pm', 'grandfather' => bless( [ 'C:/usr', 'C:\\usr' ], 'Path::Tiny' ) }; vars-grandfather : usr vars-grandfather : C:/usr