Perl

Un article de MonWiki.

Exemple de script Perl

Note : voir plutôt le portage en Ruby de ce script.

#! /usr/bin/perl 

use strict;
use warnings;

####
#   Conf.

my $prefix = shift or die "Please indicate a prefix, you don't want to get all the strings!";

my $codeDir   = "/cygdrive/c/Scripts/States";
my $code      = "PlayerStates.lua";
my $regexCode = "'($prefix\[\\w_\]*)'"; 

my $specDir   = "/cygdrive/c/Base_Documentaire/StateMachine";
my $spec      = "States.vdx";
my $regexSpec ="<Text>(<.*>)*($prefix.*)</Text>"; 

####
#   Subroutines

sub WriteHashKeysToFile {
    my( $hash, $filename ) = @_;
    print "> Wrinting $filename...\n";
    open( my $file, "> $filename" ) || die "can't open $filename: $!";
    for my $key (sort keys %{$hash}) {
        print $file "$key\n";
    }
    close $file;
}

sub Diff {
    my( $name1, $hash1, $name2, $hash2 ) = @_;
    if( 0 == scalar keys %{$hash1} ) {
        print "> No string found in $name1.\n";
        return;
    }
    my $found;
    for my $key ( sort keys %{$hash1} ) {
        if( not ${$hash2}{$key} ) {
            if( not $found ) {
                print "> String(s) in $name1 & not in $name2:\n";
                $found = 1;
            }
            print "  - '$key'\n";
        }
    }
    if( not $found ) {
        print "> Strings form $name1 are all in $name2.\n";
    }
}

sub FilePath {
    my( $dir, $file ) = @_;
    if( $dir ) {
        return "$dir/$file";
    }
    else {
        return "$file";
    }
}

print "= Looking for '$prefix' =\n";
print "Reading:\n";

my $file;
my $regex;
my $hash;
my %codeStrings;
my %specStrings;

####
#   Reading code strings

print "> Reading strings from Code...\n";

open( $file, FilePath( $codeDir, $code ) ) || die "can't open $code: $!";

$hash = \%codeStrings;
$regex = $regexCode;

while ( <$file> ) {
    if ($_ =~ /$regex/g) {
        if ($2) {
            ${$hash}{$2} = $2;
        }
        ${$hash}{$1} = $1;
    }
}
close $file;

####
#   Reading spec strings

print "> Reading strings from Specs...\n";

open( $file, FilePath( $specDir, $spec ) ) || die "can't open $spec: $!";

$hash = \%specStrings;
$regex = $regexSpec;

while ( <$file> ) {
    if ($_ =~ /$regex/g) {
        if ($2) {
            ${$hash}{$2} = $2;
        }
        else {
            ${$hash}{$1} = $1;
        }
    }
}
close $file;

####
#   Output

print "Diffing:\n";
Diff( "Code", \%codeStrings, "Specs", \%specStrings );
Diff( "Specs", \%specStrings , "Code", \%codeStrings);

# WriteHashKeysToFile( \%codeStrings, "StringsFrom$code.txt" );
# WriteHashKeysToFile( \%specStrings, "StringsFrom$spec.txt" );

print "All done.\n"

Warning: main() [function.main]: open_basedir restriction in effect. File(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) is not within the allowed path(s): (/mnt/109/sdb/9/8/sroccaserra) in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) [function.main]: failed to open stream: Operation not permitted in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main() [function.include]: Failed opening '/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt' for inclusion (include_path='/mnt/109/sdb/9/8/sroccaserra/include:.:/usr/php4/lib/php') in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2