in reply to self-reporting perl code: how can it be done?
How can one write a perl script in such a way that the code can report on itself?
If you have a __DATA__ section at the end, you can seek on the DATA filehandle, and read the source of your current program. Note that without the __DATA__ section, it will not work. Here's a quick example:
#!/usr/bin/perl use strict; use warnings; my %chars; seek DATA, 0, 0; while (<DATA>) { chomp; $chars{$_}++ for split //; } for (sort keys %chars) { printf "%s => %2d\n", $_, $chars{$_}; } __DATA__
|
|---|