How do I do this? read __DATA__ then seek to 0 then write over it?
Yes, just open up the current script and write to it like you would any other file e.g
use Fatal qw/ open seek /;
open(my $d => '+<', $0);
seek $d => 0, 2;
print $d @ARGV;
__DATA__
foo
bar
This will append @ARGV to the __DATA__ section of the currently executing script. Of course modify the seek as you see fit and if you're going to completely overwrite it then remember it's position with a quick tell.
HTH
_________ broquaint | [reply] [d/l] |
Oh come on.
Please read between the lines. The poster obviously doesn't know how DATA really works, and it's not how to do file IO he's asking about. He wants to learn about DATA and probably use it in his script and try it out. I think it's good to strive for greater understanding. If the problem is that you don't think he should be doing that by himself then just explain why, and then give him the alternative safe solution.
Inline::Files is good and all, but it can also be total overkill. I like Inline::Files when I want to have several DATA filehandles, but when you just want one the only thing that differes DATA from any other filehandle is that it's preopened and that it's sought to some non-BOF location.
Inline::Files plus Inline::Files::Virtual is about 500 lines of Perl, using tieing and AUTOLOADing, fiddles with the CORE::GLOBAL namespace, and utilizes the Filter module--which is XS.
I just wrote a program that checks the difference between a perl -e0 and perl -MModule -e0 on ActivePerl 5.8.0 build 806. The numbers in parenthesis in the approximate line count; POD, __END__/__DATA__, comments, and blank lines stripped (all pretty imperfectly done). The last number is the sum of the above. Empty (with my definition of empty) packages are filtered out.
Here's the output for Inline::Files:
New packages:
AutoLoader
Carp
Config
Cwd
DB
Data::Dumper
Dos
EPOC
Exporter
Filter::Util::Call
Inline::Files
Inline::Files::Data
Inline::Files::Virtual
Mac::FileSpec::Unixish
VMS::Filespec
XSLoader
base
fields
strict
vars
warnings
warnings::register
New files:
( 117) AutoLoader.pm
( 41) Carp.pm
(1122) Config.pm
( 325) Cwd.pm
( 125) DynaLoader.pm
( 59) Exporter.pm
( 33) Filter/Util/Call.pm
( 139) Inline/Files.pm
( 351) Inline/Files/Virtual.pm
( 55) XSLoader.pm
( 40) base.pm
( 21) strict.pm
( 39) vars.pm
( 316) warnings.pm
( 26) warnings/register.pm
(2809)
Now compare this to
my $BOF = tell DATA;
open my $data, '+<', $0 or die ...;
seek $data, $BOF, 0 or die ...;
and pretty much all you need to remember is that BOF isn't 0 but $BOF.
I'm surprised to see you give this advice, since I've seen you elsewhere advocate to not invoke modules when they mean "unnecessary" overhead: "the code I posted elsewhere in this thread copies the method of the module, without even having to invoke the overhead of the module". In fact, I even disagree with you there, and think that constant.pm should be utilized there. :-) Funny, heh.
ihb | [reply] [d/l] [select] |
Thanks Randal. It's an extra module though, and i want to deploy it on some desktops at work without adding modules if possible. Is it at all possible with __DATA__ ?
| [reply] |
| [reply] |
Puuuuhhhlease. Not the "it's a module" argument again. It's pure Perl... just copy it into your distro. Or use PAR. Or look at the source code and cannibalize the parts you need.
But pllllllleeeease let's stop with the "I can't use this because it's a module instead of some idea about code" whining.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
| [reply] |
It is always bad practice to state implementation details in your specification. | [reply] |