Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

sending an email to a perl script

by davidj (Priest)
on Nov 16, 2006 at 17:01 UTC ( #584517=perlquestion: print w/replies, xml ) Need Help??
davidj has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone,

I have a situation at work in which managements desired solution is an email, .forward, and a perl script. Basically, it needs to process the email content, do some db stuff, and log some stuff.

As this is the first time I've done something like this, there's one thing in particular I do not understand. When I do this:

#!/usr/bin/perl while (<STDIN>) { -- process the email content } -- do the db stuff here open(FILE, ">logfile"); ---write to logfile close(FILE);
The file does not get opened and written to.

However, when I do this:

#!/usr/bin/perl open(FILE, ">logfile"); while (<STDIN>) { -- process the email content } -- do the db stuff here ---write to logfile close(FILE);
the file does get opened and written to.

My guess is that it has to do with I/O and buffering. The only thing I could think of was to set  $|=1; , but that didn't work. I could do it the second way, since its pretty trivial, but I would really like to know how to do it the first way.

As always, thanks for your wisdom


Replies are listed 'Best First'.
Re: sending an email to a perl script
by grinder (Bishop) on Nov 16, 2006 at 17:37 UTC

    I don't think it has anything to do with buffering. I would say that the code is failing to open the file, but it doesn't tell you why, since the code doesn't print $! in the event of an error. (I don't know if you are paraphrasing the code, and omitting the error check).

    Another possibility is that something in the while is calling exit, or dying. Again, without seeing the code, it's difficult to know. Can you strip the program down to a bare minimum, for instance, the while loop just counts the number of lines, and print that to the logfile.

    A final possibility is that you are opening the output file for create, not append. Maybe the subsequent run trashes the file each time.

    • another intruder with the mooring in the heart of the Perl

Re: sending an email to a perl script
by Melly (Hermit) on Nov 16, 2006 at 17:32 UTC

    Hmm, I suspect that your problem is not the problem you think you have... btw how are you terminating your while(<STDIN>) loop?

    Basically, I don't think buffering is the problem here, but without more code, it's hard to say - certainly there's nothing in your code that indicates a buffering problem.

    Tom Melly,
Re: sending an email to a perl script
by blahblahblah (Priest) on Nov 17, 2006 at 12:20 UTC
    Since you're not using an absolute path, it's possible that your working dir is different in each case. In addition to the above suggestions, I'd suggest using the full path of the filename when opening it.


Re: sending an email to a perl script
by GrandFather (Sage) on Nov 16, 2006 at 19:40 UTC

    Can you reproduce the problem replacing STDIN with DATA (and a __DATA__ section) and FILE with STDOUT? If so, post the sample here.

    Using STDIN to provide the source is somewhat unusual. It would be more conventional to provide an input file name on the command line and open an input file rather than use STDIN.

    DWIM is Perl's answer to Gödel
      Using STDIN to provide the source is somewhat unusual.

      It's usual for handling mail in a .forward file, however.

Re: sending an email to a perl script
by j3 (Friar) on Nov 16, 2006 at 20:30 UTC
    You might add
    use strict; use warnings;
    to the top of your script. That would very likely shed some light on the problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://584517]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2019-04-19 06:19 GMT
Find Nodes?
    Voting Booth?
    I am most likely to install a new module from CPAN if:

    Results (106 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!