Carp & parameters passed to subs

S

scottyj1111

Hi

I'm using Carp to write all my events to a log file. One of my scripts
connects to a serious of DB's which I do from a loop and a sub.

My problem is that when I call the sub I pass in the DB connection
details, including the password. As I'm using Carp I see the call of
the sub but I also see the parameters that I called it with (i.e. the
password)

Is there any way to get Carp to record the sub call but not the
parameters passed into it so
my DB passwords aren't recorded in the log file.

Any help appreciated.

Thanks

Scott
 
A

A. Sinan Unur

(e-mail address removed) wrote in @j33g2000cwa.googlegroups.com:
I'm using Carp to write all my events to a log file.
....

Is there any way to get Carp to record the sub call but not the
parameters passed into it so my DB passwords aren't recorded in
the log file.

Since you wrote the code (which we cannot see), why can't you change the
call so it does not write the subparameters to the log file?

Sinan
 
S

scottyj1111

Firstly sorry for not posting my code.

Secondly I didn't write it and the developer has left the company.

Basically the script calls a module called Logger.pm which contains the
following :-

use CGI::Carp qw/carpout/;
use Carp::Heavy;
use POSIX qw/strftime/;
use Time::localtime;

my $tm = localtime;
my ($DAY,$MONTH,$YEAR);
($DAY, $MONTH, $YEAR) = ($tm->mday, ($tm->mon + 1), ($tm->year +
1900));
if($DAY < 10){ $DAY = "0" . $DAY; }
if($MONTH < 10){ $MONTH = "0" . $MONTH; }
my $numdate = "$YEAR" . "$MONTH" . "$DAY";
open LOG, ">>$numdate.log" || die "Cannot open log file";
carpout(\*LOG);

The script itself goes like this :-


use strict;
use Logger;


..... stuff to work out the Oracle connection...

do_extract($ora_serv, $alias, $ora_sid, $ora_pass, $ora_user,
$ora_port);


sub do_extract {

.... stuff to connect and run queries....
}


#############################################################

What I see in my log file is :-

[Fri Mar 24 08:25:04 2006] C:\bin\tas2csv.pl:
main::do_extract(10.10.10.1, 'db1', 'sid', 'oracle', 'password', 1521)
called at C:\bin\tas2csv.pl line 59


I want to see the main::do_extract line but I don't want to see the
Oracle connection details.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @e56g2000cwe.googlegroups.com:
Firstly sorry for not posting my code.

Post code others can run with minimal effort.
Secondly I didn't write it and the developer has left the company.

OK. I don't envy you ;-)

Basically the script calls a module called Logger.pm which contains
the following :-

use CGI::Carp qw/carpout/;
use Carp::Heavy;
use POSIX qw/strftime/;
use Time::localtime;

my $tm = localtime;
my ($DAY,$MONTH,$YEAR);
($DAY, $MONTH, $YEAR) = ($tm->mday, ($tm->mon + 1), ($tm->year +
1900));
if($DAY < 10){ $DAY = "0" . $DAY; }
if($MONTH < 10){ $MONTH = "0" . $MONTH; }
my $numdate = "$YEAR" . "$MONTH" . "$DAY";
open LOG, ">>$numdate.log" || die "Cannot open log file";
carpout(\*LOG);

I would suggest replacing this gunk with:


my ($mday, $month, $year) = (localtime)[3, 4, 5];
my $log_filename = sprintf('%4.4d%2.2d%2.2d.log',
1900 + $year, 1 + $month, $mday);

open my $log, '>>', $log_filename
or croak "Cannot open '$log_filename': $!";

carpout( $log );

close $log
or croak "Cannot close '$log_filename': $!";

Now, is this really all there is in Logger.pm?

Aren't there any subroutines?
The script itself goes like this :-


use strict;
use Logger;


.... stuff to work out the Oracle connection...

do_extract($ora_serv, $alias, $ora_sid, $ora_pass, $ora_user,
$ora_port);


sub do_extract {

.... stuff to connect and run queries....
}


Please do not leave so much out that your readers have to try to figure
out which one of their umpteen guesses is correct.
What I see in my log file is :-

[Fri Mar 24 08:25:04 2006] C:\bin\tas2csv.pl:
main::do_extract(10.10.10.1, 'db1', 'sid', 'oracle', 'password', 1521)
called at C:\bin\tas2csv.pl line 59


I want to see the main::do_extract line but I don't want to see the
Oracle connection details.

Create the shortest script you can which

1) Uses Logger.pm
2) Running the script results in the kind of entries you show above in
the log file.

Then post it here if the process did not show you how to solve your
problem.

Sinan
 
P

Peter Scott

Basically the script calls a module called Logger.pm which contains the
following :-

use CGI::Carp qw/carpout/;
use Carp::Heavy; [...]
open LOG, ">>$numdate.log" || die "Cannot open log file";
carpout(\*LOG);

The script itself goes like this :- [...]
do_extract($ora_serv, $alias, $ora_sid, $ora_pass, $ora_user,
$ora_port); [...]

What I see in my log file is :-

[Fri Mar 24 08:25:04 2006] C:\bin\tas2csv.pl:
main::do_extract(10.10.10.1, 'db1', 'sid', 'oracle', 'password', 1521)
called at C:\bin\tas2csv.pl line 59

I want to see the main::do_extract line but I don't want to see the
Oracle connection details.

Presumably somewhere in Logger is calling confess(). But it is also using
Carp::Heavy for unknown reasons so maybe it's calling something in there
directly. I'll assume for the moment that it's calling confess().

There's no beginner level way to do this. I'd override
CGI::Carp::confess, which consists of:

sub confess { CGI::Carp::die Carp::longmess @_; }

and take the output from longmess, do some substitutions on it to remove
passwords, and then go on with the die.
 
J

John W. Krahn

Basically the script calls a module called Logger.pm which contains the
following :-

use CGI::Carp qw/carpout/;
use Carp::Heavy;
use POSIX qw/strftime/;
use Time::localtime;

my $tm = localtime;
my ($DAY,$MONTH,$YEAR);
($DAY, $MONTH, $YEAR) = ($tm->mday, ($tm->mon + 1), ($tm->year +
1900));
if($DAY < 10){ $DAY = "0" . $DAY; }
if($MONTH < 10){ $MONTH = "0" . $MONTH; }
my $numdate = "$YEAR" . "$MONTH" . "$DAY";

Since you are importing POSIX::strftime change the "crap" above to:

my $numdate = strftime '%Y%m%d', localtime;
open LOG, ">>$numdate.log" || die "Cannot open log file";

Because of the relatively high precedence of the || operator that will never
die. You need to either use parentheses or the lower precedence 'or'
operator. Also you should include the file name and the $! variable in the
error message.

open( LOG, ">>$numdate.log" ) || die "Cannot open $numdate.log: $!";

Or:

open LOG, ">>$numdate.log" or die "Cannot open $numdate.log: $!";



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top