[Israel.pm] Direct access to variables from sub

Gaal Yahas gaal at forum2.org
Sun Oct 2 05:53:45 PDT 2005

On Sun, Oct 02, 2005 at 02:07:33PM +0200, david istermann wrote:
> I have a subroutine which returns a hash (a real hash, not a
> reference). I'd like to directly access an entry inside this hash
> bypassing variable assignment, however I cannot seem to successfully
> tell Perl my intention... Please help!

Subs can only return scalars or lists: what yours does is return a
*flattened* hash, which cannot be directly subscripted.

You can, however, construct an anonymous hash and indirectly subscript

       $value = +{ your_func() }->{$key};

Although this may turn out not to be a great idea if the guy who maintains
your code finds out where you live.

Gaal Yahas <gaal at forum2.org>

More information about the Perl mailing list