Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: hash from sub directly into foreach loop

by davidj (Priest)
on Jun 30, 2006 at 08:17 UTC ( [id://558529]=note: print w/replies, xml ) Need Help??


in reply to hash from sub directly into foreach loop

First of all, the syntax in the sub is incorrect:
%s{'a'} = 10 ; %s{'b'} = 20 ; %s{'c'} = 30 ;
should be
$s{'a'} = 10 ; $s{'b'} = 20 ; $s{'c'} = 30 ;
anyway, the following change will work for you:
#!/usr/bin/perl use strict ; use warnings ; foreach ( keys %{&testa} ) { print "key is $_\n" ; } sub testa { my $s = () ; $s->{'a'} = 10 ; $s->{'b'} = 20 ; $s->{'c'} = 30 ; return $s ; }
hope this helps,
davidj

Replies are listed 'Best First'.
Re^2: hash from sub directly into foreach loop
by GrandFather (Saint) on Jun 30, 2006 at 11:30 UTC

    Note that:

    my $s = ();

    is equivelent to

    my $s = (undef);

    which is equivelent to:

    my $s;

    The first use of a hash ref autovivifies the referred hash making the previous assignment redundant (and misleading). Consider:

    use strict; use warnings; use Data::Dump::Streamer; my $s = (); Dump ($s); $s->{'a'} = 10 ; Dump ($s);

    which prints:

    $VAR1 = undef; $HASH1 = { a => 10 };

    and note that the contents of $s changes from an undefined value to a hash ref.

    If you really want to indicate intent with an assignment use:

    my $s = {};

    DWIM is Perl's answer to Gödel
Re^2: hash from sub directly into foreach loop
by jeanluca (Deacon) on Jun 30, 2006 at 08:23 UTC
    nope, the sub should return a hash and not a hash reference. It should be the same as with
    foreach( keys CGI::Vars() )
    because that is what I'm trying to do!

    LuCa

      You can't "return a hash" from a subroutine, you can only return a list, the effect of return %hash is to flatten the hash into a list of key => value pairs. Thus the effect of the the three following subroutines is similar (the order of the hash keys in the first aside:)

      sub foo { my %foo = ( Foo => '1', Bar => '2'); return %foo; } + sub bar { my @bar = qw(Foo 1 Bar 2); return @bar; } + sub baz { return 'Foo','1','Bar','2'; } + print foo(),"\n"; print bar(),"\n"; print baz(),"\n";

      This is why people are telling you to return a hash reference as this is the only way to retain the, er, hashiness after the appropriate dereferencing.

      /J\

      Please show working code which does what you're trying to do. When I try the following, it doesn't work:

      Q:\>perl -MCGI -le "foreach( keys CGI::Vars() ) { print }" 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.

      When I try the following, which the documentation and davidj suggest, it works (in the sense of not being a syntax error). But that works by returning a hash reference:

      Q:\>perl -MCGI -le "foreach( keys %{ CGI::Vars() }) { print }" Q:\>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://558529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found