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

Dear monks,

I am going through a Perl code and found this line. Please help me understand this.


 $__PACKAGE__::mylog=Log::InLog->get_logged();
Here get_logged is a method of class?? Inlog? Log? and why the whole stuff is assigned to _PACKAGE_::mylog?? Y identifier has underscores? Is it just to recongnize the class name or it indicates something special?

Replies are listed 'Best First'.
Re: Calling a method from class / subclass?
by merlyn (Sage) on Oct 05, 2007 at 14:37 UTC
    I don't believe this code will do anything useful. It will assign a value to a variable literally named $__PACKAGE__::myLog, which would be a very strange thing, as it would be no different from assigning to (again picking an arbitrary string) $__WOOHOO__::myLog. It's a variable in the package called __PACKAGE__, not a variable in the current package.

    __PACKAGE__ as a special token works only when it is not part of a larger string or construct. Perhaps what they really wanted was:

    { no strict; ${__PACKAGE__ . "::mylog"} = ...; }
    which would make more sense, making $mylog in the current package set to a value.
    Update: Of course, I now realized I could have simply used ${"mylog"} = ... for that assignment. What I did was overkill. :)
Re: Calling a method from class / subclass?
by arkturuz (Curate) on Oct 05, 2007 at 08:44 UTC
    get_logged() is a method of InLog which can be a subclass of Log but not neccesarily since we don't have the rest of the code.

    Basically, this line assignes to a current package variable named $myLog the value of Log::InLog->get_logged()

    For more information read the following documentation: perlmod and perlboot

    Update: see merlyn's explanation here Re: Calling a method from class / subclass?

    Time for me to buy a better glasses :)

Re: Calling a method from class / subclass?
by Gangabass (Vicar) on Oct 05, 2007 at 08:32 UTC

    Perl gives us the ``__PACKAGE__'' constant which contains the name of the current package.

Re: Calling a method from class / subclass?
by moritz (Cardinal) on Oct 05, 2007 at 09:19 UTC
Re: Calling a method from class / subclass?
by Anonymous Monk on Oct 05, 2007 at 08:49 UTC
    One addition:  $__PACKAGE__::mylog=Log::Log4perl->get_logger();

    In this line of code; Log::Log4perl is a module of CPAN that generates the log files during runtime
    Now as _PACKAGE_ contains the current package name then what mylog ($PACKAGE::mylog) is for. Is this for calling a variable named mylog from class in _PACKAGE_? Again, why Log::Log4perl is assigned to _PACKAGE_::mylog?? What does this mean?

      Quote: what mylog ($PACKAGE::mylog) is for

      To store the value you need variable name: that's $mylog. To store it in the current package, you can use $__PACKAGE__::mylog.

      Quote: Again, why Log::Log4perl is assigned to _PACKAGE_::mylog?? What does this mean?

      It could mean a number of things. One of them could be that author of the code wants a package global variable (variable that is global in the package defined) to be accessed from outside of the package.