Strange behaviour

R

Richard

Why does isn't my function executed in this context:


# does not work
my $doc = XMLin($configfile) or die Notifier::Info("ERROR: Can't open
$configfile");

# works if I change the above line in my perl script
Notifier::Info("blablub");
my $doc = XMLin($configfile)


So if I just call Notifier::Info() it works, but if I call my func to
handle the error :

or die Notifier::Info("ERROR: Couldn't open $configfile");

or this way

or Notifier::Info("ERROR: Couldn't open $configfile");

it doesn't work.
Does anybody know the reason for this?

Thanks in advance , Richard


#content of Notifier.pm

package Notifier;

sub Info{
my ($msg) = @_;
open(LOG, ">>/tmp/log.txt") or die "Couldn't open /tmp/log.txt \n";
$logentry = "\nINFO:" . $msg ;
print LOG $logentry;
close LOG;
}
 
T

Tad McClellan

Richard said:
Why does isn't my function executed in this context:


# does not work
my $doc = XMLin($configfile) or die Notifier::Info("ERROR: Can't open
$configfile");
Does anybody know the reason for this?


perl does, and it will tell you if you ask it to.

#content of Notifier.pm

package Notifier;

sub Info{
my ($msg) = @_;
open(LOG, ">>/tmp/log.txt") or die "Couldn't open /tmp/log.txt \n";
$logentry = "\nINFO:" . $msg ;
print LOG $logentry;
close LOG;
}


You should always enable warnings when developing Perl code!


perl -w -MNotifier

Notifier.pm did not return a true value.
BEGIN failed--compilation aborted.
 
R

Richard

Tad said:
perl does, and it will tell you if you ask it to.

well if perl did I 'd be already at home having some drink.
You should always enable warnings when developing Perl code!

Who said I didn't enable warnings? I didn't post a complete script!
perl -w -MNotifier

Notifier.pm did not return a true value.
BEGIN failed--compilation aborted.

Well as I said it works when calling with Notifier::Info but not with or
die Notifier::Info so this is not the problem.
I just didn't post the complete module. Of course my module has got 1;
at the end.

I use warnings and strict there are no errors.
 
T

Tad McClellan

[ Please do not send stealth-CCs of usenet posts, it is bad netiquette. ]




Since you said this was the contents of Notifier.pm...

Who said I didn't enable warnings? I didn't post a complete script!


.... I assumed that you actually meant that this was the
contents of Notifier.pm.
 
G

Gunnar Hjalmarsson

Richard said:
# does not work
my $doc = XMLin($configfile) or die Notifier::Info("ERROR: Can't open
$configfile");

Pls define "does not work".

Maybe XMLin() returns a true value even if it cannot read the file? In
that case, checking whether the return value is true isn't an adequate
error check.
 
J

J. Gleixner

Richard said:
Why does isn't my function executed in this context:


# does not work
my $doc = XMLin($configfile) or die Notifier::Info("ERROR: Can't open
$configfile");

# works if I change the above line in my perl script
Notifier::Info("blablub");
my $doc = XMLin($configfile)

You'd have seen the same behavior even without using your class.

Put an eval around it. XMLin will croak/die the file doesn't exist.

eval {
my $doc = XMLin($configfile);
#.. more XML::Simple methods...
};
if ( $@ )
{
Notifier::Info($@);
die( $@ );
}

Or redefine $SIG{__DIE__}.
 
G

Gentoopower

Gunnar said:
Pls define "does not work".

Maybe XMLin() returns a true value even if it cannot read the file? In
that case, checking whether the return value is true isn't an adequate
error check.

No in my case it dies, because the file doesn't exist.
Of course I did a simple test and replaced:

... or die Notifier::Info("ERROR: Can't open $configfile");

with:

or die print "..";

which works:)

But J.Gleixner replied with a resonable solution, which I'm going to
checkout. I'm still don't exactly understand, why:

or die print "..."; # works

and

or die Notifier::Info("..."); # doesn't

but calling Notifier::Info("...") within normal code works.
 
R

Richard

J. Gleixner said:
You'd have seen the same behavior even without using your class.

Darn you are right, when I first tested some of my stuff I used used it
with an open file and there a print worked:)
Then I did lots of coding and didn't pay attention to it anymore.
Put an eval around it. XMLin will croak/die the file doesn't exist.

eval {
my $doc = XMLin($configfile);
#.. more XML::Simple methods...
};
if ( $@ )
{
Notifier::Info($@);
die( $@ );
}

Or redefine $SIG{__DIE__}.

Perfect answer, A+ :)

Thanks, now I'm all set. a simple print just wasn't sufficient to me
since I have to send mails etc. in case something goes wrong(dies).
 
G

Gunnar Hjalmarsson

Gentoopower said:
No in my case it dies, because the file doesn't exist.
Of course I did a simple test and replaced:

.. or die Notifier::Info("ERROR: Can't open $configfile");

with:

or die print "..";

which works:)

The latter does probably not "work" the way you think it does.

You don't understand the meaning of using 'or' that way, do you? You can
do so if the function _returns_ a true value at success, but only at
success. That's probably not the case with XMLin(), which - assuming we
are talking about XML::Simple::XMLin() - returns a reference according
to the POD.
 
B

Ben Morrow

Quoth "J. Gleixner said:
You'd have seen the same behavior even without using your class.

Put an eval around it. XMLin will croak/die the file doesn't exist.

eval {
my $doc = XMLin($configfile);

This variable is scoped over the eval only, so it won't exist outside.
#.. more XML::Simple methods...
};
if ( $@ )
{
Notifier::Info($@);
die( $@ );
}

Since eval returns undef when it catches an exception, this can be
simplified to

my $doc = eval { XMLin($configfile) }
or die "XMLin('$configfile') failed: $@";

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top