Logging with Log::StdLog from within a package

Discussion in 'Perl Misc' started by Jerry Krinock, Jul 31, 2008.

  1. 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
    Jerry Krinock, Jul 31, 2008
    #1
    1. Advertising

  2. Jerry Krinock

    Ben Morrow Guest

    Quoth 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".


    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

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    Ben Morrow, Jul 31, 2008
    #2
    1. Advertising

  3. 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
    Jerry Krinock, Jul 31, 2008
    #3
  4. Jerry Krinock

    Ben Morrow Guest

    Quoth 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?


    *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

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." [C.S.Lewis]
    Ben Morrow, Jul 31, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dean Schulze
    Replies:
    5
    Views:
    8,734
    Dean Schulze
    Jan 18, 2006
  2. unomystEz
    Replies:
    0
    Views:
    542
    unomystEz
    Nov 19, 2006
  3. Replies:
    1
    Views:
    438
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jun 3, 2007
  4. Royan
    Replies:
    1
    Views:
    1,431
    Andrea Francia
    Apr 1, 2008
  5. Use UTF-8 in Log::StdLog?

    , Jul 16, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    81
    Jerry Krinock
    Jul 20, 2007
Loading...

Share This Page