Help - Search - Member List - Calendar
Full Version: Access __END__ <DATA> from module/package
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
JupiterHost.Net
Howdy all,

In a script I can access __END__ via <DATA>

I'd like to access __END__ form within a module (IE package)

Foo.pm

package Foo;

use strict;
use warnings;

sub dofoo {
print <DATA>;
}

1;

__END__
hello
world

in script:

#!/usr/bin/perl

use strict;
use warnings;
use Foo;

Foo::dofoo();

should output:

hello
world

Anyone knwo how to do that/if its even possible?

TIA

Jeff 'japhy' Pinyan
On Jul 13, JupiterHost.Net said:

QUOTE
In a script I can access __END__ via <DATA

I'd like to access __END__ form within a module (IE package)

Reading 'perldoc perldata' yields this:

Text after __DATA__ but may be read via the filehandle "PACK-
NAME::DATA", where "PACKNAME" is the package that was current when the
__DATA__ token was encountered. The filehandle is left open pointing
to the contents after __DATA__. It is the program's responsibility to
"close DATA" when it is done reading from it. For compatibility with
older scripts written before __DATA__ was introduced, __END__ behaves
like __DATA__ in the toplevel script (but not in files loaded with
"require" or "do") and leaves the remaining contents of the file acces-
sible via "main::DATA".

Thus, use __DATA__ in your modules, instead of __END__, and then you can
access the contents thereof by reading from <PackageName::DATA>.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

JupiterHost.Net
Jeff 'japhy' Pinyan wrote:

QUOTE
On Jul 13, JupiterHost.Net said:

In a script I can access __END__ via <DATA

I'd like to access __END__ form within a module (IE package)


Reading 'perldoc perldata' yields this:

Text after __DATA__ but may be read via the filehandle "PACK-
NAME::DATA", where "PACKNAME" is the package that was current when the
__DATA__ token was encountered.  The filehandle is left open pointing
to the contents after __DATA__.  It is the program's responsibility to
"close DATA" when it is done reading from it.  For compatibility with
older scripts written before __DATA__ was introduced, __END__ behaves
like __DATA__ in the toplevel script (but not in files loaded with
"require" or "do") and leaves the remaining contents of the file acces-
sible via "main::DATA".

Thus, use __DATA__ in your modules, instead of __END__, and then you can
access the contents thereof by reading from <PackageName::DATA>.


Right on Jeff, thanks for pointing that out :)

Should of looked at the docs better :)


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.