We help IT Professionals succeed at work.

Perl printing variables and values

bt707
bt707 asked
on
I have a long list of variables which when I run the scripts in debug mode I want to print out the value of each varible.

$one = 1;
$two = 1;
$three = 0;
$four = 1;
$five = 0;

Right now I'm just printing them out a long way such as:

Print "\$one is: $one\n\$two is: $two\n\$three is: $three\n\$four is $four\n\$five is $five\n";

With a long list of variables and this is not very good for sure, I've tried to put the list of variables in a hash then print out the keys and values for them but not getting it to work, I know there is a simple way to do this but not getting it to work.

Can someone show me how to do this?

Thanks,
Comment
Watch Question

Basically, this should do it


my %num = ( 'one' => 1,
            'two' => 1,
            'three' => 0,
            'four' =>1,
            'five' => 0);

foreach $k (keys %num)
{
    print $k , ' is ' , $num{$k}, "\n";
}

Unfortunately, it may not preserve the order
To preserve the order, you could use a technique like


my @num = ('one', 'two', 'three', 'four', 'five');
my %num = ( 'one' => 1,
            'two' => 1,
            'three' => 0,
            'four' =>1,
            'five' => 0);

foreach $k (@num)
{
    print $k , ' is ' , $num{$k}, "\n";
}

Author

Commented:
farzani,

Thanks for the help, sorry I may of not may the question very clear.

In the script all of the variables is set to either 1, 0 or nothing depending on different checks that is done and what info is submitted to the script when run, so I do not know what the values are to be able to put them into a hash like that which is the problem.

That is why I was printing them the way I showed above, which shows me what the value is or if there is not one but wanted to do it in some sort of loop to shorten it other than the long line I have, above it just an example, I have a lot more in the list.

I hope that makes more sense.
Sorry, don't really understand what you are asking.

If you are wanting to dynamically set hash like

$num{$k} = $val;

Author

Commented:
Let me try to explain what I want to do.

I have some cgi web scripts, mostly all perl, depending on what info is given the checks will create variables such as:
$one = 1;
$two = 1;
$three = 0;
$four = 1;
$five = 0;

Depending on the checks in the script some of the variables will be set and some will not be set, so I'm doing a print out to see which ones was set and if set what the value is.

I can print out the info I need such as:
Print "\$one is: $one\n\$two is: $two\n\$three is: $three\n\$four is $four\n\$five is $five\n";

so may print out something like:
$one is: 1
$two is:
$three is:
$four is: 1
$five is:

I have a dozen or more variables so didn't want to use several lines like I have above to print them, was thinking I could put them all in a hash or array some way then just loop through them to print out the values for each one if a value does exists.

Thanks,




Nem SchlechtIT Supervisor
Top Expert 2009

Commented:
I think you're looking for something like the PadWalker module which can list out all the variables in the current scope.  Here's some example code:
use PadWalker qw/peek_my/;

# here are some variables that you've set
my $one = 1;
my $two = 1;
my $three = 0;
my $four = 1;
my $five = 0;
my $six = "";
my @foo = qw(here is some stuff in an array);
my %bar = qw(this is just a bunch of junk in a hash);

# call this sub anytime you want them dumped out
dump_all_vars();

sub dump_all_vars {
    my %here = %{peek_my(0)};
    foreach my $var (keys %here) {
	if (ref($here{$var}) eq "SCALAR") {
	    print "$var -> ", ${$here{$var}}, "\n";
	    next;
	}
	if (ref($here{$var}) eq "ARRAY") {
	    print "$var -> ", @{$here{$var}}, "\n";
	    next;
	}
	if (ref($here{$var}) eq "HASH") {
	    print "$var -> ", join(":", keys(%{$here{$var}})), "\n";
	    next;
	}
    }
}

Open in new window


Which produces:  
$two -> 1
$five -> 0
%bar -> a:just:junk:bunch:this
@foo -> hereissomestuffinanarray
$six -> 
$four -> 1
$three -> 0
$one -> 1

Open in new window


But note, since the variables are stored in a hash, they'll be in somewhat random order.  (You can always sort the keys on line 18).

You may have to read the PadWalker man page and modify line 17 to get the right scope.  If it were me, I would name any such variable I wanted with a unique string, and then I could just check the value of $var to see if it contained that string.  For example, instead of "$one", use "$one_dump", and then add in at line 19:
next unless ($var =~ /_dump$/);

Open in new window


So you won't get a ton of other values that you're not interested in.
IT Supervisor
Top Expert 2009
Commented:
Just saw your followup addition - PadWalker sounds like overkill for you (I was thinking tons or even hundreds of variables).  What you want is maybe this:
my(@vars)=qw(one two three four five six);
for (@vars) {
    print "$_ -> ", eval('$' . $_), "\n";
}

Open in new window

Author

Commented:
nemws1, I think that's pretty close to what I was asking or trying do, thanks to both for all the help, I just learned a few new things from both of you anyway, so will give a bit of credit to farzanj as it was probably that I was not very clear on my question.

Thanks to all
Nem SchlechtIT Supervisor
Top Expert 2009

Commented:
farzanj deserves it - happy you gave him some points as well. ;-)
Thank you for the points, bt707.  Sorry, I got busy and I am glad that nemws1 took over and solved your problem.

nemws1:- Thanks for your kind words.  Appreciate your gesture.