Hello, so to summarize everything, I've learned a few things:
  1. I learned that in order to use aware functions on complex variables, it's better to use objects with methods.
  2. I learned that packages can be in the same file as the main script.
  3. I learned that bless function makes methods work in objects.
One thing that confused me in bless function was the way it was written:
bless { @_ }, $class;
I couldn't understand it, forgetting that functions can be used without parentacies (I sined myself, doing that) and arrays can be applied and converted to hashes that way.

Then, after digesting all of the information, you guys gave me and taking a closer look, I've had a revelation that { @_ } is nothing other then a regular anonymous hash reference of @_ array being converted to hash ref by {} braces and a $class is a reference to Self object.

So, not exactly, but in other words:
my %hash = @_; bless(\%hash, $selfObjRef);
or
bless({@_[1..$#_]}, $_[0]);
One thing I am a little confused still is, when I add values to the variable this way:
my %phrases = ( "phrase 1" => Qty->new( qty => 1, qtySum => 5, qtyCont => 8 ), "phrase 2" => Qty->new( qty =>10, qtyCont =>34 ), "phrase 3" => Qty->new( qty =>1 ) );
Can I still access them by (I didn't test it yet, still trying to digest everything)?:
print $phrases{'phrase 2'}->{'qtySum'}; print $phrases{'phrase 1'}->{'qtyCont'}; print $phrases{'phrase 3'}->{'qty'};
I thought about coding the way, you've described in your first 2 methods with closures before posting to perlmonks, but things that stopped me were a curiosity if it's possible to do it without passing parameters, informing about it's container to a function and also I needed the function to be able to accept explicit parameters, because I do not always need a total of all qtys. Sometimes, I would need a total of qty and qtyCont and sometimes, a qty and qtySum, etc..

You guys've made a point that it's done with objects, and I need to read more to understand them better and experiment a little bit.

While reading, your first 2 methods, I remembered of using static variables in regular subs, which, in turn made me think about ability to pass explicit values without polluting them with container info to an anonymous sub, which passes all of the values to the sub, calculating qtys.

my %phrases = ( 'phrase 1'=>{ qty=>1, qtySum=>5, qtyCont=>8, qtyTotal=>sub{return &qtyTotal('phrase 1', @_);} }, 'phrase 2'=>{ qty=>10, qtyCont=>34, qtyTotal=>sub{return &qtyTotal('phrase 2', @_);} }, 'phrase 3'=>{ qty=>1, qtyTotal=>sub{return &qtyTotal('phrase 3', @_);} } );
I've used a key value, but I understand, that the current branch reference can also be used.

I realize, that this is an inefficient solution, but this is the solution that allows using pseudo methods without an actual objects.

I need to play some more with objects to completely grasp the subject, while the pseudo method is a temporary solution, until I completely understand objects and update my program to use objects instead.

Thank You all for your help.


In reply to Re^2: How to detect a hash container key from a referenced sub value by igoryonya
in thread How to detect a hash container key from a referenced sub value by igoryonya

Title:
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, details, 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, summary, 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.