sending data from one program to a perl prog

Discussion in 'Perl Misc' started by Andrew Wheeler, Apr 26, 2004.

  1. Hello everyone,

    I want to write a Perl program to edit an email then sent the email back
    to the sender automatically.

    My email program allows me to filter emails and send the email to a
    program (As I already have done with Spam-Assassin)

    However as this is my first real Perl Program and I need a little help:)

    Can anyone point me to any documentation on how Perl handles a large block
    of text (the email) when it is piped directly to a shell command (the Perl
    program).
    I need to replace all occurrences of [ ] with [ X ] so is it best to
    treat the email as one block or break it into chunks and process the
    chunks separately.

    And what would happen if evolution pipes two separate messages the Perl
    program? will the system start 2 separate programs or crash ?

    Any comments gratefully received

    Andrew
     
    Andrew Wheeler, Apr 26, 2004
    #1
    1. Advertising

  2. Andrew Wheeler wrote:
    > Can anyone point me to any documentation on how Perl handles a
    > large block of text (the email) when it is piped directly to a
    > shell command (the Perl program).


    Well, it depends on the code in the Perl program. ;-)

    Such a message is made available in STDIN, and this is an (untested)
    example code fragment for storing it in a variable:

    my $msg;
    my $maxsize = 131072;
    unless ($ENV{CONTENT_LENGTH} > $maxsize) {
    $msg = do {local $/; <STDIN>};
    } else {
    print "Requested action aborted:\n",
    "Message too large.\n\n";
    exit;
    }

    There are suitable modules at CPAN for parsing the message, so you'd
    better take a look there.

    > I need to replace all occurrences of [ ] with [ X ] so is it best
    > to treat the email as one block or break it into chunks and process
    > the chunks separately.


    It sounds to me as if you can make a global substitution to the whole
    message body at one time.

    Then, so send the revised message, I recommend that you pay another
    visit at CPAN for a mail sending module. There are quite a few of
    them; my personal favorite is Mail::Sender.

    > And what would happen if evolution pipes two separate messages the
    > Perl program? will the system start 2 separate programs or crash ?


    Two processes; shouldn't cause a problem.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 26, 2004
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    >
    > my $msg;
    > my $maxsize = 131072;
    > unless ($ENV{CONTENT_LENGTH} > $maxsize) {
    > $msg = do {local $/; <STDIN>};


    I'd better add that such a check of message size does not always work.
    For instance, the sendmail configuration on my own brand new server is
    so 'secure' so that no %ENV variable at all is present when a process
    is run as the mail program. Perl's stat() function, i.e.
    (stat STDIN)[7], does not contain the size either...

    Oh, well.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 27, 2004
    #3
  4. Andrew Wheeler

    Joe Smith Guest

    Gunnar Hjalmarsson wrote:

    > Gunnar Hjalmarsson wrote:
    >
    >>
    >> my $msg;
    >> my $maxsize = 131072;
    >> unless ($ENV{CONTENT_LENGTH} > $maxsize) {
    >> $msg = do {local $/; <STDIN>};

    >
    >
    > I'd better add that such a check of message size does not always work.
    > For instance, the sendmail configuration on my own brand new server is
    > so 'secure' so that no %ENV variable at all is present when a process is
    > run as the mail program. Perl's stat() function, i.e.
    > (stat STDIN)[7], does not contain the size either...
    >
    > Oh, well.


    What about something like
    my $bytes_read = read STDIN,$msg,$maxsize;
    unless eof(STDIN) { return 'error'};
     
    Joe Smith, Apr 27, 2004
    #4
  5. Joe Smith wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> my $msg;
    >> my $maxsize = 131072;
    >> unless ($ENV{CONTENT_LENGTH} > $maxsize) {
    >> $msg = do {local $/; <STDIN>};
    >>
    >> I'd better add that such a check of message size does not always
    >> work. For instance, the sendmail configuration on my own brand
    >> new server is so 'secure' so that no %ENV variable at all is
    >> present when a process is run as the mail program. Perl's stat()
    >> function, i.e. (stat STDIN)[7], does not contain the size
    >> either...
    >>
    >> Oh, well.

    >
    > What about something like
    > my $bytes_read = read STDIN,$msg,$maxsize;
    > unless eof(STDIN) { return 'error'};


    Yes, that's it. (But you don't need $bytes_read, do you?) Thanks, Joe!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 27, 2004
    #5
  6. On Mon, 26 Apr 2004 23:42:43 +0200, Gunnar Hjalmarsson wrote:

    > my $maxsize = 131072;


    what is the significance of 131072 as a max size ?
     
    Andrew Wheeler, Apr 27, 2004
    #6
  7. Andrew Wheeler wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >>my $maxsize = 131072;

    >
    > what is the significance of 131072 as a max size ?


    None, AFAIK. It's up to you to decide what's a reasonable value (if
    you want to include such a check).

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 27, 2004
    #7
  8. Gunnar Hjalmarsson wrote:
    > Andrew Wheeler wrote:
    >> Gunnar Hjalmarsson wrote:
    >>>
    >>> my $maxsize = 131072;

    >>
    >> what is the significance of 131072 as a max size ?

    >
    > None, AFAIK. It's up to you to decide what's a reasonable value (if
    > you want to include such a check).


    Besides the obvious, of course, i.e. prevent that your program
    processes and forwards an unreasonably large message.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 27, 2004
    #8
  9. In article <>,
    Andrew Wheeler <> wrote:
    :On Mon, 26 Apr 2004 23:42:43 +0200, Gunnar Hjalmarsson wrote:

    :> my $maxsize = 131072;

    :what is the significance of 131072 as a max size ?

    128 kilobytes = 131072 bytes.

    64 Kb is one of the magic numbers in TCP, I seem to recall;
    128 Kb allows for two buffer's worth of the maximum size.
    --
    "Meme" is self-referential; memes exist if and only if the "meme" meme
    exists. "Meme" is thus logically a meta-meme; but until the existance
    of meta-memes is more widely recognized, "meta-meme" is not a meme.
    -- A Child's Garden Of Memes
     
    Walter Roberson, Apr 28, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bernd wegener

    Piping ping into perl-prog

    bernd wegener, Sep 15, 2004, in forum: Perl
    Replies:
    3
    Views:
    660
    Jim Gibson
    Sep 22, 2004
  2. Nishi Bhonsle
    Replies:
    3
    Views:
    475
  3. Mei Li
    Replies:
    3
    Views:
    581
    Thomas Schodt
    Jul 20, 2004
  4. -intl.com
    Replies:
    1
    Views:
    371
    Martin Gregorie
    Oct 22, 2006
  5. Replies:
    6
    Views:
    229
    Peter Makholm
    Feb 10, 2010
Loading...

Share This Page