Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Inheritance without defining the object on inherited module

by Corion (Patriarch)
on May 18, 2015 at 09:16 UTC ( [id://1126962]=note: print w/replies, xml ) Need Help??


in reply to Inheritance without defining the object on inherited module

... our @ISA = qw(Person); # inherits from Person my $firstName = $object->getFirstName(); print "This is the first name: " . $firstName . "\n";

Can you explain in English what this code is supposed to do and when it is supposed to be executed?

The code is not in a subroutine so it will run at use time.

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

    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!

      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!

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found