Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

line no. in a file

by ashok (Sexton)
on Dec 29, 2000 at 04:40 UTC ( [id://48740]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am checking line no in a file using $. While I am debugging the I tried to verify the value of $. using x $. and I surprised that value of $. is totally different. I am getting the line no of the file I am debugging not the line no of a file program opens and reads. But while running the program without debug mode it prints the line no normally. How to check the line no. then in debug mode? Here is in clear:
I wrote a program 'test.pl'. This program reads a file called 'names +.txt'. I am checking line no. of the file 'names.txt' in debugging us +ing x $. But I am getting line no. of 'test.pl' , not a line no. of ' +names.txt'.
Can anyone help me ? Thanks Ashok

Replies are listed 'Best First'.
(tye)Re: line no. in a file
by tye (Sage) on Dec 29, 2000 at 09:21 UTC

    I suspect the problem is that:

    sub save { @saved = ($@, $!, $^E, $,, $/, $\, $^W); $, = ""; $/ = "\n"; $\ = ""; $^W = 0; }
    from perl5db.pl doesn't mention $. (though the real solution may be about the opposite of that -- I haven't given it enough thought).

    Dive right in and patch that and see if it makes a difference. If so, I'm sure you could send your patch in as a bug using perlbug and we'd all benefit.

    While your at it, patch in a option so that $^W = 0 can be $^W = 1 so you can use the debugger to interactively play with warnings. (:

            - tye (but my friends call me "Tye")
      I know this has come up before on p5p, and I can only suppose that there was some reason why Ilya didn't fix it in the obvious way. You should probably check through the p5p archives to see why this hasn't already been done.

      (Searching... Aha.)

      Says Ilya:

      Debugger is already doing local($.). This is not enough to localize $., since it is a combination of two ideas: last file handle and last line number.

      References of interest:

        If I understand what is going on, then what we want to do is localize the "last file handle read" as well as $., but Perl gives us no access to localize the "last file handle read".

        So I think lemming's patch is okay. It seems to me that what it does is temporarilly set $. for the wrong file handle to the "right" (expected by the user) value (and the user can't tell that the line number is being reported for the wrong file, just that the "right" line number is now being reported). This is why both the @saved and local bits are required. One puts the expected value into place while the other restores the correct value for the file handle that the user isn't interested in (but that the debugger is).

        Well, I hope I got that right. I didn't dig into it so my analysis could be wrong.

                - tye (but my friends call me "Tye")
      tye gave me a very good hint on what to do and it was in the debugger. Version 1.07 perldb.pl from ActiveState 5.6 perl In Unix dif format:
      498c498 < $usercontext = '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' . --- > $usercontext = '($@, $!, $^E, $,, $/, $\, $^W, $.) = @saved;' . 546c546 < $usercontext = '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' . --- > $usercontext = '($@, $!, $^E, $,, $/, $\, $^W, $.) = @saved;' +. 1434c1434 < ($@, $!, $^E, $,, $/, $\, $^W) = @saved; --- > ($@, $!, $^E, $,, $/, $\, $^W, $.) = @saved; 1496c1496 < @saved = ($@, $!, $^E, $,, $/, $\, $^W); --- > @saved = ($@, $!, $^E, $,, $/, $\, $^W, $.); 1516a1517 > local $saved[-1]; # Preserve the old value of $.

      Can't say it's pretty, but no more ugly than before. I think I'll go check to make sure it hasn't already been fixed in a later version.

      Update:Thanks to Dominus to doing what I was going to check this morning. I was just too tired to check last night.

      More update: I would suggest getting the 1.07 version of perl5db.pl from www.perl.com 5.7.

        That is the current version of the debugger in development snapshots. So add to your version an increment in the version number, check perlvar for any other possibly useful but missing global variables, do a "diff -u" and email the result to perl5-porters@perl.org... :-)
      Hi, Infact I am working on Sun Unix. I do not have any admin. previliges to modify anything. I will try with your code by saving the variables in my program and will debug. Thanks Ashok

        Just copy perl5db.pl from where @INC finds it to some directory of your own and run perl -Ithatdir -d to use your private modified debugger.

                - tye (but my friends call me "Tye")
Re: line no. in a file
by lemming (Priest) on Dec 29, 2000 at 05:13 UTC
    The problem is that $. is from the perspective of the debugger.
    What you might want to do is keep track of the line number of your file with your own variable.
      or localize $. in your code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found