Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: backtick operator

by Anonymous Monk
on May 13, 2010 at 21:32 UTC ( [id://839908]=note: print w/replies, xml ) Need Help??


in reply to Re: backtick operator
in thread backtick operator

Yeah, I thought about that and completely agree with you - I was just reusing that ksh script someone else had written - I replaced that with:
sub dbConnect(){ my ($dbName) = @_; LogStatus "Retrieving $dbName login info"; my ($dbUser, $dbPass) = get_db_login($dbName); LogStatus "User for $dbName is $dbUser"; die "$0: Could not find password for $dbUser" unless(defined($dbPas +s)); my $dbh = DBI->connect("dbi:DB2:$dbName", $dbUser, $dbPass, {AutoCo +mmit=> 0, RaiseError => 1}) || die "Can't connect to $database: $DBI::errstr"; LogStatus "Connected to $dbName as $dbUser"; return $dbh; } sub get_db_login { my ($dbName) = @_; my ($dbUser, $dbPass, $whoami); chomp($whoami = `whoami`); open(PASSWD,"/home/$whoami/.passwd") or die "~/.passwd does not ex +ist"; while(<PASSWD>){ if($_=~/^$dbName\s+(\w+)\s+(\w+)/i){ $dbUser = $1; $dbPass = $2; } } close PASSWD; return $dbUser, $dbPass; }

Unfortunately, I am still getting the same behavior as I never see the result of:

LogStatus "User for $dbName is $dbUser";

Replies are listed 'Best First'.
Re^3: backtick operator
by AndyZaft (Hermit) on May 13, 2010 at 22:03 UTC
    Hmm, but it doesn't die when opening the .passwd file? That's weird, time to debug that get_db_login sub line by line right after the open statement. If for some reason you don't get anything from the file then your while loop would never get executed. Btw, you can use $ENV{USER} instead of `whoami`, one less system call :) I assume it is not dying from
    die "$0: Could not find password for $dbUser" unless(defined($dbPass)) +;
    since the $dbPass will be defined as an empty string at that point, right?

      Found the problem - I needed to increment $| to autoflush. I had a long running sql statement after the call to dbConnect that made it seem as if it was hanging.

      I added the following:

      $|++;

      after

      #!/usr/bin/perl
        FWIW, always set $| to the value you want, ie on $|=1; or $|=0; off, because $|++; $|++; turns it on, then turns it off.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://839908]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found