Logging with Log::StdLog from within a package

J

Jerry Krinock

Today, I separated out a file of functions into a separate package:

package MyPackage ;

Instead of 'use', I 'require and call the functions using

MyPackage::aFunction() ;

It works fine except for my Log::Stdlog calls such as:

print {*STDLOG} info => "Hello\n" ;

None of any such calls from inside package functions print to my log
any more. They "just don't work".

A confession: I've come a long way with Perl, but one thing still
beyond my comprehension is the 'tie' function with its "enchanted"
variable that seems to be the magic behind {*STDLOG}. What does the
asterisk do?

How can I make my logs work inside my package?

Thanks very much,

Jerry Krinock
 
B

Ben Morrow

Quoth Jerry Krinock said:
Today, I separated out a file of functions into a separate package:

package MyPackage ;

Instead of 'use', I 'require and call the functions using

MyPackage::aFunction() ;

It works fine except for my Log::Stdlog calls such as:

print {*STDLOG} info => "Hello\n" ;

None of any such calls from inside package functions print to my log
any more. They "just don't work".

Global filehandles are package scoped, so if you opened that STDLOG
filehandle in package main, then you will need

print {*main::STDLOG} info => "Hello\n";

Alternatively, you can 'use Log::StdLog' within your package MyPackage
as well (or instead); this will reopen the logfile but that shouldn't be
a problem.
A confession: I've come a long way with Perl, but one thing still
beyond my comprehension is the 'tie' function with its "enchanted"
variable that seems to be the magic behind {*STDLOG}. What does the
asterisk do?

The {* } is just Damian being paranoid :). Normally that would be
written

print STDLOG info => "Hello\n";

but if you have a sub or a package called STDLOG that statement is
ambiguous, and it's far from obvious how perl resolves that ambiguity.

print {*STDLOG} ...

is just a way of saying 'yes, I really mean the builtin 'print' and the
filehandle 'STDLOG', whatever other ideas you might have'. The fact
STDLOG is tied is not relevant here: you could just as well say

print {*STDERR} ...

if you wanted to.

Ben
 
J

Jerry Krinock

Thank you, Ben. I used {*::STDLOG} and it works now.

But what is that {*FILEHANDLE} syntax mean to the perl interpreter? I
thought that there is no "*" operator in perl, and {} is used for code
blocks or hash assignments. Which, if any, perldoc?

Jerry
 
B

Ben Morrow

Quoth Jerry Krinock said:
Thank you, Ben. I used {*::STDLOG} and it works now.

But what is that {*FILEHANDLE} syntax mean to the perl interpreter? I
thought that there is no "*" operator in perl, and {} is used for code
blocks or hash assignments. Which, if any, perldoc?

*FOO is described in perldata, under 'Typeglobs and Filehandles', and
further elaborated on in perlmod. The print {...} syntax is documented
in perldoc -f print; it applies quite generally to any builtin or method
call that has an 'indirect object' slot. The block is required if you
want to put anything more complicated that a simple scalar there.

Ben
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top