Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Is It Text-Editable???

by Anonymous Monk
on Apr 27, 2004 at 15:43 UTC ( [id://348620]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

testing a file $file

is there quick way to do this, i've got it seeing if it's a file or directory using if (-d $file), is there a -* or wutnot to see if it is editable via text-editor? (.cgi vs .jpg)?

Replies are listed 'Best First'.
Re: Is It Text-Editable???
by gjb (Vicar) on Apr 27, 2004 at 15:46 UTC

    -T tries to guess whether the file is ASCII, -B is for binary. See the docs for more info.

    Hope this helps, -gjb-

Re: Is It Text-Editable???
by matija (Priest) on Apr 27, 2004 at 15:49 UTC
    That's a difficult call to make - a .jpg is editable with a graphics program like GIMP.

    But if you mean if it's a text file or not, Perl can try to guess if you check the file with -B operator.Note that this is just a guess, and some text files might confuse it. In particular, I've never tried it with UTF or ISO-8859-X files.

    Another option is to call the Unix file function, and parse it's output to see if it thought you were dealing with a text file.

      File::Magic is a more perlish way of finding a file's type. If you use the MIME version of this from File::MimeInfo::Magic, you can probably just see if the MIME type matches m|^text/|.

      Update: As davido says, File::Type may work too. It claims to solve many bugs in the two modules I suggested.

Re: Is It Text-Editable???
by dragonchild (Archbishop) on Apr 27, 2004 at 16:15 UTC
    You could do a sysread of the first N bytes and see if any are above 0x7F. I wouldn't recommend it, though. Personally, I'd depend on the extension and let people hang themselves.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      You could do a sysread of the first N bytes and see if any are above 0x7F.

      But only if you're looking for english text. For virtually any other language, the range between 0xa0 and 0xff is valid, too. And there are sure some encodings where 0x7f-0xa0 is also valid.

Re: Is It Text-Editable???
by davido (Cardinal) on Apr 27, 2004 at 16:23 UTC
    You could use File::Type whos description is, "determine file type using magic". Sounds good to me. ;)

    Derived from the POD:

    my $file = 'path/to/filename.ext'; my $ft = File::Type->new(); my $type = $ft->checktype_filename( $file );

    HTH!


    Dave

      I'd be very curious to see how File::Type compares against file(1). Of course it obviously depends whose version of file(1) you're running. It's all about the clever heuristics.
Re: Is It Text-Editable???
by chanio (Priest) on Apr 27, 2004 at 22:20 UTC
    -w tests if the file is writable. That is, if you could modify it.

    {\('v')/}
    _`(___)' __________________________

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://348620]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.