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.bless { @_ }, $class;
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:ormy %hash = @_; bless(\%hash, $selfObjRef);
One thing I am a little confused still is, when I add values to the variable this way:bless({@_[1..$#_]}, $_[0]);
Can I still access them by (I didn't test it yet, still trying to digest everything)?: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 ) );
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..print $phrases{'phrase 2'}->{'qtySum'}; print $phrases{'phrase 1'}->{'qtyCont'}; print $phrases{'phrase 3'}->{'qty'};
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.
I've used a key value, but I understand, that the current branch reference can also be used.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 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |