Subject: Re: a recursion perl script
To: Water NB <netbsd78@126.com>
From: Robby Griffin <rmg@yakshavers.com>
List: netbsd-users
Date: 09/05/2006 14:52:43
Not that this has anything to do with NetBSD per se, but when you refer
to $g, it's just a filename and not a path. Pretty much everything
inside the foreach will fail as soon as you're looking inside a
subdirectory, because those filenames won't mean anything in the
current working directory. You could chdir first (but that would give
&g2u weird side effects), or prepend a relative path. See also
File::Find, I think.
--Robby
On Sep 5, 2006, at 12:57, Water NB wrote:
> I wrote below perl script to convert all files and directories.
> When it enter the first directory, the script quit and skip any others
> directories without any errors.
> What's wrong?
>
>
> #! /usr/pkg/bin/perl
>
> use Unicode::UTF8simple;
> use Cwd;
>
> my $uref = new Unicode::UTF8simple;
> my $dirname = shift;
>
> sub g2u {
> my ($dirname) = @_;
> my $g, $u, @D;
> printf "----> $dirname\n";
> opendir(DIR, $dirname) || die "can't opendir $dirname: $!";
> @D = readdir(DIR);
> foreach $g (@D) {
> #printf "file $g\n";
> if ( -d $g && $g ne "." && $g ne "..") {
> #printf "----> $g\n";
> &g2u("$dirname/$g");
> }
> my $u = $uref->toUTF8("gb2312", $g);
> if ($g ne $u) {
> printf "GB2312==>UTF8 $g\n";
> #rename "$dirname/$g", "$dirname/$u";
> }
> }
> closedir DIR;
> }
>
> &g2u($dirname);