http://www.perlmonks.org?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