Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Treating sub output as a hash

by ISAI student (Scribe)
on Jan 26, 2012 at 16:57 UTC ( #950166=perlquestion: print w/ replies, xml ) Need Help??
ISAI student has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. This question pertains to PERL5.* I know that I can use a function output as an array easily, w/o needing temporary arrays I.E.:

foreach $line ($self->getAllLines() {... }

I want to be able to do the same with a sub that return hashes, but i can't get it to work. See tiny example below using perl -e:

liord@analog4 267 > perl -e 'sub a { %a = ( 1 , "ho" ); return %a} pri +nt keys a(), "\n"; ' Type of arg 1 to keys must be hash (not subroutine entry) at -e line 1 +, near ")," Execution of -e aborted due to compilation errors.

However, using a variable to get the hash and then using keys on the variable, works:

liord@analog4 268 > perl -e 'sub a { %a = ( 1 , "ho" ); return %a} %t= + a() ; print keys %t , "\n"; ' 1

Is there a way to avoid defining %t, and make the code more compact?

Comment on Treating sub output as a hash
Select or Download Code
Re: Treating sub output as a hash
by moritz (Cardinal) on Jan 26, 2012 at 17:05 UTC
Re: Treating sub output as a hash
by chromatic (Archbishop) on Jan 26, 2012 at 17:20 UTC

    The problem is that return %hash doesn't return a hash. It returns a list. Only when you assign that list to something hashy (a hash or an anonymous hash) can you treat it as a hash.


    Improve your skills with Modern Perl: the free book.

Re: Treating sub output as a hash
by AnomalousMonk (Monsignor) on Jan 26, 2012 at 17:26 UTC

    With 5.14, one can use keys, values and each directly on an unblessed hash (or array) reference or an expression that evaluates as such (although this is flagged as "highly experimental"). I don't have 5.14 and so cannot provide a working example, but I think it should go something like this:

    for my $k (keys function_returning_hash_ref()) { print "the hash has a key $k \n"; } sub function_returning_hash_ref { return { fee => 'Fie', foe => 'Fum', }; }

    Update: Example tested OK with 5.14 per chromatic (with thanks!).

Re: Treating sub output as a hash
by JavaFan (Canon) on Jan 26, 2012 at 22:33 UTC
    Is there a way to avoid defining %t, and make the code more compact?
    print keys %{{a}};
    a() returns a list. The inner {} make a hashref, and the outer %{} dereferences this hashref.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950166]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-08-20 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls