15.03.2020 Views

perl-language-es

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Perl no intenta decodificar los nombres de archivos devueltos por las funciones integradas o los

módulos. Tales cadenas que representan nombres de archivos siempre deben decodificarse

explícitamente, para que Perl los reconozca como Unicode.

use v5.14;

use Encode qw(decode_utf8);

# Ensure that possible error messages printed to screen are converted to UTF-8.

# For this to work: Check that you terminal emulator is using UTF-8.

binmode STDOUT, ':utf8';

binmode STDERR, ':utf8';

# Example1 -- using readdir()

my $dir = '.';

opendir(my $dh, $dir) or die "Could not open directory '$dir': $!";

while (my $filename = decode_utf8(readdir $dh)) {

# Do something with $filename

}

close $dh;

# Example2 -- using getcwd()

use Cwd qw(getcwd);

my $dir = decode_utf8( getcwd() );

# Example3 -- using abs2rel()

use File::Spec;

use utf8;

my $base = 'ø';

my $path = "$base/b/æ";

my $relpath = decode_utf8( File::Spec->abs2rel( $path, $base ) );

# Note: If you omit $base, you need to encode $path first:

use Encode qw(encode_utf8);

my $relpath = decode_utf8( File::Spec->abs2rel( encode_utf8( $path ) ) );

# Example4 -- using File::Find::Rule (part1 matching a filename)

use File::Find::Rule;

use utf8;

use Encode qw(encode_utf8);

my $filename = 'æ';

# File::Find::Rule needs $filename to be encoded

my @files = File::Find::Rule->new->name( encode_utf8($filename) )->in('.');

$_ = decode_utf8( $_ ) for @files;

# Example5 -- using File::Find::Rule (part2 matching a regular expression)

use File::Find::Rule;

use utf8;

my $pat = '[æ].$'; # Unicode pattern

# Note: In this case: File::Find::Rule->new->name( qr/$pat/ )->in('.')

# will not work since $pat is Unicode and filenames are bytes

# Also encoding $pat first will not work correctly

my @files;

File::Find::Rule->new->exec( sub { wanted( $pat, \@files ) } )->in('.');

$_ = decode_utf8( $_ ) for @files;

sub wanted {

my ( $pat, $files ) = @_;

my $name = decode_utf8( $_ );

my $full_name = decode_utf8( $File::Find::name );

push @$files, $full_name if $name =~ /$pat/;

}

https://riptutorial.com/es/home 109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!