# Copyright (c) 2009 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: CallingContext.pm,v 1.3 2009/10/10 04:09:46 nagler Exp $ package Bivio::IO::CallingContext; use strict; # Bivio::IO::Alert imports so do not change import structure use base 'Bivio::UNIVERSAL'; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.3 $ =~ /\d+/g); my($_IDI) = __PACKAGE__->instance_data_index; my($_A) = 'Bivio::IO::Alert'; sub calling_context_get { $_A->warn_deprecated('use get'); return shift->get(@_); } sub equals { my($self, $that) = @_; return 0 unless $self->is_blessed($that); foreach my $f (qw(file line)) { return 0 unless $self->get($f) eq $that->get($f); } return 1; } sub get { my($self) = shift; my($fields) = $self->[$_IDI]->[0]; return $self->return_scalar_or_array( map(exists($fields->{$_}) ? $fields->{$_} : $_A->bootstrap_die($_, ': not a calling_context field'), @_), ); } sub get_top_package_file_line_sub { return @{shift->[$_IDI]->[0]}{qw(package file line sub)}; } sub inc_line { my($self, $inc) = @_; return $self->new_from_file_line( $self->get('file'), $self->get('line') + $inc, ); } sub internal_as_string { my($self) = @_; return $self->get(qw(file line)); } sub new_from_caller { my($proto, $skip_packages) = @_; my($frame) = 0; if ($skip_packages) { while (my $p = caller($frame)) { last unless grep($p eq $_, @$skip_packages); $frame++; } } $frame++; my($self) = $proto->SUPER::new; $self->[$_IDI] = [ map(+{ package => (caller($_))[0] || undef, file => (caller($_))[1] || undef, line => (caller($_))[2] || undef, sub => (caller($_ + 1))[3] || undef, }, $frame, $frame + 1), ]; return $self; } sub new_from_file_line { my($proto, $file, $line) = @_; my($self) = $proto->SUPER::new; $self->[$_IDI] = [{ file => $file, line => $line, }]; return $self; } 1;