5.8 bilan ishlatish uchun port kodini qanday qilib o'chirish mumkin?

Perl 5.10 ni ishlatadigan maxsus kodni (switch-operatsiyani bajarish uchun), lekin uni 5.8 da ishlashi kerak. Buni 5.8 ga yozishning yana bir usuli qanday? Afzal yo'l/usul

for my $detail ($Details1, $Details2) {
    for (keys %$detail) {
        when ('file') {
            print " File: $detail->{file}{path}\n";
            print "Bytes: $detail->{file}{size}\n";
        }

        when ('directory') {
            given (ref $result->{directory}) {
                when ('ARRAY') {
                    for my $entry (@{$detail->{directory}}) {
                        print "Directory: $entry->{path}\n";
                    }
                }
                when ('HASH') {
                    print "Directory: $detail->{directory}{path}\n";
                }
            }
        }
    }
}
2

3 javoblar

agar / elsif s bo'lsa, / kodini o'zgartirish kifoya qiladi.

for my $detail ( $Details1, $Details2 ) {

    for ( keys %$detail ) {

        if ( $_ eq 'file' ) {

            print " File: $detail->{file}{path}\n";
            print "Bytes: $detail->{file}{size}\n";
        }

        elsif ( $_ eq 'directory' ) {

            if ( ref $result->{directory} eq 'ARRAY' ) {

                for my $entry ( @{$detail->{directory}} ) {
                    print "Directory: $entry->{path}\n";
                }
            }

            if ( ref $result->{directory} eq 'HASH' ) {
                print "Directory: $detail->{directory}{path}\n";
            }
        }
    }
}

Lekin men uni anonim subs deb dispetcherlik jadvali bilan qayta yozishni boshladim.

7
qo'shib qo'ydi
if (! $ result -> {directory}) {...} yoki if ($ natija -> {katalog}) eq '') {..} bo'sh bir mag'lubiyatga mos tushadi.
qo'shib qo'ydi muallif Zaid, manba
rahmat. agar qaytarish uchun hech qanday ma'lumotim bo'lmasa, bo'sh qator uchun ref ni ham tekshirishim mumkinmi?
qo'shib qo'ydi muallif cjd143SD, manba

Menimcha oddiy if-elsif kodni almashtirish kodini almashtirish uchun kifoya qiladi.

The CPAN Switch is a source filter thus I guess it will generate similar code to if-elsif. And it has some limitation when you use it in eval: "Due to the way source filters work in Perl, you can't use Switch inside an string eval." http://perldoc.perl.org/5.8.9/Switch.html

3
qo'shib qo'ydi
@cjd - iltimos, sizning xatolaringizni ko'rib chiqaylik
qo'shib qo'ydi muallif DVK, manba
Rahmat. Men buni if-elsif bilan urinib ko'rdim. Menga if-elsif bilan ko'rsata olasizmi?
qo'shib qo'ydi muallif cjd143SD, manba

Shaxsiy afzalligim for , regex va do bloklarini ishlatishdir:

for my $detail ($Details1, $Details2) {
    for (keys %$detail) {

        /^file$/ && do {
            print " File: $detail->{file}{path}\n";
            print "Bytes: $detail->{file}{size}\n";
        };

        /^directory$/ && do {

            for (ref $result->{directory}) {

                /^ARRAY$/ && do {
                    for my $entry (@{$detail->{directory}}) {
                        print "Directory: $entry->{path}\n";
                    }
                };

                /^HASH$/ && do {
                    print "Directory: $detail->{directory}{path}\n";
                };
            }
        };
    }
}

Har do blokidan keyin ; ni unutmang.

E'tiroz: Keyingi ishingizga o'tishni xohlamasangiz, keyingi yoki last dan foydalanishni unutmang.

2
qo'shib qo'ydi