Zaxo has asked for the wisdom of the Perl Monks concerning the following question:

I'm tinkering with a script which writes a fixed wrapper around perl source which is fed to it through STDIN. The wrapper code needs data which is currently stored after an __END__ tag. A problem arises if the input source uses its own DATA handle.

Following some hints in Camel3, I've tried declaring package Wrapped; before the input code to qualify the __DATA__ tag. This fail with the main::DATA handle reading from the guest code's __DATA__ tag to the end of the file. Schematically, the resulting code is structured like this:

#!/usr/bin/perl -w use strict; =pod Output: Name "Extra::DATA" used only once: possible typo at ./camtst line 23. From main Extra datum __END__ This data is for main Read on closed filehandle <DATA> at ./camtst line 23. From Extra =cut print "From main ", <DATA>; package Extra; print "From Extra", <DATA>; package main; __DATA__ Extra datum __END__ This data is for main
Is it possible to use two DATA tags in the same file? I don't see a way to change namespace after the first is seen.

I'd appreciate any explanations or links about fine control of the DATA filehandle. I can obviously store namespace main's data in a heredoc or some other construct, so my question is entirely in "Search of Perl Wisdom". Thanks.

After Compline,
Zaxo

Edit Petruchio Thu Sep 20 09:11:38 UTC 2001 - Added READMORE tag.

Replies are listed 'Best First'.
Re: Scope of the DATA handle
by danger (Priest) on Sep 20, 2001 at 12:24 UTC

    If you really want multiple 'virtual' files (each with their own __TOKEN__), see Damian's Inline::Files package on CPAN.

(tye)Re: Scope of the DATA handle
by tye (Sage) on Sep 20, 2001 at 19:26 UTC

    A good way to do what you want has already been suggested.

    No, Perl stops reading when it sees __END__ or __DATA__ and so anything you put after that is completely ignored by Perl. It certainly doesn't expect to find Perl code after those lines and so won't parse "package" nor "__DATA__" nor any other Perl keywords below that, otherwise people would be getting syntax errors when they had placed data that isn't Perl code there.

            - tye (but my friends call me "Tye")