carcassonne has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Is there a way to use text data stored at the end of a Perl file in the context of a POE::Component::Logger, like this:

POE::Component::Logger->spawn(ConfigFile => 'log.conf')

Can the log configuration (text) found in log.conf be at the end of the same program file, past the __DATA__ (or __END__) statement and if so, how to tell POE::Component::Logger ?

Thanks.

UPDATE

It is possible to store the configuration file in the same file as the program file and then, to use it before launching the program.

The configuration has to go at the end of the program file, after the __DATA__ marker. Then when program starts, this data is written in /tmp and the program uses it.

setupConfig(); POE::Component::Logger->spawn(ConfigFile => '/tmp/mainlog.conf', Alias + => "mainlog"); sub setupConfig { local $/; my $config = <DATA>; open (LOGCONF, ">/tmp/mainlog.conf"); print LOGCONF $config; close LOGCONF; } __DATA__ dispatchers = file file.class = Log::Dispatch::File file.min_level = debug file.filename = "/tmp/mainlog.log" file.mode = append file.format = [%d] [%p] %m %n

Replies are listed 'Best First'.
Re: Using data at the end of a program file
by MrFlibble (Initiate) on Feb 20, 2006 at 20:08 UTC
    I'm not sure what POE::Component::Logger wants, but I suspect that it's a configuration file on it's own; in that case, it's a bit difficult just to point it at the calling script, since I don't think you can easily tell it to "open this file, but only start from after '__END__'".

    However, you could create a temporary file from a "here" document:

    open TEMPFILE, ">$temp_filename"; print << END_OF_HEREFILE .... ... write your config file stuff here .... ... END_OF_HEREFILE close TEMPFILE; POE::Component::Logger->spawn(ConfigFile => $temp_filename); ... unlink ($temp_filename);
    Remember you need to add in the unlink somewhere to delete the tempfile; you need to decide where it should go depending on your code.
      Thanks, that's just about the right solution, apart from one little thing. But first, a correction:

      print << END_OF_HEREFILE

      should be (if not, the Perl I'm using will say it's deprecated) :

      print TEMPFILE <<END_OF_HEREFILE;

      And then, this approach, as it is, will have the print statement print out the following warning at the console:

      /tmp/log1.conf: No such file or directory

      Up to now I cannot get rid of these messages. Creating a file beforehand like this does not help:

      open(LOG1, ">/tmp/log1.conf"); print LOG1 "test"; close LOG1;

      I think it's because the file does not get actually created on the disk. Not flushed or something. Because if files are simply 'touched' at the console before running the code, the print warnings are not displayed.

      There must be a Perl way of doing this ! ;-)