English 中文(简体)
书写永久存档
原标题:Writing subroutine to log file in perl
  • 时间:2013-07-12 09:36:00
  •  标签:
  • perl

I am new to perl scripting. I want to write subroutine test to log file. for e.g.

my ($logfile, $logpath);
$logpath =  /usr/bin ;
$logfile = "$logpath/log.txt"; 
open (LOG,">>","$logfile") || die ("Error : can t open log file");

sub test
{
   print "Hi
"; 
   my $date = `date`;
}

sub logFunc
{
   print LOG "Writing log files
";
   print LOG test(); # we cannot do like this :)
}

logFunc();

Say their are 15+ subroutines. So to write commands in each subroutine to log file I have to write print LOG "[Command] "; which works fine but script length is huge. So using common subroutine is their any way to achieve this?

最佳回答

你的法典有几个问题。

  1. 您是否(和希望)以书面形式进入<代码>/usr/bin/?

  2. You don t ever call your log() or your test() subroutines. No one will call any of them automatically.

  3. The name log clashes with the built-in log function. So you will either have to call it with a prepended ampersand &log() which is ugly or rename it.

  4. 页: 1 相反,明确退还<代码>日的价值。

  5. 你们正在利用一个空档全球档案处理,使用<代码>开放<>/代码”的2-argument版本。 请使用3-arg版本,并附有一套弹性文件:开放我的博客_fh, >> logfile

问题回答

几个方面:

  • Always add use strict; and use warnings; at the top of your script.
  • Since you re dealing with reading and writing files, you should also add use autodie;. This will automatically kill your program if you cannot open a file, or you cannot write to an open file.
  • Don t use OS commands when Perl probably can do exactly what you want without calling an OS command.
  • A Subroutine usually takes arguments and returns a value of some sort. In your case, have your test subroutine return something to write to the log. Or, create just a log subroutine that writes to a log, and have your test subroutine call it.

在此,我推翻了您的传教。 我创设了一个<条码>write_to_log,作为处理我的路程电话的常规。 www.un.org/Depts/DGACM/index_chinese.htm 现在我的各种次路线仅叫write_to_log

我所有传道的通知都有某种价值。 www.un.org/chinese/ga/president 我可以利用这一方法检验我对我的非例行呼吁是否奏效。

use strict;
use warnings;
use autodie;
use features qw(say);   #Allows you to use `say` instead of `print:

my $log_file = "/usr/bin/log.txt";    #You have write permission to this directory?

open my $log_fh, ">", $log_file;

my test ( $log_fh ) or die qq(Can t write to the log);   #Pass the file handle to log
my test2 ( $log_fh ) or die qq(Can t write to the log);

close $log_fh;

sub test {
    return write_to_log ( $log_fh, "Hello World!" );
}
sub test2 {
    return write_to_log ( $log_fh, "Goodbye World!" );
}

sub write_to_log {
    my $file_handle  = shift;
    my $message      = shift;

    use Time::Piece;

    my $time = localtime->cdate;
    return say {$file_handle} "$time: $message";
}

http://modernperl.net/books/evaluating-a-good-perl-book.html 该书列出了学习优良书籍modern Perl,以及这些书本应研究的内容。 如果你开始学习Perl,则使用其中一本书。

第2024和PERL号主要为像我这样的人所使用,他们有时不得不研究某些遗产法典。 我们往往没有时间、耐心或能够安装单元。 仅使用基本垂直指挥的简单功能:

#Print based on loglevel 
sub log {
    my %LOG_LEVELS=( "ERROR"=>0, "WARNING" =>1 , "INFO"=> 2, "DEBUG"=> 3);
    printf("[$_[0]] $_[1]
") if $LOG_LEVELS{uc($loglevel)}+0 >= $LOG_LEVELS{uc($_[0])}+0;
}

并且称职

在本案中,我通过一个百分之十

my $loglevel="INFO"; #default
#parse command-line vars 
    foreach $arg (@ARGV){
        chomp($arg);
        ($key,$val) = split(/=/,$arg,2);
        if ($key eq "url"){ $url = $val; }
        if ($key eq "loglevel"){ $loglevel = $val; }
    
    }

然后将职能与级别和讯息联系起来。

&log("INFO", "Calling script to generate result");
$result=`blahblah`;
if (index($result,"OK") == -1) {
    &log("ERROR", "Generate role FAILED with status $result");
}
else
{
    &log("DEBUG", "Sucessfully created ".$result);
}

它处理每一个案件,但你可以很容易地在LOG_LEVELS阵列中增加更多数量/较少的数量。

需要增加更多的验证,但已经处理过不同的案件:在有机农业组织中,信息信息是完全有效的。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签