Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
... 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

sub 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; }
or maybe
sub new { my $class = shift; my ($uid, $root, $app) = @_; return bless { Root => $root, UserDir => "$root/Usr/$uid/", UserAppData => "$root/Usr/$uid/$app", } => $class; }
(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!

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:  <%-{-{-{-<

In reply to Re^3: reference to an undefined key by AnomalousMonk
in thread reference to an undefined key by exilepanda

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2020-04-06 02:32 GMT
    Find Nodes?
      Voting Booth?
      The most amusing oxymoron is:

      Results (36 votes). Check out past polls.