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

In reply to Re^3: creating valid paths for Path::Tiny by Lotus1
in thread creating valid paths for Path::Tiny by Aldebaran

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.