From: Steve Bertrand <st...@ibctech.ca>
> Hi all,
> 
> I know this is a no-brainer, but I'm drawing a blank after coding all
> day (I'm not a coder by trade).
> 
> I'm trying to write a test program for a function I'm accessing from a
> module I wrote years ago, and because I'm over-tired, I can't remember
> how to (or find how to) iterate over the items within an object.
> 
> This is literally my test program. The $user object contains a couple
> dozen vars. One of them is shown in a print statement with the expected
> output as a comment.
> 
> The following (and numerous variants of it) do not work. How do I
> properly iterate through all of the items in '$user'?:
> 
> ---- code below ----
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> use EagleUser;
> 
> my $user = EagleUser->new();
> 
> $user->build_inf_user('steveb');
> 
> print "$user->{'login_name'}\n"; # outputs 'steveb' as expected
> 
> while ( my ($key, $value) = each($user) ) {

 while ( my ($key, $value) = each(%$user) ) {

>         print "$key => $value\n";
> }
> 
> ---- end code ----
> 
> Cheers,
> 
> Steve

It doesn't matter whether it's an object. It's a hash reference so 
you can treat it as such. And since each() expects a hash, not a hash 
reference, you have to dereference. That is put a % in front of the 
$user.

HTH, Jenda
===== je...@krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to