Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Is it possible to modify __DATA__ via the DATA file handle or otherwise?

by clueless newbie (Chaplain)
on Feb 09, 2018 at 19:30 UTC ( #1208858=note: print w/replies, xml ) Need Help??


in reply to Is it possible to modify __DATA__ via the DATA file handle or otherwise?

Will this do it for you?
#!/usr/bin/env perl # Just because you can be a SOB doesn't mean you should # or among the things you probably shouldn't do is this: # modify (read/write) the program's DATA section - # but hey, it was interesting and amusing. use Carp; use English qw(-no_match_vars); use strict; use warnings; open my $READER,'+<',$PROGRAM_NAME or Carp::confess "Can't open '$PROGRAM_NAME'! $OS_ERROR"; open my $WRITER,'+<',$PROGRAM_NAME # Don't us +e '+>' or Carp::confess "Can't open '$PROGRAM_NAME'! $OS_ERROR"; seek $READER,tell DATA ,0; seek $WRITER,tell DATA,0; # The overflow buffer: my @Buffer_a; #print "READER: @{[tell $READER ]} \n"; while (<$READER>) { #print "Read: '$_'\n"; # Read (past tense) a line - buffer its replacement ... { # Modify the line here! my $Update_s="x s $_"; push @Buffer_a,$Update_s; }; #print "WRITER: @{[tell $WRITER ]} \n"; # Write from the overflow buffer if we can ... while (@Buffer_a && tell($READER)-tell($WRITER) > length $Buff +er_a[0]) { # Enough room to write $Buffer_a[0] so write it ... #print "Writing '$Buffer_a[0]'\n"; print {$WRITER} shift @Buffer_a or Carp::confess "Error while writing \$WRITER: $OS_ER +ROR"; #print "WRITER: @{[tell $WRITER]} \n"; }; #print "READER: @{[tell $READER]} \n"; }; # Nothing more to read ... close $READER or Carp::confess "Can't close '$PROGRAM_NAME'! $OS_ERROR"; #print "WRITER: @{[tell $WRITER]} \n"; # If there's anything in the buffer write it ... while (@Buffer_a) { #print "Writing '$Buffer_a[0]'\n"; print {$WRITER} shift @Buffer_a or Carp::confess "Error while writing \$WRITER: $OS_ERROR" +; #print "WRITER: @{[tell $WRITER]} \n"; }; # Truncate the file, in case, what we're writing is shorter than w +hat we read truncate $WRITER,tell$WRITER; # ... and close close $WRITER or Carp::confess "Can't close '$PROGRAM_NAME'! $OS_ERROR"; exit; __END__ 0 1 2 3 4 5 6 7 8 9
  • Comment on Re: Is it possible to modify __DATA__ via the DATA file handle or otherwise?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208858]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2018-06-25 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (127 votes). Check out past polls.

    Notices?