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

Hello Corion,

This is actually the key point of my question. I want to find a way to pass/ inherit the value to my $firstName when the module will be called.

My intention is to use warnings on my script instead of die functions and store the error on a directory.

Sample of pseudo code Employee.pm:

So my question is how to pass the $fileDirectory into %WARNS so I can use it to print the error on correct location. I can pass it as a parameter through main.pl.

Sample of new main.pl:

#!/usr/bin/perl use Person; use strict; use warnings; use Employee; my $dirToBePassed = "/path/Dir"; my $object = new Person( "Thanos", "Test", 123456); my $firstName = $object->getFirstName(); print "This is the first name: $firstName\n"; my $secondObject = new Employee( "refToBeUsed" ); my $solution = $secondObject->somethingCalledFromMain(); print "This is the solution : $solution\n";

Sample of new Employee.pm

#!/usr/bin/perl use strict; use warnings; package Employee; sub new { my $class = shift; my $self = { refFromMain => shift, }; bless $self, $class; return $self; } sub somethingCalledFromMain { my( $self ) = @_; =comment do something here; warn "I was not able to complete the process!\n"; =cut return $self->{refFromMain}; } my %WARNS; local $SIG{__WARN__} = sub { my $message = shift; return if $WARNS{$message}++; logger('warning', $message); }; sub logger { my ($level, $msg) = @_; if (open my $out, '>>', 'log.txt') { chomp $msg; print $out "$level - $msg\n"; close $out; } } 1;

Sample of working output:

This is the first name: Thanos This is the solution : refToBeUsed

It is really easy to pass the my $dirToBePassed = "/path/Dir"; to Employee.pm as somethingCalledFromMain($dirToBePassed); with minor code modifications. The problem is how to pass this parameter to %WARNS. So I can print the warnings on the correct directory.

Thank you for your time and effort reading and replying to my question.

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

Replies are listed 'Best First'.
Re^3: Inheritance without defining the object on inherited module
by Corion (Patriarch) on May 18, 2015 at 10:55 UTC

    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.

      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; };
Re^3: Inheritance without defining the object on inherited module
by Anonymous Monk on May 18, 2015 at 11:07 UTC

    Its simple :P use Log::Log4perl / Log::Log4perl::FAQ

    Does this make sense ?

    Employee::logger_outfile( "/path/Dir" ); ... ##Employee.pm my $outfile ; sub Employee::logger_outfile { my $path = shift; croak "This is not a method" if ref $path or int@_; $outfile = $path; } sub Employee::logger { open ... $outfile ... }

      Hello Anonymous Monk,

      That is probably what exactly I need.

      Thanks for your time and effort.

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