in reply to Re^2: reference to an undefined key
in thread reference to an undefined key
... I know why my example won't work... but I am juz looking for if any work around to declare and define my object's properties in the style alike what I hope can happen.
The Monastery may harbor certain subtle and puissant monks who possess the dark art of accessing an object before that object is completely defined, but their ways should not be yours if you seek the Way of Clarity!
If you wish "to state the logic of the properties in clear", I don't see why, combining the approaches of golux and stevieb with my own personal preferences, an approach like
or maybesub new { my $class = shift; my ($uid, $root, $app) = @_; my $obj = bless {} => $class; $obj->{Root} = $root; $obj->{UserDir} = "$obj->{Root}/Usr/$uid/"; $obj->{UserAppData} = "$obj->{UserDir}$app"; return $obj; }
(both tested) would not be sufficiently clear. (Note the trailing / path separator is not needed on the $root path specification.) Why be sad? Bask in the light of the Clear Path!sub new { my $class = shift; my ($uid, $root, $app) = @_; return bless { Root => $root, UserDir => "$root/Usr/$uid/", UserAppData => "$root/Usr/$uid/$app", } => $class; }
Update: Just to be sure:
c:\@Work\Perl>perl -wMstrict -le "use warnings; use strict; use Test::More 'no_plan'; use Test::NoWarnings; use Data::Dumper; package Test; sub new1 { my $class = shift; my ($uid, $root, $app) = @_; my $obj = bless {} => $class; $obj->{Root} = $root; $obj->{UserDir} = \"$obj-^>{Root}/Usr/$uid/\"; $obj->{UserAppData} = \"$obj-^>{UserDir}$app\"; return $obj; } sub new2 { my $class = shift; my ($uid, $root, $app) = @_; return bless { Root => $root, UserDir => \"$root/Usr/$uid/\", UserAppData => \"$root/Usr/$uid/$app\", } => $class; } package main; my $test1 = Test->new1('steve', '/home', 'perl'); my $test2 = Test->new2('steve', '/home', 'perl'); isa_ok $test1, 'Test', '$test1'; isa_ok $test2, 'Test', '$test2'; is_deeply $test1, $test2, qq{object data equivalent}; " ok 1 - $test1 isa Test ok 2 - $test2 isa Test ok 3 - object data equivalent ok 4 - no warnings 1..4
Give a man a fish: <%-{-{-{-<
|
|---|