Hi Monks!
I have made a script with Perl and Mysql, executing queries. The script works fine, the thing I would like to change -if possible- is to not need to write the credentials all the time, but only when I first execute it, and then somehow they remain stored until I logout or something.
My script looks as follows:
use strict; use warnings; use Term::ReadKey; use DBI; use List::Util qw( min max ); use Data::Types qw/:all/; use DateTime; use DateTime::Format::Strptime; use Date::Calc qw(:all); my $dates_file = $ARGV[0]; my $dsn = "DBI:mysql:DiabetesDB"; print "Please give your username:"; my $username = <STDIN>; chomp $username; ReadMode(0); print "Password for user \"$username:\""; ReadMode('noecho'); my $password = ReadLine(0); ReadMode 'normal'; chomp $password; # connect to MySQL database my %attr = ( PrintError=>0, # turn off error reporting via war +n() RaiseError=>1 # turn on error reporting via die( +) ); my $dbh = DBI->connect($dsn,$username,$password, \%attr); print "\nUser \"".$username."\" is connected to the \"DiabetesDB\" dat +abase.\n"; my ($patient_id_date, $specific_date, $wanted_date, $type_of_search, $ +max_window, $sql_query)=''; open DATES, $dates_file; while(<DATES>) { chomp; if($_=~/^(.*?)\t(.*?)\t(\d+)\t(.*)/) { $patient_id_date=$1; $specific_date=$2; $max_window=$3; $type_of_search=$4; my %p; @p{qw(year month day)} = split /\//, $specific_date; my $dt = DateTime->new(%p); my $wanted_date = $dt->clone->add(months => $max_window)->strf +time('%Y/%m/%d'); #this is the date I am interested in, +X months if($type_of_search eq 'past') { $sql_query = "SELECT * FROM MEASUREMENTS WHERE MEASUREMENT +S.patient_id='".$patient_id_date. "' AND MEASUREMENTS.measure_date<'".$wanted_d +ate."' ORDER BY MEASUREMENTS.measure_date DESC LIMIT 1"; } elsif($type_of_search eq 'future') { $sql_query = "SELECT * FROM MEASUREMENTS WHERE MEASUREMENT +S.patient_id='".$patient_id_date. "' AND MEASUREMENTS.measure_date>'".$wanted_d +ate."' ORDER BY MEASUREMENTS.measure_date ASC LIMIT 1"; } elsif($type_of_search eq 'both') { $sql_query = "(SELECT * FROM MEASUREMENTS WHERE MEASUREMEN +TS.patient_id='".$patient_id_date. "' AND MEASUREMENTS.measure_date<'".$wanted_d +ate."' ORDER BY MEASUREMENTS.measure_date DESC LIMIT 1)". " UNION (SELECT * FROM MEASUREMENTS WHERE ME +ASUREMENTS.patient_id='".$patient_id_date. "' AND MEASUREMENTS.measure_date>'".$wanted_d +ate."' ORDER BY MEASUREMENTS.measure_date ASC LIMIT 1)"; } my $sth = $dbh->prepare($sql_query); $sth->execute(); $sth->dump_results( ); $sth->finish(); print "\n"; } } close DATES;

So, as you can see, the user is asked to provide his/her credentials when calling the script. I was wondering if there is a way so that the user does it once, but if they re-run the script 2 mins later, they can still be "logged in" on the Mysql server. Is that possible, and, if yes, how does one go about it?

Thanks!

In reply to can one create a "session" of Perl & Mysql? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.