CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Module Question - Resetting __DATA__

 



Donna
Deleted

Feb 13, 2000, 4:08 PM

Post #1 of 3 (2632 views)
Module Question - Resetting __DATA__ Can't Post

I'm writing (my first) module and have run into a snag. The purpose of the module is to retrieve remote information based on one field of user input. The location of the remote info is held in precompiled data (stored in __DATA__) within the module.

Everything works perfectly when I enter one value in the field.

I would now like to enter multiple values into the one field (delimited by spaces), toss those split values into an array, and iterate through the array, creating new objects for each value, validating input and retrieving the correct location for the remote data.

The problem is that the first value works, but the rest do not. After some troubleshooting, this is happening because the module has already iterated through <DATA> and is not starting at the beginning for the second value.

Here's the constructor, etc, within the program:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use constant CLASS => "Lookup";
use Lookup;

foreach(@value){ #this holds the split field values
$news = new Lookup($_);
(@errors) = $news->IsValid();
unless(@errors){
(@moreinfo) = $news->DoLookup();
}
}
</pre><HR></BLOCKQUOTE>

Here's the relevant portion of the module:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my ($URL,$QUERYDB,$NAME) = "";
while (<DATA> ){
chomp;
($URL,$QUERYDB,$NAME) = split(/\t/,$_);
if (lc($NAME) eq lc($self->{_NAME})){
$self->{_URL} = $URL;
$self->{_QUERYDB}= $QUERYDB;
last;
}
}
</pre><HR></BLOCKQUOTE>

In a nutshell, I guess I need to know how to force the module to start at the beginning of __DATA__ for the second and subsequent values. (Though I thought that each new constructor would do that?)

Any help would be appreciated.

Thanks!

[This message has been edited by Donna (edited 02-13-2000).]


japhy
Enthusiast / Moderator

Feb 13, 2000, 7:09 PM

Post #2 of 3 (2632 views)
Re: Module Question - Resetting __DATA__ [In reply to] Can't Post

The DATA filehandle is almost a filehandle like any other. When used without being assigned a file, it will read from the __END__ token (or the __DATA__ token -- whichever comes first) to the end of your program. And you can seek() on it, too. However, doing:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print <DATA>;
seek DATA, 0, 0;
print <DATA>;

__DATA__
abc
</pre><HR></BLOCKQUOTE>

will print more than "abc" twice. It will print:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


abc
print <DATA>;
seek DATA, 0, 0;
print <DATA>;

__DATA__
abc
</pre><HR></BLOCKQUOTE>

That's right, seeking to the beginning of the DATA filehandle takes you to the beginning of your Perl program's file. How do you get back to the beginning of where you WANT DATA to start?

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$start = tell DATA;
print <DATA>;
seek DATA, $start, 0;
print <DATA>;

__DATA__
abc
</pre><HR></BLOCKQUOTE>

That prints the expected results.


Donna
Deleted

Feb 14, 2000, 4:15 AM

Post #3 of 3 (2632 views)
Re: Module Question - Resetting __DATA__ [In reply to] Can't Post

That worked like a charm... thank you japhy!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives