develooper Front page | perl.beginners | Postings from September 2009

shift() from the caller() stack?

From:
Steve Bertrand
Date:
September 30, 2009 19:36
Subject:
shift() from the caller() stack?
Message ID:
4AC41516.6080902@ibctech.ca
Hi all,

I am trying to rectify a 'bug' that I've created, and hence wrote a test
for.

The code is within an error module, which is allowed to die() the
program upon failure. To compensate for this within the test, I use eval().

In this case however, I'm testing an 'add_trace()' method, that dies if
the caller supplies a param to step back one level before adding parts
of the trace. Hence, I'm expecting death, but I don't get it. Death only
comes when I run the test outside of the eval() scope.

My question is, how do I remove the 'eval' wrapping from the stack after
I eval() the code I'm trying to execute to get my breakage? Or, iow, how
can I better write my  add_trace() method?

... # add_trace() follows # test

#
# test
#

{ # add_trace with param, but with an improper caller stack position

    _reset();

    eval { $error->add_trace( 1 ) };

    like (  $@,
            '/Bad API/',
            "add_trace() with a param when you are a first-level " .
            "caller() results in death via ISP::Error"
        );
}


#
# add_trace()
#

sub add_trace {

    my $self 	  = shift;
    my $step_back = shift;

    $self->function_orders();

    if ( $step_back ) {

        my $valid_caller = ( caller(1) )[0];

        if ( ! $valid_caller ) {

            my $message =   "blah..."; # appended with 'Bad API' later..

            $self->bad_api( $message );
        }

        unshift @{ $self->{ stack } }, {
            package  => ( caller(1) )[0],
            filename => ( caller(1) )[1],
            line     => ( caller(1) )[2],
            sub      => ( caller(2) )[3] || '',
        };
        return 1;
    }

    unshift @{ $self->{ stack } }, {
            package  => ( caller(0) )[0],
            filename => ( caller(0) )[1],
            line     => ( caller(0) )[2],
            sub      => ( caller(1) )[3] || '',
    };

    return 0;
}

Steve



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About