Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

problems with reading __DATA__ from current package

by perlguy (Deacon)
on Jul 16, 2003 at 16:16 UTC ( #274898=perlquestion: print w/ replies, xml ) Need Help??
perlguy has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

Having used __DATA__ at the bottom of scripts for a while, I'm a bit frustrated. Instead of reading the <DATA> filehandle in the main:: package, I am wanting to read it in the current package (in this case package States, but I'm struggling with having it read even a single line of __DATA__.

While searching for information on this topic, I often came across code snipplets that read from this special filehandle in other packages. I'm not planning on changing the data anytime soon, so I'd rather just read it from the bottom of my object declaration.

Here is what I have so far:

######################## FILE 1 ####################### use strict; use warnings; use States; my $s = States->new(); ######################## FILE 2 ####################### package States; use CGI qw(:standard); use strict; use warnings; use Switch; use diagnostics; sub new { my ($class) = @_; print "Hello\n"; my @states; while (<DATA>) { chomp; print "$_\n"; # only for debugging purposes # prints nothing currently my ($code, $abbr, $proper, $state) = split /\t+/; push @states, { CODE => $code, ABBR => $abbr, PROPER => $proper, STATE => $state, } } return bless \@states; } 1; __DATA__ 01 AL 1 Alabama 50 AK 1 Alaska 02 AZ 1 Arizona 03 AR 1 Arkansas 04 CA 1 California 05 CO 1 Colorado 06 CT 1 Connecticut 07 DE 1 Delaware

I'm using v5.8.0 on Solaris 9. The output is the following (printed from the constructor, only for debugging reasons):

Hello

Any thoughts?

Comment on problems with reading __DATA__ from current package
Select or Download Code
Re: problems with reading __DATA__ from current package
by Aristotle (Chancellor) on Jul 16, 2003 at 16:25 UTC
    Remove the use Switch; and have fun.

    Makeshifts last the longest.

      Hmmm... I stripped down my module for better Perlmonks readability, and I had a use Switch switch inside of my code. I had no idea it would change how __DATA__ special filehandle works. Can you explain?

        I don't know really, I happened upon it by accident myself.

        Makeshifts last the longest.

Re: problems with reading __DATA__ from current package
by dws (Chancellor) on Jul 16, 2003 at 17:09 UTC
    Problems with Switch aside, your class will only support a single instance. On a subsequent attempt to create a new instance, DATA will be at eof. Since the data is static, you might cache it instead in a package variable, and use the Singleton pattern to return a single instance of States.

    I'm guessing that you'll only be using a single instance, but someone who picks up your code later might not we aware of the implicit limitation, and will be surprised when their n+1st States object is empty.

      Good tip. I will do so. I was so hung up on getting the first part to work, that... Well, you know.

      Thanks a bunch for the tips, everyone.

Re: problems with reading __DATA__ from current package
by IlyaM (Parson) on Jul 16, 2003 at 18:13 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://274898]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-20 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls