خرید بک لینک

Vote count: 0

I am working on an application that takes a regex as input from the user at the command line, and then applies that regex to find certain files under the current directory. The application support UTF-8 input, and should be able to find UTF-8 encoded file names. Here is an example:

use feature qw(say);
use open qw( :std :utf8 );
use strict;
use utf8;
use waings;

use Encode ();
use File::Find::Rule;

system 'touch', 'aæ', 'bæ', 'aa'; # some test files, 

my $pat = 'æ$';
my $pat_encode = encode( $pat );
run_test( $pat_encode, 'With encode()' );
run_test( $pat, 'Without encode()' );
my $pat2 = '[æ]$';
my $pat2_encode = encode( $pat2 );
run_test( $pat2_encode, 'With encode()' );

sub encode {
    retu Encode::encode('UTF-8', $_[0], Encode::FB_CROAK | Encode::LEAVE_SRC);
}

sub run_test {
    my ( $pat_encode, $test_str ) = @_;

    say $test_str;
    say '-' x length $test_str;
    say "";
    my @files = File::Find::Rule->new->name( qr/$pat_encode/ )->in('.');
    for (@files) {
        $_ = Encode::decode('UTF-8', $_, Encode::FB_CROAK | Encode::LEAVE_SRC );
    }

    say $_ for @files;
}

The output is:

With encode()
-------------

aæ
bæ
Without encode()
----------------

With encode()
-------------

aæ
bæ

I would expect that the last regex [æ]$ would not work after being encoded, since the æ will expand to two bytes 0xC3A6, but somehow, it seems Perl knows that the regex is encoded in UTF-8 and does some magic to have it work.

I wonder if someone knows why the latter example is working, and if there are any other cases where encoding the regex would not work? ( So I am trying to decide if I can use File::Find::Rule or if I should switch to File::Find which would allow me to avoid having to encoding the regex.)

asked 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 1 ارديبهشت 1395 ساعت: 13:49

صفحه بندی