in reply to Re^2: Inheritance without defining the object on inherited module
in thread Inheritance without defining the object on inherited module

Your problem does not show anything related to inheritance - the code shows no relation between Person and Employee.

If you want to do a global capturing of all warnings raised by code in the Employee class, you will need to store the "current" Employee instance in a global variable and then reuse that variable in your %SIG handler for warnings. The easiest approach is to set the "current" employee in the main program:

local $Employee::current= $secondObject; $secondObject->doSomethingThatRaisesWarnings();

Note that your statement of

local $SIG{__WARN__} = sub { ... }

loses its effect as soon as Employee.pm has been compiled. This may or may not be what you want.

Personally, I would recommend to avoid capturing warnings in such a global way. I think it's better to have your Employee (or Person) class implement a ->logMessage() or ->warn() method that will store the warning with the employee.

Replies are listed 'Best First'.
Re^4: Inheritance without defining the object on inherited module
by thanos1983 (Parson) on May 18, 2015 at 19:02 UTC

    Hello again Corion,

    That is true the sample code that I have provided it does not explain exactly so I will try to explain in English.

    I want to use a subroutine from Person to Employees. More precisely I want to invoke a subroutine input main.pl</p> that calls a method from <code>Person and replies back with a hash. I want to invoke the same subroutine from Employees without the need to use the object.

    My goal the whole time was to get a hash with the directories where all my %WARNS will be stored in different folders in case of an error.

    I hope now that I explained everything in English that it makes a bit more sense why I wanted to use both Perl Modules.

    Again thank you for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      You cannot (or rather, should not) use code as both, a function and a method. Using inheritance is the bad approach in any way.

      If you want to call the subroutine in Person from Employee, just call it directly:

      package Person; my %WARN; sub get_warnings { return \%WARN; };
      package Employee; use Data::Dumper; sub frobnicate { my $warnings_by_person = Person::get_warnings; warn Dumper $warnings_by_person; };