Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tracking down errors in data from input files.

by why_bird (Pilgrim)
on Mar 07, 2008 at 10:38 UTC ( [id://672731]=perlquestion: print w/replies, xml ) Need Help??

why_bird has asked for the wisdom of the Perl Monks concerning the following question:

Good Morning Monks,

This is a question about error tracking.. Say I'm reading in various variables from various files, and passing them to various different subs in various different modules I wrote. Then say that some of my input data (in the input files) is messed up in some way. So I pass it to one of my subs, and the sub throws an error (lets have an example because I'm confusing myself here :P)

(main.pl) use strict; use warnings; use mySubs; my @array; while (my $temp=<INPUT>){ my $number=&mySub(\$temp); @array=(@array,$number); } --------------------------- (mySubs.pm) use strict; use warnings; package mySubs; #...Exporter.. etc.. sub mySub { my $variable = ${$_[0]}; .... #some stuff .... if (some condition){ print "data $variable not valid in mySub\n"; return -1; } .... #rest of sub .... }

So what I want to be able to do is instead of just printing 'data $variable not valid in mySub' I'd like to be able to print 'data $variable not valid in mySub from line xx in 'main.pl'; line yy in <INPUT>'

Is this possible? I thought I could pass $. around but that wouldn't (I don't think) tell me it came from 'main.pl' (And it would be a pain to change all of my subs if there's a simpler way!)

I don't even really know where to start on this one, so any monk-ish wisdom would be appreciated!
Cheers, why bird.

p.s. I wasn't sure what an appropriate title would be so feel free to change it/suggest what it should be changed to

........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

Replies are listed 'Best First'.
Re: Tracking down errors in data from input files.
by hipowls (Curate) on Mar 07, 2008 at 10:55 UTC

    Two things, first @array=(@array,$number); is more efficiently written as push @array, $number;. The way you wrote it the entire contents of @array are copied each time.

    Second see Carp for printing the call stack. At its simplest

    use Carp qw(carp); carp "data $variable not valid in mySub\n";
Re: Tracking down errors in data from input files.
by moritz (Cardinal) on Mar 07, 2008 at 10:58 UTC
    $. is a global variable, so if you're always working with the current line, you don't need to pass it around.

    For debugging I always use Carp::confess (it's in core) instead of die, which gives me a full stack trace.

    If you want to associate additional informations (like line numbers) with your data, I recommend the object oriented approach.

      Thankyou both, I'll look into Carp (and thanks for the tip about push (@array) --- I didn't realise it made a difference, d'oh!)
      moritz, humour me? :) where would I start with an object oriented approach in Perl? How would this help me track down line number etc?
      Cheers, why bird
      ........
      Those are my principles. If you don't like them I have others.
      -- Groucho Marx
      .......
        Object Orientation in Perl

        My idea was inspired by compilers. They usually build a parse tree where the nodes store the matched text, logical information (what kind of token is this?) and the position in the source file.

        Now if that parse tree is converted to an abstrac syntax tree, they still keep that line information, so if there is an execution error (let's say division by zero) the compiler can tell you which line of code caused that error.

        So you can create a class "Traceable" that stores a string and its position in an input file to keep track of where it comes from.

        It might be a bit overkill in your project, but for larger projects (parser, compiler) that is common practice.

        (BTW it's nice to see that the quality of your questions improve over time ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://672731]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found