Computer tools >
perl
perl
Summary
Lauch perl file in command line:
perl Hello_World.pl
Lauch perl script in command line:
perl -pe '{print "Hello World\n";}'
Hello_World.pl
#!/bin/perl
#use strict;
#use warnings;
print "Hello World\n";
Suggested way: wrap perl script into a bash script. Example:
clean_perl_equalities
merge_pdf.pl
#!/bin/perl
#use strict;
#use warnings;
# Usage: perl merge_pdf.pl file1 file2 > file-merged.pdf
# Remove white spaces in filenames before use!
# Not always stable: check the merged pdf!
sub max {
($a, $b) = @_;
if ($a>$b) {return $a}
else {return $b}
}
my @lines1, @lines2;
open($fh1, $ARGV[0]) or die "1er fichier inexistant";
while (<$fh1>) {
if (/\/Count (\w)/) {
$n_Count1 = $1;
}
if (/\/Parent ([0-9]*) 0 R/) {
$n_Parent1 = $1;
}
if ($_ =~ "trailer"){
$test_trailer1=1
}
if ($test_trailer1 == 1){
push @lines_end1, $_;
} else
{
push @lines1, $_;
}
}
my $delta_n_xref = 500;
my $Kids2;
open($fh2, $ARGV[1]) or warn "2e fichier inexistant";
while (<$fh2>) {
if (/\/Count (\w)/) {
$n_Count2 = $1;
}
if (/([0-9]*) 0 obj/) {
$n_ref = $1 + $delta_n_xref;
s/([0-9]*) 0 obj/$1+$delta_n_xref . " 0 obj"/ge;
}
if (/!?(\/Parent )?([0-9]*) 0 R/) {
$n_ref = $1 + $delta_n_xref;
s/([0-9]*) 0 R/$1+$delta_n_xref . " 0 R"/ge;
}
if (/\/Parent ([0-9]*) 0 R/) {
s/\/Parent $1/\/Parent $n_Parent1/;
}
if (/\/Kids\[ (.*)\]/) {
$Kids2 = $1;
}
if ($_ =~ "trailer"){
$test_trailer2=1;
}
if ($test_trailer2 != 1){
push @lines2, $_;
}
}
for (@lines1) {
if (/\/Count (\w)/) {
my $n_Count = $n_Count1+$n_Count2;
s/$1/$n_Count/;
}
if (/\/Kids\[ (.*)\]/) {
s/($1)/$1 $Kids2/;
}
print $_;
}
for (@lines2) {
print $_;
}
for (@lines_end1) {
print $_;
}
Suggested way: wrap perl script into a bash script. Example:
merge_pdf
put_qDebug.pl
#!/bin/perl
# Usage: perl put_qDebug.pl file.cpp > file_qDebug.cpp
# Warning: do not forget to add #include <QDebug>
## Explanation: research of patterns such as:
#
# $output_type Class1::method1()
# {
#
## to add:
#
# qDebug() << "method1" << '\n';
#
use strict;
use warnings;
# open (my $out, '>', "file_qDebug.cpp") or die "Cannot write new file: $!";
my $output_type = '(void|b1)';
my $excepted_method_list = 'IsValid';
my $regexp = '^' . "$output_type" . ' .+::(.+)\(.*\).*$\n';
my $test_method = 0;
my $test_bracket = 0;
my $last_method_name = "";
while (<>)
{
if ($test_method == 1 && !($last_method_name =~ $excepted_method_list))
{
print "qDebug() << \"$last_method_name\" << '\\n';\n";
$test_method = 0;
$test_bracket = 0;
$last_method_name = "";
}
elsif ($test_method > 1)
{
$test_method--;
if (/{/)
{
$test_bracket = 1;
}
}
if (/$regexp/)
{
$test_method = 2;
$last_method_name = $2 # to catch method1 in 2nd parentheses () of regexp
}
print;
}