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;
}
| [reply] [d/l] |
|
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
| [reply] |
|
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.)
| [reply] |
Re: Line Number Confusion
by Eily (Monsignor) on Oct 01, 2015 at 15:39 UTC
|
| [reply] |
|
| [reply] |
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.
| [reply] [d/l] [select] |
|
| [reply] |
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.
| [reply] [d/l] [select] |
|
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?
| [reply] [d/l] |
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)
| [reply] [d/l] [select] |
|
| [reply] |
|
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
| [reply] [d/l] [select] |
|