in reply to How to walk through convoluted data?

the following part of your code has "class-factory" written all over it in big flashing red letters:

# called with : mkBless(\$p->{Vcpeid}{$cpeid}, RepoVData->new); sub mkBless($;$) { my $targ = shift; if (@_) { ${$targ} = shift unless blessed $targ; } \$targ }

(btw i think you have errors in above code: shouldn't condition be: blessed $$targ and return $$targ ?)

Nothing fancy though:

# notice @ params: contails optional class name and any optional par +ams to new() sub mkBless($;@) { my $targ = shift; if (@_) { ${$targ} = shift->new(@_) unless blessed $$targ; } # you don't need to return anything, but if you must: $$targ; #\$targ } # example call: my $obj = mkBless(\$someotherobj, 'RepoVData', 1,2,3);

another observation: your code calls mkBless() with a newly created object which you discard if the 1st param is already blessed. That's a waste.

bw, bliako

Replies are listed 'Best First'.
Re^2: How to walk through convoluted data?
by perlfan (Parson) on Jul 20, 2021 at 22:29 UTC
      ... majorly helpful, open electronic form.

      perlfan's link is a free (but not public domain) download direct from Dominus.


      Give a man a fish:  <%-{-{-{-<

      My bible!, or one of them anyway, Great inspiration for my P module or Types::Core, maybe even Xporter in CPAN. Some invaluable modules, though needing a bit of update. someday, at some point. But HoP...any particular chapter or page? I only have a paper copy, no majorly helpful, open electronic form.

      Don't even find 'Class Factory' in its Index, so maybe under a different conceptual idea or term?

      Maybe it's not a good book for this, as I might never get the program working -- but might spend another few months re-factor/re-writing it again :^)

      Yeah, since it's a rolling, release, I could express it as some type of infinite series, though am not sure what formula I could apply to the input to get useful output...

      Given the discrete inputs by various authors for package releases, I'd have to allow for a callback at each stepping of the release to allow for new output. Certainly no lossy algorithm would work -- it would just get everyone p'd off at me in exponential time. Must be in the nature of linux distros...*sigh* Maybe I'll try a different part of book for inspiration, though I'm not sure how quickly such an endeavor will result in any convergence to the actual output...

      (only 1/2**(# times attempted) for actual applicability).

      Maybe I need a better paradigm as I sense only divergence down this route....

      I hope someone reading this has a sense of humor or alot more insight!

      -l

Re^2: How to walk through convoluted data?
by perl-diddler (Chaplain) on Jul 21, 2021 at 20:41 UTC
    May be a class factory, remember this is a rewrite of a previous version that only read the xml files as a guide of that rpms to download. That there are errors in it, is unsurprising, since, while I started with working code, I'm no where close to the new code that needs to go into its place. I'll have to reread some source(s) about class facs.

    As for throwing away the output of mkBless...that's because it is equivalent to:

    sub new() {my $p = shift; my $c=ref $p||$p; my $arghash = @_ ? shift : {}; blessed $p? $p->SUPER::new($arg) : ($p = $c->new($arghash); $p }
    except that there was a chain of included blessed object before the final output -- that's what I tried to short with mkBless (which I just cooked up for this example, actually, so I can't really defend its usage much, it being my first experience with it.

    So bugs? ya ya!, plenty, it was meant to be example code to give an idea of what I was having to do to generate / access data, its definitely not working code! ;-( :-) :-)

    -l

Re^2: How to walk through convoluted data?
by perl-diddler (Chaplain) on Jul 22, 2021 at 11:21 UTC
    Not to dispute anything you are suggesting, but more to explain how I got there. Before mkBless (something I came up with on the fly while writing there -- not always my best work -- BREAK--- Just as an FYI why I seem to go away for a while in middle of conversations. My typing speed has decayed something fierce over the years...like down to 20-30% of faster times. No way can my typing keep up with my thoughts these days so sometimes blocks of explanatory text are skipped -- confusing readers and myself. But sigh.... In case someone doesn't know, use mem allows me to mostly easily include a bunch of packages in 1 files.
    somwhere further above: { package RPM_data; #{{{ use constant D => q(-); #D for Dash use constant o => q(.); #o for dot use Data::Vars [qw(N V R A reldir reponame cpeids size)], {cpeids=>sub{ {} } }; # N-keys of this is ref-cnt sub cpeids() { my $p = shift; scalar keys %{$p->{cpeids}} } sub cpeid(;$) { return q(cpeid) unless @_; my $p = shift; my $cpeid = shift; mkARRAY $p->cpeids; push @{$p->cpeids}, $cpeid; my $cpeids = ErV $p, cpeids; push @{$p->cpeids}, $cpeid unless ErV $cpeids, $cpeid; return $p->cpeids($cpeid); } sub new($) { my $p = shift; my $c = ref $p || $p;...} sub new_from_file($) { my $p = shift; my $fname = shift; ... sub new_from_path($) { my $p = shift; my $pthnam = shift;... sub VR () { my $p = shift; $p->{V} .D. $p->{R} } sub NVR () { my $p=shift; $p->{N} .D. $p->VR } sub NVRA () { my $p=shift; $p->NVR .o. $p->{A} } sub relpth() {my $p = shift; pathcat($p->reldir, $p->NVRA .o."rpm")} }##### { package VRs; #{{{ use Types::Core qw(blessed LongSub); use Data::Vars [qw(VRs)], {VRs=>sub{ {} }}; use P; use Dbg(1,1,1); sub vr($;$) { my $p = shift; my $c = ref $p || $p; my $argp = shift; my $vr = ErV $argp, VR; my $vrp; if (@_) { $p->{VR}{$vr} = RPM_data->new(shift); } $p->{VR}{$vr}; } 1;} #}}} ###### (somewhere above: { package RepoVData; #{{{ use strict; use warnings; use mem; use Data::Vars [qw(RepoDB RepoXMLs )], #RDFile_inf { RepoXMLs => sub() { {} }, # HASH{type => RDFil +e_inf} RepoDB => sub() { {} }, }; # nam=>rpmdata

    And then a bunch of these progresssive assignments checking to see if each section is blessed. This started getting very repetitive and ugly looking.

    $p->{Vcpeid}{$cpeid} = RepoVData->new() unless blessed $p->{Vcpeid}{$c +peid}; $p->{Vcpeid}{$cpeid}{RepoDB} = RepoDB->new() unless blessed $p->{Vcpei +d}{$cpeid}{RepoDB} $p->{Vcpeid}{$cpeid}{RepoDB}{$nam} = VR->new() unless blessed $p->{Vcpeid}{$cpe +id}{RepoDB}{$nam}; $p->{Vcpeid}{$cpeid}{RepoDB}{$nam}{vr($V, $R) = RPM_data->new({...}) u +nless blessed $p->{Vcpeid}{$cpeid}{RepoDB}{$na +m}{vr($v, $R) ....

    This is where I saw I had:

    lvalue = new addon unless blessed lvalue;

    That's where mkBless came in -- to check if the levalue was blessed, and if so, assign more of the data-to-follow onto it.

    I had it return a value so I could skip some repetition at the beginning of each line.

    So, call that a class factory if you want, I'm not sure it is, but at least wanted you to see how it "fell out of" progressively adding on more data to walk through this DB.

    (is there anyway to insert a picture or diagram in this chaos? as it might help me clarify where I'm going....the old pic is worth a thousand words thing.

    Thanks for the feedback so far, I won't be annoyed if anyone dropps out of this mess, I sorta wish I could, but it stuff I need to get done to manage my system, UG.

    cheers!...oh and the bit about doing that "blessed test or return less work" bit, I probably would have gotten there eventually, in some later cleanup -- I put focus first on getting something to work -- and yes, I am one of those who keeps working at things to clean them up and make them better. -- because if I know I need to do that for my "future self" (if no one else) to able to reuse the code I write today.