Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Can't call method "otherwise" without a package or object reference at

by rpelak (Sexton)
on Oct 05, 2010 at 22:54 UTC ( #863701=perlquestion: print w/ replies, xml ) Need Help??
rpelak has asked for the wisdom of the Perl Monks concerning the following question:

I am getting this error
Can't call method "otherwise" without a package or object reference at ...
Before that it was
Can't call method "with" without a package or object reference at ...
So something is funky...
I have several try catch blocks in the same sub though they are mutually exclusive mostly. The others work. I recently decided I needed to add the one that is having the issue. (stupid command I am calling inexplicably fails 1 in 10,000 times) And this new one just won't work... Here is some code...
if ( $pbOnDStarMachine == 1 ) { my ($sComm) = "sudo dchown foo \'$this->{sFullPath}\'"; try { libs::exec("$sComm"); } catch err with { my ($ex) = @_; dPrt("warn","$ex"); } otherwise { my ($ex) = @_; dPrt("warn","OTHERWISE:$ex"); }; return ""; }
That is the offending code. The Exec proc is here
sub exec { my ($comm) = @_; if ( dbg::debugOn() ) { dPrt("dbg","Debug, Would execute:\n$comm"); } else { dPrt("libs_verbose","Executing:$comm"); system("$comm"); if ($? == -1) { throw err("system command returned -1: $comm\nReason $!"); } elsif ($? & 127) { my ($sSig) = $? & 127; throw err("Command died with signal $sSig :$comm"); } else { my ($nExitVal) = $? >> 8; if ( $nExitVal != 0 ) { throw err("Command exited with value $nExitVal :$comm"); } } }
When I first ran into the error about "with" I did some research and found that you can get that error if an exception other than the one you named gets thrown. And they suggested the addition of the otherwise clause to catch those "other" exceptions. So I dropped one in to try and basically catch all errors and convert to a warning... But now the error is about the otherwise clause. AS far as my research is convinced it can't be about the error type that is getting thrown, so it must be something else, perhaps the otherwise isn't related... Anyone have any insight?

Comment on Can't call method "otherwise" without a package or object reference at
Select or Download Code
Re: Can't call method "otherwise" without a package or object reference at
by ikegami (Pope) on Oct 05, 2010 at 23:04 UTC
    Which module provides "try", "catch", "err", "with" and "otherwise"? More importantly, what are their prototypes?
Re: Can't call method "otherwise" without a package or object reference at
by ikegami (Pope) on Oct 05, 2010 at 23:16 UTC
    The error comes from something like this:
    $ perl -e'$x="1"; $x->otherwise' Can't call method "otherwise" without a package or object reference at + -e line 1.

    You don't appear to show any method calls to otherwise, unless something ends up being treated as an indirect method call.

    $ perl -e'$x="1"; otherwise $x' Can't call method "otherwise" without a package or object reference at + -e line 1.

    That would indicate otherwise isn't found as a sub.

    $ perl -le'sub otherwise { print "foo" } $x="1"; otherwise $x' foo

    Make sure you properly imported otherwise from whatever module provides it.

      The model Error.pm is the standard module that defines the try, catch and all that. The err comes from a module of my own...
      package err; use da qw(/.*/); use dbg qw(/.*/); use base qw(Error); use overload ('""' => 'stringify'); sub new { prtDbgSubInfo(); my ($pkg,$text) = @_; my @args = (); local $Error::Depth = $Error::Depth + 1; local $Error::Debug = 1; # Enables storing of stacktrace my ($this) = $pkg->SUPER::new(-text => $text, @args); my ($stack) = $this->stacktrace; $stack =~ s#$text #$text\n\t#; #adds a newline after the text and before the first at ... my ($preMsg) = "\n--Error--"; $this->{-text} = "$preMsg\n$stack"; return($this); }
      And the other try / catch blocks work in the same sub, so the stuff is defined. It has to be something about this particular use of it... like a simple syntax error (I checked for the semi-colon on the end) or the a unique exception coming from inside, but I thought the otherwise should handle any exception type...
        Like I said earlier, make sure you properly import otherwise.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863701]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-08-30 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (292 votes), past polls