in reply to Re^2: How to detect a hash container key from a referenced sub value
in thread How to detect a hash container key from a referenced sub value
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.
No, not quite. See "Method Invocation" in perlobj. For class methods, the first argument for a method is the class (just its name); for objects, it is the object. Not a reference - just like that. For class methods, the class name is a literal, and the object is a reference already. The arrow operator "->" makes the passing of the first parameter implicit, so the next two are equivalent:
$obj = MyClass->new( %params); $obj = MyClass::new( 'MyClass', %params);
Can I still access them by (I didn't test it yet, still trying to digest everything)?:
Yes, since an object is just a reference blessed into a Namespace. Otherwise, it behaves just like any reference. Again, read perlobj and related material.
The use of the ampersand "&" as a sigil to suroutine calls makes passing @_ implicit, but only if the call has an empty parameter list. Consider:qtyTotal=>sub{return &qtyTotal('phrase 1', @_);}I realize, that this is an inefficient solution, but this is the solution that allows using pseudo methods without an actual objects.
$\ = $/; # see perlvar sub b { print join" ",@_ } sub e { &b } # @_ passed implicitly sub f { &b( "got") } # parens necessary! sub g { b "got", @_ } # parens may be omitted, if sub b # has been seen earlier @l = (1...3); e @l; f @l; g @l; __END__ 1 2 3 got got 1 2 3
So, use "&" only if you mean to pass @_ (and only @_) implicitly.
And it's not ineffective, I'd say, but neither is it "pseudo-methods" - that is calling anonymous subroutines. A pseudo method (call) would be something like the following:
sub b { print join" ", @_ } "b"->(1..3)
Further reading: perlboot, perlref, perlreftut, perltoot, ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to detect a hash container key from a referenced sub value
by igoryonya (Pilgrim) on Dec 06, 2009 at 02:51 UTC | |
by shmem (Chancellor) on Dec 07, 2009 at 09:55 UTC |