LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2007, 04:40 PM   #1
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
Perl eternal loop problem


Hi there

I am beginner in Perl and I must say, it's unlike anything I've seen before. Things one almost takes for granted in PHP seem very hard to do in Perl.

Basically, I want to provide an abstraction to DBI so that it uses the same method names as the abstraction we have in PHP (makes it easier later)

The problem lies in the fact that I want to fetch the rows of the result set, and put these in an array of arrays (records)

The code executes until the end of time for some reason:

Code:
sub getFullArray() {

	my ($self)=@_;
	
	my @out=undef;
	
	my @read=undef;
	
	while(1) {
	
		if(@read=$self->getArray()) {
	
			push @out, [ @read ];
			
		} else {
		
			last;
		}
	}
	
	return @out;
}

sub getArray() {

	my ($self)=@_;
	
	my @out=undef;
	
	if($self->{'lastresult'}) {
	
		if(!(my (@out)=$self->{'lastresult'}->fetchrow_array())) {
		
			#end of result set
			
			$self->{'lastresult'}->finish();
			
			$self->{'lastresult'}=undef;
		}
	}
	
	return @out;
}
Can you see what the problem is?

Thanks
 
Old 07-31-2007, 01:44 AM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
It's been a while since I've done anything in Perl, but instead of using "last", why not use "break"?
 
Old 07-31-2007, 02:41 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
last is correct

Code:
	(my @out=undef;
	
	if($self->{'lastresult'}) {
	
		if(! (my (@out)=$self->{'lastresult'}->fetchrow_array())) {
		
			#end of result set
			
			$self->{'lastresult'}->finish();
in getArray
you are declaring @out with my twice, once inside the loop which, as it's a lexical means it's valid only in the loop.


a few observations... (style wise)
also, usually we do
my $self = shift;


and

unless (blah) rather than if(!blah)

also maybe?

Code:
while (@read=$self->getArray()) {
	push @out, [ @read ];
}

Last edited by bigearsbilly; 07-31-2007 at 02:44 AM.
 
Old 07-31-2007, 12:10 PM   #4
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Original Poster
Rep: Reputation: 38
Thanks for the insight. Suppose it's artifacts of my PHP past. Trying to grasp perl might be easier for non-coders. . .
 
Old 08-01-2007, 02:12 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
perl is difficult to grasp anyway because it's not quite like anything else, the concept of vars and functions having different contexts
is quite weird and I think the basis of getting perl.

I admit that I loathed it with a passion when I first encountered it, now
I'm really quite fond of it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
eternal hdd problem tripled2006 Ubuntu 1 01-15-2007 03:07 PM
me and my eternal problem with ati setlec_seta Mandriva 3 12-16-2006 06:39 AM
perl loop jazman Programming 3 07-05-2006 10:31 AM
eternal resolving host problem mjsabby Linux - Networking 4 03-31-2004 05:44 PM
eternal resolving host problem mozilla mjsabby Linux - General 4 03-30-2004 09:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration