Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Strange behaviour

Reply
Thread Tools

Strange behaviour

 
 
Richard
Guest
Posts: n/a
 
      11-09-2006
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;
}
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-09-2006
Richard <(E-Mail Removed)> wrote:

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



--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      11-09-2006
Tad McClellan schrieb:
> Richard <(E-Mail Removed)> wrote:
>
>> 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.


well if perl did I 'd be already at home having some drink.

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


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



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-09-2006

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


Richard <(E-Mail Removed)> wrote:
> Tad McClellan schrieb:
>> Richard <(E-Mail Removed)> wrote:



>>> #content of Notifier.pm



Since you said this was the contents 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!

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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-09-2006
Richard wrote:
>
> # 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      11-09-2006
Richard wrote:
> 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__}.


 
Reply With Quote
 
Gentoopower
Guest
Posts: n/a
 
      11-09-2006
Gunnar Hjalmarsson wrote:
> Richard wrote:
>>
>> # 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.
>


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.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      11-09-2006
J. Gleixner schrieb:
> Richard wrote:
>> 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.


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).

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-09-2006
Gentoopower wrote:
> Gunnar Hjalmarsson wrote:
>>Richard wrote:
>>>
>>># 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.

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-09-2006

Quoth "J. Gleixner" <(E-Mail Removed)>:
> Richard wrote:
> > 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);


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

--
"If a book is worth reading when you are six, * (E-Mail Removed)
it is worth reading when you are sixty." [C.S.Lewis]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
a strange behaviour of FF Maurice Firefox 2 03-11-2005 11:53 PM
Strange mouse behaviour with flash in Mozilla 1.3.7 hpoppe Firefox 0 11-07-2004 12:16 PM
Strange taskbar behaviour (notification area) Falcon Wireless Networking 0 08-17-2004 09:03 AM
[mozilla1.6] strange behaviour with newsgroup joost68 Firefox 5 04-03-2004 03:48 AM



Advertisments