getting line number from perl interpreter? 
Author Message
 getting line number from perl interpreter?

I have a perl program that provides a bunch of functions that
basically implements a little language.  The "data files" are themselves
perl that make calls to the functions provided in the
"main" program.   How can I structure the main program and the evaluation of
the data file such that when I encounter an error
in the data file I can print what line number it occurred on?   The trick is
that the data files are truly perl: I'd have to parse
perl to find the line numbers associated with a particular function call,
and I've heard over and over again that "perl is best at
parsing perl".   OK, so how can I do this?

Currently I'm doing something like this:

main program:

#!/usr/bin/perl

sub func
{

    if ($arg eq "error")
    {
        print("error!\n"); # at what data file line number, though?
        exit(-1);
    }
    print ("good input\n");

Quote:
}

while (<>)
{
    $cmd .= $_;

Quote:
}

eval($cmd);

data file:
func("howdy");
func("there");
func("error");
etc.

% main.pl < data.pl

(I actually do an "open" of the data file and read it in via the file
handle from within main.pl, but this shows the idea).

The data files are fully perl-ized and free-form:
simple tricks like counting carriage returns or ");" won't work -- surely
the line number in the internal interpreter must be available somewhere (if
I do the evaluation differently?   How?) since perl
issues line numbers for perl errors!

thanks for any help!

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.*-*-*.com/



Mon, 03 Feb 2003 03:00:00 GMT  
 getting line number from perl interpreter?
On Thu, 17 Aug 2000 20:01:56 PDT,

Quote:

>         print("error!\n"); # at what data file line number, though?

# perldoc perlvar

look for $.

Martien
--
Martien Verbruggen              |
Interactive Media Division      |
Commercial Dynamics Pty. Ltd.   | What's another word for Thesaurus?
NSW, Australia                  |



Tue, 04 Feb 2003 03:00:00 GMT  
 getting line number from perl interpreter?

Quote:

> I have a perl program that provides a bunch of functions that
> basically implements a little language.

That sounds like it should be a module.  See end of this message.

Quote:
>  The "data files" are themselves
> perl that make calls to the functions provided in the
> "main" program.   How can I structure the main program and the evaluation of
> the data file such that when I encounter an error
> in the data file I can print what line number it occurred on?   The trick is
> that the data files are truly perl: I'd have to parse
> perl to find the line numbers associated with a particular function call,
> and I've heard over and over again that "perl is best at
> parsing perl".   OK, so how can I do this?

                                    ^^
The answer is in the question!

perldoc -f do.

Quote:
> while (<>)
> {
>     $cmd .= $_;
> }

> eval($cmd);

Make that:



Of course this only works if your OS has /dev/stdin.


require() or use() anything.  This will mean that Perl looks for your

BTW: This is all rather "old-fashoned".  The "modern" way to do this
would be to make your app into an Exporter module, say FooBar.pm and
then:

perl -MFooBar data.pl

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Tue, 04 Feb 2003 03:00:00 GMT  
 getting line number from perl interpreter?

Quote:

> I have a perl program that provides a bunch of functions that basically
> implements a little language.  The "data files" are themselves  perl
> that make calls to the functions provided in the
> "main" program.   How can I structure the main program and the
> evaluation of
> the data file such that when I encounter an error in the data file I can
> print what line number it occurred on?   The trick is  that the data
> files are truly perl: I'd have to parse perl to find the line numbers
> associated with a particular function call,  and I've heard over and
> over again that "perl is best at parsing perl".   OK, so how can I do
> this?

Look at the docs for the following:



catch warnings as well).

caller (see perlfunc)

the Carp module

the %SIG hash and signal handlers (see perlipc)

What you probably want to do is to provide a localized __WARN__

(through your handler).

If you provide a localized __DIE__ handler as well, you can also look at

Inside that handler, you can use caller with an argument to get the line
number info:

(from doc of caller):

With EXPR, it returns some extra information that the de{*filter*} uses to
print a stack trace.  The value of EXPR indicates how many call frames
to go back before the current one.

    ($package, $filename, $line, $subroutine,
     $hasargs, $wantarray, $evaltext, $is_require) = caller($i);

(back to my commentary)

Here, $evaltext would be the text that you were eval'ing.

--
Ned Konz
currently: Stanwood, WA

homepage:   http://www.*-*-*.com/



Wed, 05 Feb 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Handling FieldDefs dynamically???

2. DBCombo Initially Blank

3. Database initialisation faults/loss of resources - best way to quit on start-up?

4. AutoSplit gets line numbers wrong?

5. AutoSplit gets line numbers wrong??

6. getting line number n of a file

7. getting the line number of the scriptfile

8. Q: Getting program line number?

9. getting line numbers from file - grep -n

10. Getting line numbers in scripts

11. Getting the line number where cursor is in Text widget

12. Perl line numbers vs Apache error_log reported numbers?

 

 
Powered by phpBB® Forum Software