Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: _DATA_ in File::MMagic breaks mod_perl scripts?

by PodMaster (Abbot)
on Nov 22, 2005 at 15:28 UTC ( #510828=note: print w/ replies, xml ) Need Help??


in reply to _DATA_ in File::MMagic breaks mod_perl scripts?

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.


Comment on Re: _DATA_ in File::MMagic breaks mod_perl scripts?
Select or Download Code
Re^2: _DATA_ in File::MMagic breaks mod_perl scripts?
by blahblahblah (Priest) on Nov 22, 2005 at 17:09 UTC
    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.

Re^2: _DATA_ in File::MMagic breaks mod_perl scripts?
by larsen (Parson) on Nov 15, 2006 at 12:05 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://510828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-10-25 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls