http://qs321.pair.com?node_id=1144370


in reply to reference to an undefined key

Hi exilepanda,

You can't do it the way you're trying, because during the creation of the object "$obj" your code is referring to "$obj" itself which doesn't exist yet! That's kind of like trying to define a word by using the same word in its definition:

"A recursive algorithm is an algorithm which recursively calls itself +to produce an answer"

You have a couple of choices. The first is to use the second snippet of code you showed (which you already know works, since it waits until the object $obj is fully defined before trying to reference its data).

Or you could construct the object without referencing the object itself:

use strict; use warnings; package MakeObject; use Data::Dumper; my $rootDir = '/home'; my $userId = 'smith'; my $appName = 'project'; my $obj = MakeObject->new; printf "Object contents => %s\n", Dumper($obj); sub new { my $obj = bless { Root => $rootDir, UserDir => "$rootDir/Usr/$userId/", UserAppData => "$rootDir/Usr/$userId/$appName/", }, shift; return $obj; } __END__ Output is: Object contents => $VAR1 = bless( { 'Root' => '/home', 'UserDir' => '/home/Usr/smith/', 'UserAppData' => '/home/Usr/smith/project/' }, 'MakeObject' );

Since the latter has the same effect -- it only refers to data which is already defined ($rootDir, $userId and $appName, but not $obj) -- it doesn't create the catch 22 effect of the original code.

say  substr+lc crypt(qw $i3 SI$),4,5

Replies are listed 'Best First'.
Re^2: reference to an undefined key
by exilepanda (Friar) on Oct 10, 2015 at 17:39 UTC
    Thanks golux! your second opt is what I hope not to use as it can come really lengthy and will be my disaster if something is going to change ( eg. /Usr/ needed to change as /Users/ )... But what you propose remind me something I did yrs ago, do you have any feedback to my update on the post ?
      You're asking if there's any module for dealing with resolving templates. I'm sure there are, but none offhand that I've used extensively. (Did you try a search of cpan?)

      As to the inherent dangers of elaborating things as you suggest, the one that comes to mind is -- you'd better only make use of key/value pairs in the order they're resolved, or you may come up against infinite recursion. For example:

      [ConfigDirs] A = /some/dir B = /_#C#_/DeeperDir C = /_#B#_/EvenDeeperDir

      which would never fully expand (B references C, which references B again). So you wouldn't want to necessarily make that into a hash; a list would be more appropriate I think, as you could throw an error any time you came across a variable which hadn't previously been defined.

      Here's an example that uses an array (hardwired, but you could read it in from a config file), and then creates a hash of resolved parameters:

      #!/usr/bin/perl ############### ## Libraries ## ############### use strict; use warnings; use feature qw{ say }; ################## ## User-defined ## ################## # Define what makes a "template" variable my $re_template = qr/_#([^#]+)#_/; # The result of what you read from your config file, perhaps. # Note that it's an ARRAY, since we'd like to prevent unintentional # recursion (we should resolve template variables in the order in # which they're seen). my $a_params = [ [ 'A' => '/some/dir' ], [ 'B' => '/_#A#_/DeeperDir' ], [ 'C' => '/_#B#_/EvenDeeperDir' ], ]; ################## ## Main program ## ################## show_params($a_params, "[Before resolution]"); my $h_resolved = resolve_templates($a_params); show_params($h_resolved, "[After resolution]"); ################# ## Subroutines ## ################# sub resolve_templates { my ($a_params) = @_; my $h_resolved = { }; for (my $i = 0; $i < @$a_params; $i++) { my $idx = $i + 1; my $a_keyval = $a_params->[$i]; my ($key, $tmpl) = @$a_keyval; my $orig = $tmpl; while ($tmpl =~ /$re_template/) { my $ref = $1; my $refval = $h_resolved->{$ref}; defined($refval) or die "Undefined ref '$ref' in '$orig'\n +"; $tmpl =~ s/$re_template/$refval/g; } $h_resolved->{$key} = $tmpl; } return $h_resolved; } sub show_params { my ($a_params, $msg) = @_; $msg ||= "Parameters:"; say "-" x 79; say $msg; say "-" x 79; if (ref $a_params eq 'HASH') { # If a HASH was passed, turn it into an ARRAY my $h_params = $a_params; my @keys = sort { $a cmp $b } keys %$h_params; $a_params = [ map { [ $_ => $h_params->{$_} ] } @keys ]; } foreach my $a_keyval (@$a_params) { my ($key, $val) = @$a_keyval; my $text = "Parameter '$key' "; $text .= "." x (32 - length($text)); $text .= " '$val'"; say $text; } say ""; }

      And here its output:

      ---------------------------------------------------------------------- +--------- [Before resolution] ---------------------------------------------------------------------- +--------- Parameter 'A' .................. '/some/dir' Parameter 'B' .................. '/_#A#_/DeeperDir' Parameter 'C' .................. '/_#B#_/EvenDeeperDir' ---------------------------------------------------------------------- +--------- [After resolution] ---------------------------------------------------------------------- +--------- Parameter 'A' .................. '/some/dir' Parameter 'B' .................. '//some/dir/DeeperDir' Parameter 'C' .................. '///some/dir/DeeperDir/EvenDeeperDir'

      say  substr+lc crypt(qw $i3 SI$),4,5