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?


In reply to problems with reading __DATA__ from current package by perlguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.