Search code examples
perlstat

perl stat() does not work with filenames with special characters


With filenames having characters like '@' perl's stat() does not seem to work.

-rw-r--r-- 1 root root 0 Mar  1 17:33 /tmp/mark@er.txt

Perl command used:

#perl -e 'my $modtime = (stat("/tmp/mark@er.txt"))[9]|| die "$!"'
  No such file or directory at -e line 1.

Can anyone help how to escape these characters for stat()?


Solution

  • perl "sees" a @er array there, so you are stating /tmp/mark.txt. Try this:

    perl -e 'my $modtime = (stat("/tmp/mark\@er.txt"))[9] || die "$!"'
    

    Or you can use non-interpolating single quotes by using q() (thanks amon)

    perl -e 'my $modtime = (stat(q(/tmp/mark@er.txt)))[9] || die "$!"'