sara ibn el ahrache has asked for the wisdom of the Perl Monks concerning the following question:

I need to write a perl script that should access an iamp server, fetch the latest message, and convert it to an audio file. Last week the script was working fine but this week it starts to give me the following error: mmap() failed invalid argument. I googled the error but in vain. the following is the code i am using:
#!/usr/bin/env perl use strict; use warnings; use Mail::IMAPClient; use IO::Socket::SSL; use IO::File; use Mail::IMAPClient::MessageSet; use Mail::IMAPClient::BodyStructure; use MIME::Parser; # Connect to the IMAP server via SSL my $socket = IO::Socket::SSL->new( PeerAddr => ' imap.gmail.com ', PeerPort => 993, ) or die "socket(): $@"; # Build up a client attached to the SSL socket. # Login is automatic as usual when we provide User and Password my $client = Mail::IMAPClient->new( Socket => $socket, User => 'sara.ibnelahrache', Password => 'saraibnelahrache', ) or die "new(): $@"; $client->select('INBOX'); my $i = $client->message_count; my $msgString = $client->body_string($i); my $newtxt = ''; my $parser = new MIME::Parser; $parser->output_under('/tmp'); $parser->decode_headers(1); $parser->extract_nested_messages(0); $parser->ignore_errors(1); my $entity = $parser->parse_data($msgString); if ($entity->bodyhandle) { $newtxt = $entity->bodyhandle->as_string; } elsif ($entity->parts > -1) { $newtxt = $entity->parts(0); } else { $newtxt = "Unable to parse message text!"; } my $text = qq($newtxt); my $sounddir = "/var/lib/asterisk/sounds"; my $wavefile = "$sounddir/"."message.wav"; my $wavefileAst = "$sounddir/"."message"; my $t2wp= "/opt/swift/bin/"; unless (-f $wavefile) { open(fileOUT, ">$sounddir"."/message.txt"); print fileOUT "$text"; close(fileOUT); my $execf=$t2wp."swift -f $sounddir/message.txt -p audio/channels= +1,audio/volume=70,audio/sampling-rate=8000,audio/deada$ system( +$execf); unlink($sounddir."/message.txt"); } ###################################################################### +################################################ #Say bye $client->logout();

Replies are listed 'Best First'.
Re: mail::imapclient + gmail + mmap() invalid argument
by jethro (Monsignor) on May 26, 2009 at 23:36 UTC
    You could try to find out whether it is the perl script or swift that is producing the error.

    Either use the perl built-in debugger or a real debugger or simply 'strace' (if you are on linux) to find out the exact place your program gets killed.

    If you have a version control system in use, find out what changed from last week. If not, you know now why it is a good idea to have one

    If someone else installed updates on your machine, find out if one of the modules or other software your script is using got updated

Re: mail::imapclient + gmail + mmap() invalid argument
by Perlbotics (Archbishop) on May 27, 2009 at 18:12 UTC

    This part doesn't look good (copy & paste?):

    my $execf=$t2wp."swift -f $sounddir/message.txt -p audio/channels= +1,audio/volume=70,audio/sampling-rate=8000,audio/deada$ system( +$execf);

    Despite the error here, my guess would be that the error message is generated by the call to system, not by perl. You could try to manually start the swift application with appropriate parameters to see if you can reproduce this error. Maybe it's the input file with wrong properties (too long, encoding, etc.) that is the primary root of the problem? So check that too.

    Appending something like  > /tmp/swiftcall.log 2>&1 to $execf would be another approach. Run the Perl program and then have a look at /tmp/swiftcall.log - if you see the same error message there, its the call to swift that caused the problem. Here, the cause could be an updated binary or a mismatch with a dynamic library.

    Running ldd or strace on /opt/swift/bin/swift and examining $LD_LIBRARY_PATH may provide further insights.

    Finally, your program should check the return value of system. Security might be improved by using the system PROGRAM LIST form (but then the redirection-trick above would not work since the shell is no longer involved).

    HTH, Perlbotics