I want to modify Perl for 3 things.
- Preprocessor
- if,for,while without brackets
- Useful things like current ram or average load
For this three things I wrote this script :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
#!/usr/bin/perl use Getopt::Long; use Inline 'C'; #option vars my $o_print,$o_help,$o_file; #vars my $data,$search0,$search1,$search2,$replace; #get options GetOptions( "p" => $o_print, "h" => $o_help, "f" => $o_file ); #print help if($o_help){ print "Perl++ Interpreter V0.01".$/; print "-p print Perl++ converted to Perl".$/; print "-f print Perl++ file".$/; print "-h print help".$/; exit; } #print file if($o_file){print $ARGV[0].$/;exit;} #load file open (FILE, $ARGV[0]) or die $!; while(<FILE>){ $data = $data.$_; } close (FILE); #define special vars my $vars = q# °define @°free_ram°@ @`free | awk "NR == 2" | awk '{print $4}'`@ °define @°load_1°@ @`uptime | awk '{print $10}' | cut -d "," -f 1,2 | tr "," "."`@ °define @°load_5°@ @`uptime | awk '{print $11}' | cut -d "," -f 1,2 | tr "," "."`@ °define @°load_15°@ @`uptime | awk '{print $12}' | cut -d "," -f 1,2 | tr "," "."`@ #; #run preprocessor for user defined vars prep(); #add them to data $data =$vars.$data; #run preprocessor for default defined vars prep(); #prepare if|for|while without brackets while($data =~ /(.{2,})((.+))([ nr]{0,}[^;{]+;)/om){ $search0 = $1; $search1 = $2; $search2 = $3; $data = repl_str($data,"$search0($search1)$search2","$search0($search1){$search2}"); } #remove leading n $data =~ s/^n//o; #print or run if($o_print){ print $data.$/; }else{ eval $data; warn $@ if $@; } sub prep { while($data =~ /^°define (.)(.+)n/om){ $search0 = $1.$2; (undef,$search1,undef,$search2) = split(/$1/, $search0); $data = repl_str($data,"°define $search0n",""); $data = repl_str($data,$search1,$search2); } } __END__ __C__ char* repl_str(const char *str, const char *old, const char *new){ char *ret, *r; const char *p, *q; size_t oldlen = strlen(old); size_t count, retlen, newlen = strlen(new); if (oldlen != newlen) { for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) count++; retlen = p - str + strlen(p) + count * (newlen - oldlen); } else retlen = strlen(str); if ((ret = malloc(retlen + 1)) == NULL) return NULL; for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) { ptrdiff_t l = q - p; memcpy(r, p, l); r += l; memcpy(r, new, newlen); r += newlen; } strcpy(r, p); return ret; } |
if you want to test it you have to save it at “/usr/bin/perlp”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/usr/bin/perlp use strict; use warnings; °define '1' '5' °define "2" "6" °define /3/ /7/ °define '4' '8' print °free_ram°.$/; if(1) print "true".$/; for(1..10)print $_.$/; print "1".$/; print "2".$/; print "3".$/; print "4".$/; |
In line 4-7 I define some values for replace.
Line 10 you cant print the current free ram, for this the script replace the token via bash script Free Ram or Load Average .
Line 13 and 16 you see that you can use if without brackets.
Line 18-21 it prints the changed values.
I truly love your blog.. Pleasant colors & theme.
Did you make this amazing site yourself? Please reply back as I’m seeking
to create my own, personal personal blog and would like to know where you got this from or just what the theme is named.
Kudos!