Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First script, output

Status
Not open for further replies.

jpillonel

Programmer
Dec 17, 2003
61
CH
Hello,

This is my first script in perl. I need some help to create a logfile of some lines executed.

FOr this I do:

#!C:/Perl/bin/perl.exe -w
#*********************************************************
# Fonction: Nettoyage de logs *
# *
# Usage: Script utilise les paramètres dans le script. *
#*********************************************************
#
# Creation: 26.09.2005 JFP
#

# Parametres du script
$retention = 12; # Nbre de jours en entiers
$log_folder= 'c:/dev/perl/LOG'; # Répertoire à nettoyer
$file_genname = 'DailyMpgateway'; # Nom générique du fichier

# Definition de l'heure
my ($secondes, $minutes, $heures, $jour_mois, $mois,
$an, $jour_semaine, $jour_calendaire, $heure_ete) = localtime(time);
my $CTIME_String = localtime(time);

# Fichier de log du script
open WRITER,">> decode.txt" or die "Le fichier ne peut être édité !\n";


# Entete du fichier de log
print WRITER "------------------------ DEBUT ------------------------ \n";
print WRITER " Script start at: $CTIME_String \n";



# Affiche les informations sur l'environnement
print "\n \n \n";
$machine = Win32::NodeName;
print "Script start on: $machine \n";
($string, $major, $minor, $build, $id) = Win32::GetOSVersion();
@os = qw(Win32s, Win95, WinNT);
print WRITER "With OS Version : $os[$id] $major\.$minor $string (Build $build)\n";


# Affiche le répertoire de départ du script
$currpath = Win32::GetCwd();
print WRITER "Cuurent directory is $currpath \n";

# Changement de répertoire
chdir("$log_folder") || die "cannot cd to $log_folder ($!)";
$currpath = Win32::GetCwd();
print WRITER "Successfully changed to folder: $currpath \n \n \n";

# Affiche les logs présents
@all_logs = glob("$file_genname.log.*");
print WRITER "List of Logs present on folder: \n";
foreach (@all_logs) {
next unless -f;
next unless -r;
$age = -M;
print WRITER "Log: $_ Date: $age \n";
}

# Affiche puis supprime les logs
print "\n \n";
opendir(DIR,'.') or die "$!";
my @logs_todelete = grep(/^$file_genname\.log\./ && int -M > $retention, readdir DIR);
close(DIR);
print WRITER "\n Files to be deleted:\n";
print WRITER "$_\n" for @logs_todelete;


print WRITER " Script finished at: $CTIME_String \n" ;
print WRITER " ------------------------ FIN --------------------------- \n";
close WRITER;

Is there any way to have someting more visible like always using "print WRITER" ???

And is there a way to know how many time the script takes ?
 
select WRITER; ... will redirect output to the WRITER filehandle until you select STDOUT (be careful with this)


Kind Regards
Duncan
 
Nice work for a first script!

Your timing is easy:
Code:
my $start_time = time();
# do something time-consuming here
printf( "That bit took %d seconds\n", time()-$start_time );

I'm not sure that I understand your first question, but I think that you are finding it boring to type [tt]print WRITER[/tt] all the time. There are a number of ways round this. You could simply replace
Code:
open WRITER,">> decode.txt" or die "Le fichier ne peut être édité !\n";
with
Code:
open STDOUT,">> decode.txt" or die "Le fichier ne peut être édité !\n";
and then everything you print goes to WRITER. One problem is that, should you then want to write to STDOUT, you've lost it. The [tt]select[/tt] function is your friend here:
Code:
open WRITER,">> decode.txt" or die "Le fichier ne peut être édité !\n";
select WRITER;
print "This to decode.txt\n";
print "...and this\n";
select STDOUT;
print "This to the screen\n";
print WRITER "You can intermix the techniques\n";
print "This is still to the screen\n";

I find [tt]select[/tt] great unless I've got a lot of function calls with printing in each. It then gets difficult to ensure that the correct [tt]select[/tt] is still in force so explicit handles regain value.

HTH,

fish


["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top