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

Hey Friends,
I've been banging out a series of Perl scripts to support my LUG webpage. Tonight's quicke was a script that accesses a CGI-generated database of subscribed users, loads the email addresses supplies, and allows me to send a batch email out to everyone concerning meetings, etc. I think the way I did this was horribly inefficient, but I can't seem to dredge my thoughts right now for a better way. This is what I have:
use Mail::Send; ...time passes, children are born... dbmopen(%CONTACT, "/httpd/userDBASE/contact", 0775) or die "Couldn't open DBASE: $!"; for $member (keys %CONTACT) { $CONTACT{$member} =~ /^\b(\S+)\b\s/; $msg = new Mail::Send( Subject => $subject, To => $1); open MSGBODY, "$file" or die "Couldn't open $file: $!"; $fh = $msg->open; while(<MSGBODY>) { print $fh "$_"; } $fh->close; close(MSGBODY); }
Can you see my problem? It lays in the fact that I have to open and close the filehandle containing the message to start at the beginning of the file for each mail send. Otherwise, only the first message has content, and all the others are blank! I can't seem to google for an action I can take to reset the 'file pointer' to the first character in the file. If I recall from years past, C++ has this capability.

Input is, as always, treasured.

~John

Replies are listed 'Best First'.
Re: Repeated File Opens Inefficient?
by robartes (Priest) on Mar 20, 2003 at 07:46 UTC
    I'd say simply store the message body in a variable outside of your loop, and print that to your mail:
    my $body; { local $/=undef; open MSGBODY, "<$file" or die "Could not open $file: $!\n"; $body=<MSGBODY>; # slurp message body close MSGBODY; } for $member (keys %CONTACT) { ... print $fh $body; ... }
    Or am I missing something here?

    CU
    Robartes-

Re: Repeated File Opens Inefficient?
by PodMaster (Abbot) on Mar 20, 2003 at 07:45 UTC
    Why don't you just slurp the message into a variable?

    So you're looking for a perl function ..... see `perldoc perlfunc' (esp "Input and output functions")


    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.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Repeated File Opens Inefficient?
by perlguy (Deacon) on Mar 20, 2003 at 07:51 UTC
    I agree with the assessment of the previous 3 posters, but for a perl function that you can 'point' to the beginning of a file, or to any location in the file for that matter, check out perl's seek function.
Re: Repeated File Opens Inefficient?
by rjray (Chaplain) on Mar 20, 2003 at 07:48 UTC

    This is a simple problem. Just read the message body once, before your loop. Save it into a scalar, like so:

    open MSGBODY, "$file" or die "Couldn't open $file: $!"; $message = join('', <MSGBODY>); for $member (keys %CONTACT) { ... $fh = $msg->open; print $fh $message;

    If you have to do some per-user processing of the message, then look into some of the text templating systems. (I personally recommend Template::Toolkit.)

    --rjray