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!
|