in reply to how to write a subroutine content into a file

it might be funny but i thought Pal-Cod3r have diff requirement.

sub doit{ print "How are you?\n"; $x=1+1; print "$x\n"; } open(FILE,"<monkss.pl"); @lines=<FILE>; close(FILE); open (FILE, ">files.txt"); $flag=0; foreach $line(@lines) { if($line =~ /doit\{/) { $flag=1; } if($flag) { print FILE $line; } if($line=~ /\}/) { $flag=0; } } close(FILE);

contents of file.txt
sub doit{ print "How are you?\n"; $x=1+1; print "$x\n"; }

Replies are listed 'Best First'.
Re^2: how to write a subroutine content into a file
by choroba (Cardinal) on Nov 08, 2011 at 10:55 UTC
    Or, similarly:
    #!/usr/bin/perl use warnings; use strict; sub doit { print "How are you?\n"; } seek DATA, 0, 0; while (<DATA>) { print if /^sub doit {/ .. /^}/ } __DATA__