in reply to (in cleanup) Can't call method "close" on an undefined value at somePerlModule.pm line number.

This is how you fix that
#sub DESTROY { # my $class = shift; # return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; # $class->{handle}->close(); #}
  • Comment on Re: (in cleanup) Can't call method "close" on an undefined value at somePerlModule.pm line number.
  • Download Code

Replies are listed 'Best First'.
Re^2: (in cleanup) Can't call method "close" on an undefined value at somePerlModule.pm line number.
by thanos1983 (Parson) on Jan 30, 2015 at 12:39 UTC

    Hello Anonymous Monk,

    Thank you for your time and effort reading and replying to my question. Well apart from commending the destructor is there any other way to fix it?

    As I said this is the first time that I am using OO programming and I am trying to learn.

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

      thanos1983:

      If you just want to fix that line, you can do it like this:

      sub DESTROY { my $class = shift; return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; $class->{handle}->close() if defined $class->{handle}; }

      However, as mentioned elsewhere, $class->{handle} is *only* used in your destructor, so it's not really a useful line of code. Then, going backwards through the destructor, there's nothing happening that needs to be done. So removing the destructor would be the correct solution. But since you said it's a sample of your code, you might've removed a subroutine or two that could potentially set the handle member. In that case, this change should clear things up for you.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Hello roboticus,

        Thank you for your input, this is my first time coding in OO way and I do not know how to do it correctly. I am reading online documentation and I am trying to figure it through there.

        So one final question, when is needed to use a Destructor?

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