The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
Please vote in the Forum Structure Poll. Polling will close at 2PM EST on January 21, 2025.

Perl and SQL question [OP UDPATED]

naporeonnaporeon Seattle, WARegistered User regular
edited January 2009 in Help / Advice Forum
Thanks for the suggestions, guys. It's some serious Frankencode, but I've got the script configured to use a separate text file as the body of the email it sends. I do have one last issue, however:

How do I configure it so that the email also displays three of the variables defined by the script? Basically, I've got "var1", "var2", and "var3", which are all defined by the user in the course of the script running. Ultimately, I want those user-defined variables to display in the notification email sent by the script.

I have a feeling that this is a super simple thing, but again...lost. As you can probably tell, my grasp on the terminology is so tenuous that it's difficult for me to even frame the question cogently. Bah.

OLD QUESTION:
So, I just got a "throwaway" assignment at work. The problem is, it's not something I'm familiar with. At all. For the last several months, I've been writing PHP to interact with various Postgres pages, and today I was told to do something with Perl. I've got virtually no experience with Perl, and have been learning as I go with Postgres and PHP anyhow, so I'm pretty much lost right now.

The assignment is to write a Perl script that:
  • Creates a new user on a database.
  • Sends that user an email, notifying them of the hostname, their username, and their password.
  • Uses as the body of that email a text file template that can be edited independently.
It was possibly just a flip comment, but woman who gave me this assignment said that it could be done with "ten lines of actual code". Perhaps it can. I'm sure that with a psql command, it would be pretty quick. I know it's simple, but I am clueless where to start.

A straight-up answer would be fine, but isn't necessary. Just being pointed to a specific resource that can help walk me through this would be super.

Thanks.

naporeon on

Posts

  • LewishamLewisham Registered User regular
    edited January 2009
    Don't know enough about psql to help, but yeah, sounds like you want to:

    - take a username on the command-line
    - connect to the DB
    - run the SQL command to create a new user, with the password something random
    - read in an email to send (I'd use HTML::Template for this. It doesn't have to be HTML it templates... I used it for emails all the time)
    - send an email (MIME::Lite is probably the winner here)

    10 lines is an exaggeration (but "of actual code" might be right I guess. Depends what you exclude...), but you could do it in less than 100.

    Lewisham on
  • Jimmy KingJimmy King Registered User regular
    edited January 2009
    Looks simple enough. For the db stuff, either use the DBI module and run the sql commands to create the user or, if you're sure nothing is going to change later to break this, pgsql probably has something similar to the mysqladmin tool from the command line using system() from perl. I am assuming you mean a new pgsql user here, not a new user for whatever system this db is for.

    For the template, as Lewisham suggested, HTML::Template is great. The other template system I commonly use is Template-Toolkit (just Template.pm, so from cpan you use 'install Template' even though due to the name, your first thought is "install Template::Toolkit").

    For sending an e-mail, all I've ever used is Net::SMTP. It's pretty simple to use. MIME::Lite may be better, though, I've never used it and it's been years since I needed to write a perl app that sent an e-mail.

    edit: now it makes sense because I don't keep referencing mysql

    Jimmy King on
  • naporeonnaporeon Seattle, WARegistered User regular
    edited January 2009
    Thanks for the suggestions, guys. It's some serious Frankencode, but I've got the script configured to use a separate text file as the body of the email it sends. I do have one last issue, however:

    How do I configure it so that the email also displays three of the variables defined by the script? Basically, I've got "var1", "var2", and "var3", which are all defined by the user in the course of the script running. Ultimately, I want those user-defined variables to display in the notification email sent by the script.

    I have a feeling that this is a super simple thing, but again...lost. As you can probably tell, my grasp on the terminology is so tenuous that it's difficult for me to even frame the question cogently. Bah.

    naporeon on
  • BEAST!BEAST! Adventurer Adventure!!!!!Registered User regular
    edited January 2009
    html::template sounds like overkill to me but if you want to use that, the body would look something like.

    "Here is the body of my email, <TMPL_VAR var_1> is the best variable, <TMPL_VAR var_2> is the second best, etc etc etc"

    and then you'd create a new HTML::Template object, reading in the body, like:
    my $template = HTML::Template->new(scalarref=>\$emailBody)
    

    and set the params like
    $template->param(var_1 => $var_1);
    

    and finally use $template->output(); to output your text


    Personally though, i would just use a regular expression since this doesn't seem like a huge template.

    Use something like <var_1> to denote where your variable will be in the body, and then do
    $body =~ s/<var_1>/$var_1/gi;
    

    The first <var_1> matches that text, and replaces it with the $var_1 which you've defined elsewhere in the code

    BEAST! on
    dfzn9elrnajf.png
Sign In or Register to comment.