Strange behaviour

Discussion in 'Perl Misc' started by Richard, Nov 9, 2006.

  1. Richard

    Richard Guest

    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;
    }
     
    Richard, Nov 9, 2006
    #1
    1. Advertising

  2. 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");


    > 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 9, 2006
    #2
    1. Advertising

  3. Richard

    Richard Guest

    Tad McClellan 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");

    >
    >> 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.
    >
    >
    >
     
    Richard, Nov 9, 2006
    #3
  4. [ Please do not send stealth-CCs of usenet posts, it is bad netiquette. ]


    Richard <> wrote:
    > Tad McClellan schrieb:
    >> Richard <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 9, 2006
    #4
  5. 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
     
    Gunnar Hjalmarsson, Nov 9, 2006
    #5
  6. Richard

    J. Gleixner Guest

    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__}.
     
    J. Gleixner, Nov 9, 2006
    #6
  7. Richard

    Gentoopower Guest

    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.
     
    Gentoopower, Nov 9, 2006
    #7
  8. Richard

    Richard Guest

    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).
     
    Richard, Nov 9, 2006
    #8
  9. 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
     
    Gunnar Hjalmarsson, Nov 9, 2006
    #9
  10. Richard

    Ben Morrow Guest

    Quoth "J. Gleixner" <>:
    > 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, *
    it is worth reading when you are sixty." [C.S.Lewis]
     
    Ben Morrow, Nov 9, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    433
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,455
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    460
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    503
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    395
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page