-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: An Ant Pushes a Perl

Вторник, 03 Мая 2016 г. 13:30 + в цитатник

Its an old joke that Perl is a write only language. Despite some of its issues, back in the early 2000s, Perl was one of the best options out there for scripting languages and rapid-development automation.

Speaking of automation, build automation is really important. Back in the early 2000s, before Maven really caught on, your build automation tool for Java was Ant. Ant, like everything invented in the early 2000s, was driven by an XML scripting tool. Since it was tuned specifically for Java, it had some high-level operations to streamline tasks like generating proxy classes for calling web services based on a supplied WSDL file.

Actually, speaking of code generation, Carrie sends us this code block. Its a Perl script thats called from an Ant build. It runs after generating a class based off a WSDL. It parses Java code using Regular Expressions and injects a ListWrapper class which doesnt adhere to the List contract. But hey, it does have a use strict declaration, guaranteeing youll get errors if you access uninitialized variables.

use strict;
use warnings;

my $dir = $ARGV[0];
readDir($dir);

sub readDir {
        my ($dir) = @_;
        opendir my $dirHandle, $dir or die "Cannot open $dir.\n$!\n";
        foreach my $file(readdir($dirHandle)) {
                next if $file =~ m/^\./;

                if(-d "$dir/$file") {
                        readDir("$dir/$file");
                        next;
                }

                my %seenFields;
        my %multiples;

                my $fileName = "$dir/$file";

                open IN, "<$fileName" or die "Cannot open $fileName.\n$!\n";

                my @methods;

                my $file = "";
                my $currentLine;
                my $containsContent = 0;
                while() {
                        if($_ =~ m/\@XmlElementRef\(name\s*=\s*"(.*?)".+namespace\s*=\s*"(.*?)".+type\s*=\s*(.*?.class)/) {
                                my $field = ucfirst($1);
                my $namespace = $2;
                                my $class = $3;

                $multiples{$1}++;
                if($multiples{$field} > 1) {
                    $_ =~ s/name\s*=\s*"$1"/name = "$1$multiples{$1}"/gis;
                    $field = $1 . $multiples{$1};
                }

                                my $fieldlc = lc($field);
                                my $retObject = substr($class, 0, length($class) - 6);
                                die if not defined $retObject;
                                my $methodName = $field;
                                unless(defined $seenFields{$methodName}) {
                                        $seenFields{$methodName} = 1;
                                        my $method = < 4) {
                                  className = className.substring(0, className.length() - 4);
                                  if(className.equalsIgnoreCase("$fieldlc")) {
                                          good = true;
                                  }
                          }
                  }
                  if(good) {
                                retVal.addWrapped(current);
                        }
                }
                return retVal;
        }

EOF
                                        push(@methods, $method);
                                }
                        } elsif ($_ =~ m/getContent\(\)/) {
                                $containsContent = 1;
            }
                        $currentLine = $_;
                        $file .= $currentLine if defined $currentLine;
                }
                close IN;

                if($containsContent) {
                        print "$fileName\n";

                        for my $method(@methods) {
                                $file .= $method;
                        }

                        $file .= < extends ArrayList {
            private List contentsList;

            public ListWrapper(List contents) {
                super();
                contentsList = contents;
            }

            public boolean addWrapped(T toAdd) {
                return super.add(toAdd);
            }

            \@Override
            public boolean add(T toAdd) {
                return contentsList.add(toAdd);
            }

            \@Override
            public boolean addAll(java.util.Collection

http://thedailywtf.com/articles/an-ant-pushes-a-perl

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку