in reply to Detecting 'binary' in a variable

would a simple if ( $file =~ /[^\x00-\xA5]/ ) { # binary } else { #text } suffice?

Update: Also looks like there's a CGI::UploadEasy method "fileinfo" (in case you're using or could use that module)

Replies are listed 'Best First'.
Re^2: Detecting 'binary' in a variable
by dbwiz (Curate) on Jul 05, 2005 at 18:19 UTC

    Not always

    $ perl -le '{local$/; $_=<>;}print /^[\x00-\xA5]/ ? "binary" : "text" +' \ /mnt/win/WINDOWS/system32/command.com text
      That's correct, but that's not the same regexp:

      /^[\x00-\xA5]/
      ne
      /[^\x00-\xA5]/

        Sorry. My fault.

        But then, again, any text with accented vowels (à,è etc., in Italian, French, Spanish) will look like binary.

Re^2: Detecting 'binary' in a variable
by kirbyk (Friar) on Jul 05, 2005 at 18:01 UTC
    Thanks, that regex does the trick.

    -- Kirby, WhitePages.com