Perl Weekly Challenge #45 Task #2, Source Dumper

Perl Weekly Challenge #45 Task #2: Source Dumper

The Source Dumper dumps the source code of itself. For my Blog of the PWC I use syntax highlighting. The perl program I use for the syntax highlighting I included in the Source Dumper of my Perl5 solution. This is a simple add-on with Text::VimColor.

Some highlights:

Download and References

Download File: Perl5 Solution PWC #45 Task #2 ch-2.pl
Download File: Perl6 Solution PWC #45 Task #2 ch-2.p6
Download File: Python Solution PWC #45 Task #2 ch-2.py

Here I found the $*PROGRAM variable for Perl6:
docs.perl6.org: Variables

SYNOPSIS

 # ./ch-2.pl            - Execution of program
 # ./ch-2.pl help       - Help on parameters
 # ./ch-2.p6            - Execution of program
 # ./ch-2.py            - Execution of program

 # ./ch-2.pl | diff - ch-2.pl   - Should result in no difference
 # ./ch-2.p6 | diff - ch-2.p6
 # ./ch-2.py | diff - ch-2.py

 # perldoc ch-2.pod             - POD

Definition Task #2: Source Dumper

Write a script that dumps its own source code. For example, say, the script name is ch-2.pl then the following command should returns nothing.

 $ perl ch-2.pl | diff - ch-2.pl

Perl5

The printing of the Source Code is done with open(IN,$0) the file, a while loop while(IN) { print; } together with print and to close the file. Here the program name is in var $0.

The biggest part of the code is not needed for the PWC. It is a Source Dumper in HTML with Syntax Highlighting. This is done with the module Text::VimColor rather easy. You can print a full HTML page or only the source code part of the HTML. Some extra code is spent for the line numbers. Allthough Text::VimColor has an option for line numbers, it did not work. So I added the line numbers for myself.

Source Code

This is the code part needed for PWC, like described above:

 35     open(IN,$0) or die "Cant open $0\n";
 36     while(<IN>) { print; }
 37     close IN;

Some Highlights are:

Perl5
1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 use Text::VimColor; 6 7 if($ARGV[0] and $ARGV[0] eq "help") { 8 print "\n$0 - PWC #45 Task #2: Source Dumper\n"; 9 print "\n"; 10 print " This source code dumper dumps its own source code, but also the \n"; 11 print " source code of other files with syntax highlighting using Text::VimColor.\n"; 12 print "\n"; 13 print " ./ch-2.pl [<command|file>] [<full>]\n"; 14 print " command - help|high\n"; 15 print " file - filename of file to dump.\n"; 16 print " full - dump full html page not only source code.\n"; 17 print "\n"; 18 print " Examples:\n"; 19 print " ./ch-2.pl - Dumps own source code\n"; 20 print " ./ch-2.pl help - This Usage information\n"; 21 print " ./ch-2.pl high - Syntax high for ch-2.pl\n"; 22 print " ./ch-2.pl ch-1.pl - Syntax high for ch-1.pl\n"; 23 print " ./ch-2.pl high 1 - Syntax high for ch-2.pl in full html page.\n"; 24 print " ./ch-2.pl ch-1.pl 1 - Syntax high for ch-1.pl in full html page.\n"; 25 print " ./ch-2.pl | diff - ch-2.pl - PWC 45 Task #2\n"; 26 print "\n"; 27 } 28 elsif($ARGV[0] and $ARGV[0] eq "high") { 29 syntax_high($0,$ARGV[1]); 30 } 31 elsif($ARGV[0]) { 32 syntax_high(@ARGV); 33 } 34 else { 35 open(IN,$0) or die "Cant open $0\n"; 36 while(<IN>) { print; } 37 close IN; 38 } 39 40 sub syntax_high { 41 my ($file,$full) = @_; 42 43 # Option $full is for Debug Reasons, a Full HTML Page is created. 44 if(! $full) { $full = 0; } 45 46 my $syntax = Text::VimColor->new( 47 file => $file, 48 filetype => 'perl', 49 html_full_page => $full, 50 # Extra vim options do not work: I want to have the line number 51 # in the source code and also to try different colorschemes for vim. 52 # extra_vim_options => [ "+set number", "+color darkblue" ], 53 ); 54 55 my $html = $syntax->html; 56 57 my ($html1,$html2); # Full Page needs to be splitted, to get line numbers on each line. 58 if($full) { 59 my $tmp; 60 ($html1,$tmp) = split("<pre>",$html); 61 ($html,$html2) = split("</pre>",$tmp); 62 } 63 64 my @lnes = split("\n",$html); # Every line for Line Number 65 66 if($full) { print $html1, "<pre>\n"; } # Print first part of full page 67 my $cnt = 1; # Count line number 68 foreach my $line (@lnes) { # Every line for Line Number 69 printf("%3d %s\n",$cnt,$line); # Print Line with Line Number 70 $cnt++; 71 } 72 if($full) { print "</pre>\n", $html2; } # Print last part of full page 73 }

Perl6

I translated the necessary part for PWC to Perl6. And let me mention some parts different to Perl5 here. May-be things could be solved easier? But for me as a beginner in Perl6, this is what I found first.

Perl6
1 #!/home/chuck/rakudo/bin/perl6 2 3 use strict; 4 5 # print "ch-2.p6 - PWC #45 Task #2: Source Dumper\n"; 6 # print $*PROGRAM, ", ", $*PROGRAM-NAME, "\n"; 7 8 my $fh = open :r, $*PROGRAM; 9 10 my $str; 11 while ( ! $fh.eof; ) { 12 $str = $fh.get; 13 $str.print; print "\n"; 14 } 15 $fh.close;

Python

Because the code is not too complicated, let me mention some points. Put in mind that I am a beginner in Python, so any other Python programmer may wonder:

Python
1 #!/usr/bin/python 2 3 # print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; 4 # print "File:", __file__, "\n"; 5 6 fh = open(__file__); 7 for line in fh: 8 print(line), 9 10 fh.close

AUTHOR

Chuck

 Perl Weekly Challenge #45 Task #2, Source Dumper