Perl Scope of Variables Problem

Problem with the scope of variables in my program.

Page 1 of 1

3 Replies - 5103 Views - Last Post: 03 March 2008 - 07:48 AM

#1 joshthejest  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Perl Scope of Variables Problem

Posted 01 March 2008 - 05:32 PM

I'm not done with this program yet, but I am getting the following errors. I'm still new to perl so I'm not really sure how to fix these problems. I have searched online and it seems there is a problem with the scope of the variables.

Use of uninitialized value in multiplication (*) at CM.pl line 45.
Use of uninitialized value in numeric eq (==) at CM.pl line 62.
Use of uninitialized value in numeric eq (==) at CM.pl line 62.
Use of uninitialized value in numeric eq (==) at CM.pl line 62.
Use of uninitialized value in string at CM.pl line 85.
Use of uninitialized value in string at CM.pl line 85.
Use of uninitialized value in string at CM.pl line 85.

Go here for code with lines: http://pastebin.com/m340892c3
#!/usr/bin/perl -w
#CM.pl

sub doMath;
sub checkDeath;
sub checkPossible;
sub checkDuplicate;
sub testCheck;
sub depthFirst;
sub breadthFirst;
sub popPrint;

my @Stack;
my @root = qw(0 0 1);
push @Stack, @root;
depthFirst(\@Stack, \@root);

sub doMath
{

	my ($node, $choice) = @_;
	print "$_" for @{$node};

	if($choice == '1')
	{
		$node[0] += $node[2];
	}
	elsif($choice == '2')
	{
		$node[1] += $node[2];
	}
	elsif($choice == '3')
	{
		$node[0] += $node[2]*2;
	}
	elsif($choice == '4')
	{
		$node[1] += $node[2]*2;
	}
	elsif($choice == '5')
	{
		$node[1] += $node[2];
		$node[0] += $node[2];
	}
	$node[2] *= -1;
}

sub checkDeath
{
	my ($node) = @_;
	my @DeathStates = qw(3 2 3 1 2 1 0 1 0 2 1 2);
	#   0 1
	# 0 3 2
	# 1 3 1
	# 2 2 1
	# 3 0 1
	# 4 0 2
	# 5 1 2
	my $size = (scalar @DeathStates)/2;
	for ($x = 0, $x < $size, $x++)
	{
		if($node[1] == pop @DeathStates && $node[0] == pop @DeathStates)
		{
			return 0;
		}
	}

	return 1;
}

sub checkPossible
{

	return 1;
}

sub checkDuplicate
{
	my ($Stack, $node) = @_;
	$size = (scalar @Stack)/3;
	@StackNode = (pop@Stack, pop@Stack, pop@Stack);
	@StackNode = reverse @StackNode;
	for (my $x = 0, $x < $size, $x++)
	{
		if ( "$node[0]" eq "$StackNode[0]"
				&& "$node[1]" eq "$StackNode[1]"
				&& "$node[2]" eq "$StackNode[2]")
		{
			return 0;
		}
	}

	return 1;
}

sub testCheck
{
	my ($Stack, $array) = @_;
   	checkPossible (@array);
	checkDeath (@array);
	checkDuplicate (\@Stack, \@array); 
}

sub depthFirst
{
	my ($Stack, $node) = @_;
	my $choice = 1;
	doMath(\@node, \$choice);
	$check = testCheck(@Stack, @node);
	if ($check == 1)
	{
		push @Stack, @node;
	}
	elsif ($check == 0)
	{
		foreach $spot (@node)
		{
			print $spot;
		}
		print ": Not Valid";
	}
	else
	{
		print "Some random problem has happened.";
	}

}

sub breadthFirst
{

}

sub shiftPrint
{
	my ($Stack) = @_;
	my @node = (shift @Stack, shift @Stack, shift @Stack);
	foreach $spot (@node)
	{
		print $spot;
	}
}

sub popPrint
{
	my ($Stack) = @_;
#	print @Stack;
#	print "\n";
	my @node = (pop @Stack, pop @Stack, pop @Stack);
	foreach $spot (@node)
	{
		print $spot;
	}
} 

This post has been edited by joshthejest: 01 March 2008 - 05:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Perl Scope of Variables Problem

#2 joshthejest  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Re: Perl Scope of Variables Problem

Posted 01 March 2008 - 07:39 PM

If I use strict I notice that I get errors for just about all variables.

Global symbol "@node" requires explicit package name
and many more like it.

Any Advice?
Was This Post Helpful? 0
  • +
  • -

#3 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl Scope of Variables Problem

Posted 02 March 2008 - 12:41 AM

Use strict and warnings in all your perl programs, and use diagnostics for now:

use strict;
use warnings;
use diagnostics;


Drop the -w switch on the shebang line, the warnings pragma is much better.

You don't need all those subroutines listed at the beginning of the script, not sure why you did that. You have a problem here:

sub depthFirst
{
	my ($Stack, $node) = @_;
	my $choice = 1;
	doMath(\@node, \$choice);


You are passing a reference, \@node, to the doMath() function, but @node has not been previously declared. Is that supposed to be $node? $node is a reference to the @root array. If you want to pass it to doMath() you can:

sub depthFirst
{
	my ($Stack, $node) = @_;
	my $choice = 1;
	doMath($node, \$choice);


Since it is already a reference you don't want to append it with a backslash, that would create a new reference to the existing reference.

Then in the next line of the above function you have:

	$check = testCheck(@Stack, @node);


$check is not declared with "my" which will cause an error when "stict" is used. BUt you are passing two arrays to the testCheck() but in testCheck you are recieving two scalars:()

sub testCheck
{
	my ($Stack, $array) = @_;
	   checkPossible (@array);
	checkDeath (@array);
	checkDuplicate (\@Stack, \@array);
}



You should have sent references to the arrays:

	$check = testCheck(\@Stack, \@node);


And the errors continue and compound in that same fashion as your script continues to run.

Write a smaller script for now until you are more familiar with how to pass data to and from functions. Make sure everything is working before adding more functions. The basic concept is that your functions should be "black boxes". They have no idea what happens outside thier little world (their scope). Some data comes in, it does something, and it passes data back out. Generic example:

my $foo = 2;
$foo = do_this($foo);
print $foo;
sub do_this {
	my ($foo) = @_;#import data
	$foo++;
	return $foo;#return data back to caller
}


Was This Post Helpful? 0
  • +
  • -

#4 joshthejest  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Re: Perl Scope of Variables Problem

Posted 03 March 2008 - 07:48 AM

Thank you KevinADC for explaining that to me. I think you are right. I need to write smaller scripts until I get the hang of how perl works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1