in reply to search replace (interpolation)

What you're doing wrong is using symbolic references - read the excellent archived newsposts by Dominus, part one, two and three on Why it's stupid to 'use a variable as a variable name'.

You probably want to use a hash, or maybe hard references, instead (and use strict as well, of course). If you tell us what exactly you're trying to do, we might be able to propose something better suited.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: search replace (interpolation)
by Anonymous Monk on Dec 16, 2002 at 20:14 UTC
    What I am trying to do is replace the envelope headers of a email address dynamically.

    The psuedo is as follows:

    Read email template with tokens in the envelope headers:
    Message-ID: <1039751337.22128@idhost> Content-Transfer-Encoding: 8bit Content-Type: multipart/alternative; boundary="_----------=_1039751337 +22128" MIME-Version: 1.0 Date: Thu, 12 Dec 2002 19:48:57 PST From: __FULLFROM__ <__FROM__> To: __FNAME__ __LNAME__ <__EMAIL__> Subject: __SUBJECT__ Precedence: bulk

    I now break up the email template into two parts. 1 is a hash containing all the header information the other is a variable with the body.

    I then want to hit a db and dynamically replace the headers with the fields in the db and send out the email.

      So put the information you retrieve from the database in a hash with the keys FULLFROM, FROM, FNAME, LNAME, EMAIL, and SUBJECT.
      my %header_line = read_header_or_something($mail_file); while(my %recipient_info = get_info_from_db()) { while(my ($header, $value) = each %header_line) { $value =~ s/__(.+?)__/$recipient_info{$1}/g; print "$header: $value\n"; } }

      And I hope you're not sending the mail to people who haven't opted in on their own.. :)

      Update: doh, silly me for forgetting to mention you would be better off with a templating system. Unlike UnderMine, I would propose the more generic and powerful Template Toolkit though.

      Makeshifts last the longest.

        This sounds like a perfect case for using a standard templating module. Using say HTML::Template you end up with very simple template and perl coding.
        Message-ID: <1039751337.22128@idhost> Content-Transfer-Encoding: 8bit Content-Type: multipart/alternative; boundary="_----------=_1039751337 +22128" MIME-Version: 1.0 Date: Thu, 12 Dec 2002 19:48:57 PST From: <!-- TMPL_VAR NAME=FULLFROM --> <<!-- TMPL_VAR NAME=FROM -->> To: <!-- TMPL_VAR NAME=FNAME --> <!-- TMPL_VAR NAME=LNAME --> <<!-- TM +PL_VAR NAME=EMAIL -->> Subject: <!-- TMPL_VAR NAME=SUBJECT --> Precedence: bulk
        then the perl is trivial
        while(my %recipient_info = get_info_from_db()) { my $template = HTML::Template->new(filename => 'email.tmpl'); $template->param(%recipient_info); print $template->output(); }
        Hope this helps
        UnderMine
        thanks all for the help.

      This would be a hell of a lot more robust, maintainable and less obfuscated.

      my $email_template = get_email_template_as_string(); my $sql = 'SELECT first_name, last_name, email FROM address_book'; my $sth = $dbh->prepare_cached{$sql} or die 'Error'; while ( my ($first_name, $last_name, $email) = $sth->fetchrow_array() +) { my $text = $email_template; $text =~ s/__FNAME__/$first_name/; $text =~ s/__LNAME__/$last_name/; # contiue with substitutions send_email($text); }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print