Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

_DATA_ in File::MMagic breaks mod_perl scripts?

by blahblahblah (Priest)
on Nov 21, 2005 at 16:24 UTC ( [id://510476]=perlquestion: print w/replies, xml ) Need Help??

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

In Re: Discvering the MIME type of a document from its content, davorg quotes this section of File::Type's docs:

File::MMagic inlines a copy of the magic database, and uses a DATA filehandle, which causes problems when running under mod_perl.

This worries me because I'm using File::MMagic in a couple of ModPerl::Registry scripts. Can someone explain why this is a problem? All I could find is the warning in chapter 6.6 of the mod_perl book, but does that apply to used or required code from a separate file? It seems to me like it would only matter if the _DATA_ token was in the main script, which is what gets wrapped by mod_perl.

Thanks,
Joe

  • Comment on _DATA_ in File::MMagic breaks mod_perl scripts?

Replies are listed 'Best First'.
Re: _DATA_ in File::MMagic breaks mod_perl scripts?
by PodMaster (Abbot) on Nov 22, 2005 at 15:28 UTC
    Can someone explain why this is a problem?
    Because that's what the File-MMagic README says?
    5. Notice

    If you want to use the module under mod_perl environment, you'll need to divide the module into module part and magic entry file part, and use the magic file explicitly. Built-in magic entry does not work correctly under mod_perl.

    If the authors says it doesn't work correctly I tend to believe :)

    On the other hand, if its working for you, go for it.

    Examining the source it seems as if every time File::MMagic->new is invoked, File::MMagic reads from its DATA section, and it seems to me like that should be done at most once, so here's an untested replacement for sub new

    my $do_data_magic_only_once = undef; sub new { my $self = {}; my $proto = shift; my $class = ref($proto) || $proto; $self->{MF} = []; $self->{magic} = []; if (! @_) { if( $do_data_magic_only_once ){ $self->{magic} = $do_data_magic_only_once; } else { my $fh = *File::MMagic::DATA{IO}; binmode($fh); bless $fh, 'FileHandle' if ref $fh ne 'FileHandle'; my $dataLoc; # code block to localise the no strict;, contribute by Sim +on Matthews { no strict 'refs'; my $instance = \${ "$class\::_instance" }; $$instance = $fh->tell() unless $$instance; $dataLoc = $$instance; } $fh->seek($dataLoc, 0); &readMagicHandle($self, $fh); $do_data_magic_only_once = $self->{magic}; } } else { my $filename = shift; my $fh = new FileHandle; if ($fh->open("< $filename")) { binmode($fh); &readMagicHandle($self, $fh); } else { warn __PACKAGE__ . " couldn't load specified file $filename"; } } # from the BSD names.h, some tokens for hard-coded checks of # different texts. This isn't rocket science. It's prone to # failure so these checks are only a last resort. # removSpecials() can be used to remove those afterwards. $self->{SPECIALS} = { "message/rfc822" => [ "^Received:", "^>From ", "^From ", "^To: ", "^Return-Path: ", "^Cc: ", "^X-Mailer: "], "message/news" => [ "^Newsgroups: ", "^Path: ", "^X-Newsreader: "], "text/html" => [ "<html[^>]*>", "<HTML[^>]*>", "<head[^>]*>", "<HEAD[^>]*>", "<body[^>]*>", "<BODY[^>]*>", "<title[^>]*>", "<TITLE[^>]*>", "<h1[^>]*>", "<H1[^>]*>", ], "text/x-roff" => [ '^\\.\\\\"', "^\\.SH ", "^\\.PP ", "^\\.TH ", "^\\.BR ", "^\\.SS ", "^\\.TP ", "^\\.IR ", ], }; $self->{FILEEXTS} = { '\.gz$' => 'application/x-gzip', '\.bz2$' => 'application/x-bzip2', '\.Z$' => 'application/x-compress', '\.txt$' => 'text/plain', '\.html$' => 'text/html', '\.htm$' => 'text/html', }; bless($self); return $self; }

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks! I didn't think to look at the readme (I'm not the one who installed the module here or who wrote our code that uses it.) The author ought to mention this limitation in the POD also.

      Looking at your new sub, I think I see why this doesn't affect us. We use our own magic file, and by passing a filename to new() we skip the whole "if (! @_)" block.

      If you want to use the module under mod_perl environment, you'll need to divide the module into module part and magic entry file part, and use the magic file explicitly. Built-in magic entry does not work correctly under mod_perl.


      This doesn't sound clear to me, since I thought the whole problem with File::MMagic was about its __DATA__ section. But, as explained in paragraph 6.6 of "Practical mod_perl", this problem should bite only when the __DATA__ is in a script, and not in a module. So, why File::MMagic can't work with mod_perl?

      cheers,
      larsen
Re: _DATA_ in File::MMagic breaks mod_perl scripts?
by Anonymous Monk on Nov 22, 2005 at 16:36 UTC
    The Changelog notes that this was fixed in 1.21

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://510476]
Approved by Corion
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found