Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Line Number Confusion

by hmeharward (Novice)
on Oct 01, 2015 at 15:35 UTC ( [id://1143572]=perlquestion: print w/replies, xml ) Need Help??

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

I'm fairly new to Perl and I cannot seem to get the correct line number from a file when using $. I'm opening the file and I'd like to know on which line number the file (which is another program) fails if it fails so that I can make better error messages for the user. Right now I'm only getting a 0 or a 1. Any help is much appreciated! Note: exit_gracefully is just a sub for error messages.

open(FH, "$command |") || exitgracefully("Could not run $command\n") +; @day_file_args = <FH>; close(FH) || (($failure = 1) && print("TestingFailure $. \n"));

Replies are listed 'Best First'.
Re: Line Number Confusion
by choroba (Cardinal) on Oct 01, 2015 at 16:21 UTC
    You should inspect $! and $?, too (copied form system).
    if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I've been trying to use those too, but I keep getting messages and integers in other parts of my code with them such as "0 Illegal Seek," "256 ," "-1 Broken Pipe," and sometimes just "0 ." I was hoping to use $. because the other perl vars werent working for me

        I think it's not that those variables "aren't working" for you - rather, it's that you don't understand what their resulting values actually mean, or how they can help you figure out the problem.

        We don't know what sort of command line you're trying to run (or what type of system you're using), so we can't help you with that, unless you tell/show us more: what's the command line? what OS are you using?

        If you want to tell/show us more, it would be nice if you could provide enough code and data so that we can run it ourselves and see whether we get the same results you do. (If you're trying to run some custom compiled command that no one else has, that makes it a different kind of problem - probably not suitable for a perl forum.)

Re: Line Number Confusion
by Eily (Monsignor) on Oct 01, 2015 at 15:39 UTC

      Thank you! I've posted my code as simply as possible. Hopefully this helps! :)

Re: Line Number Confusion
by kcott (Archbishop) on Oct 01, 2015 at 15:46 UTC

    G'day hmeharward,

    Welcome to the Monastery.

    "Right now I'm only getting a 0 or a 1."

    You'll need to show us the code that only produces 0 or 1. When we have this, we'll be in position to advise where you've gone wrong.

    There's guidelines for posting questions: "How do I post a question effectively?". Please read this before posting again.

    — Ken

      Thank you! I've posted my code as simply as possible. Hopefully this helps! :)

Re: Line Number Confusion
by SuicideJunkie (Vicar) on Oct 01, 2015 at 17:32 UTC

    Couple of problems.

    @day_file_args = <FH>;

    That there, reads the whole thing. It is pretty rare to have a problem reading from a filehandle. You probably want while (my $line = <FH>){ munge(); } to read one line, process that, then read line two, and process that, so when you get an error you've only read N lines rather than all of them.

    ($failure = 1)

    You're setting failure to 1 instead of testing to see if it is 1? ($failure == 1) is what you want, or simply ($failure) if you only care if it is true or false.

      No, $failure is being set to one for a reason later in the code and it works fine. How do I get the line number from that though? I'm confused. Will:

       my $line = <FH>

      set $line to the current line number? Is there any way to use $. with this?

Re: Line Number Confusion
by RonW (Parson) on Oct 01, 2015 at 20:14 UTC
    close(FH) || (($failure = 1) && print("TestingFailure $. \n"));

    You are closing the file handle before printing the value of $. The file's line counter gets reset by close() (See Variables related to filehandles)

      I moved $. to between open and close and now it says I have 12243 lines when I only have 546.

        Hi,
        I think you have the whole stuff mixed up. Please re-read all that has been posted on this thread already.
        Then I think you also need to read up the documentation on $.
        Simply, do perldoc -v $. from your CLI and you should have it.

        Here is an example, if you may:

        use warnings; use strict; my $look_for; while(<DATA>){ if (/^\./) { $look_for = $.; next; } print $., $_; } print "The dot came up on line ", $look_for, $/; __DATA__ Mary has a little lamb little lamb, little lamb . Mary has ....
        Output
        1Mary has a little lamb 2little lamb, little lamb 4Mary has .... The dot came up on line 3
        Hope that helps!

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 22:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found