Try Catch with Assert Statement

Discussion in 'Perl Misc' started by Bpejman@gmail.com, Jun 15, 2007.

  1. Guest

    Hi Everyone,

    Can someone please shed some light on this Try and Catch issue that
    I'm running into? I would highly appreciate any help and/or
    guidance. I've researched the web thoroughly and still no luck. I
    need to know exactly what libs I need to import into my program to be
    able to do the below statement (as done similarly in Python).

    sub foo {
    try {
    assert (0);

    } catch {
    print "Caught assertion error\n";
    };
    }

    Now, I've used all of the below libraries but unfortunately
    "assert(0);" does not jump into the catch {} scope. It simply exists
    the program from there where in other languages, assert always raises
    an exception which is caught by except{} or catch{} statement.

    use Carp::Assert;
    use Test::Harness::Assert;
    use Error;

    I do not want to use Eval and if ($@) since I cannot return any values
    from within Eval. For example, the following will not work for me:

    print foo(); #does not print any of the return codes from function
    foo since they reside within the eval.

    sub foo {
    eval {
    assert (1);
    return 1;
    };
    if ($@) {
    print "oops";
    return 0;
    }
    }

    Thanks everyone!!!
    Bobby
    , Jun 15, 2007
    #1
    1. Advertising

  2. Ala Qumsieh Guest

    wrote:

    > I do not want to use Eval and if ($@) since I cannot return any values
    > from within Eval. For example, the following will not work for me:


    Yes you can. From 'perldoc -f eval':

    In both forms, the value returned is the value of the last
    expression evaluated inside the mini-program; a return state-
    ment may be also used, just as with subroutines. The expres-
    sion providing the return value is evaluated in void, scalar,
    or list context, depending on the context of the eval itself.


    > print foo(); #does not print any of the return codes from function
    > foo since they reside within the eval.


    The return value of foo() is different from the return value of your eval
    block. You'll need to save the return value of your eval, and make it the
    return value of your sub. Something like this:

    > sub foo {
    > eval {


    my $evalReturn = eval {

    > assert (1);
    > return 1;
    > };
    > if ($@) {
    > print "oops";
    > return 0;
    > }


    return $evalReturn;

    > }
    >
    Ala Qumsieh, Jun 15, 2007
    #2
    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. bienwell
    Replies:
    4
    Views:
    3,713
    bienwell
    May 27, 2005
  2. Robert Brewer
    Replies:
    1
    Views:
    478
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,497
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    890
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,620
    Richard Bos
    May 17, 2010
Loading...

Share This Page