Error when redirecting data to file - perl

Frequently [not always] when i run procedure define a file handler i get strange error on internal function which i dont understand how to debug.
In My PERL code i have the following line [111]:
open V_FILE_SEC, ">>$file/V_$file$dir.csvT" or die $!;
And when i am operating the script [>myscript.pl DPX_*] i get:
"No such file or directory at myscript.pl line 111, line 18004."
What is the meaning of line 18004? How to start debug?
Thanks.

From perldoc -f die:
If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied. [Emphasis added]
The "input line number" is the value in $., roughly the number of lines of input you have read from the most recent filehandle you accessed.
In your case, you could use to look at your program input and see if there is anything unusual around line 18004 that your program wasn't expecting.

Related

Storing a line read from text file to variable in Perl script

I am looking for a solution to below case.
I need to read task.txt file from my Perl script disconnect.pl,
and I have to save the content of file to variable which I can use in multiple places in my Perl script.
Task.txt file contains words in single quotes separated by comma but in a single line like below
'qbc','456','sdf','fgh'
#########################
my $filename = 'testing_folder/text_document.txt';
open(FH, '<', $filename) or die $!;
while(<FH>){
$content=$_
}
print "$content"
my $ql = "select in ($content)";
print "$ql"
###############################
It's giving error at line my $ql
I put your code into a file called "line" and tried to run it. Here's what happened:
$ perl line
syntax error at line line 8, near "my "
Execution of line aborted due to compilation errors.
As you say, line 8 is this line:
my $ql = "select in ($content)";
But here's a little secret about compiler error messages - they aren't as clever as we'd like them to be. And one common problem that you'll see is that they often report an error on the wrong line. So it's always worth checking the line or two before where the error is reported.
In this case, the problem is on the previous line:
print "$content"
Can you see it now? Take a close look at the end of that line. Do you see anything missing?
Perl statements are separated by semicolons. Newlines (usually) mean nothing to the Perl compiler. Perl is happy for you to spread a statement over two or more lines. So when it comes across a line without a semicolon at the end, it just assumes that the next line is a continuation of the same statement.
Which means that when you write:
print "$content"
my $ql = "select in ($content)";
Perl sees it as one statement:
print "$content" my $ql = "select in ($content)";
And that's a statement that doesn't make sense. So the compiler gives an error.
That also explains why the error reporting is a line out. Perl reports the error at the end of the problematic statement.
Oh. And in case I wasn't clear - you need a semicolon on the end of that line.

Why do I lose output when calling another perl program with backquotes

If I run a perl program and call another perl program using backquotes, print statements from the called program don't appear at the terminal.
If I call the program using 'system', the print statements are displayed.
EG:
This is ProgA.pl
print "In ProgA.pl, about to call ProgB.pl";
my $dum=`ProgB.pl`; # print output doesn't appear
### $dum=system("ProgB.pl"); # this prints OK
print"\nBack in ProgA.pl";
print "\ndum = $dum"; # ProgB's output doesn't show here either
(No warnings or errors, perl.exe found through file association)
This is ProgB.pl:
print "\nPrinting from ProgB.pl";
What is the reason for the difference?
Why isn't the backquoted call output returned in $dum (I tried both STDOUT and STDERR)? If I call dir in backquotes, I get its output in $dum.
You have a path issue.
It works as expected ($dum is assigned the value "Printing from ProgB.pl") if I change the backticks from ProgB.pl to ./ProgB.pl. Without the explicit ./ path, it searches the system path and generates an error, as you can see if you change that line to
my $dum=`ProgB.pl` or die $!;
Which generates the output
In ProgA.pl, about to call ProgB.plNo such file or directory at ./ProgA.pl line 4.
Thus illustrating once again that you should always check the return values of your system calls for error conditions.
It appears that by failing to put a newline character at the end of the print command in ProgB, I failed to flush the buffer before returning to ProgA. Thanks to Chris Turner.

What does <DATA> mean in Perl error messages?

I'm trying to debug a error I am currently experiencing in Perl, and my first clues are the files and the lines stated. However, I'm not sure what <DATA> is.
So what is it?
It means you had read 228 lines from the DATA file handle when the error occurred. It's unlikely to be relevant in this case.
It's even less likely to be relevant when the handle in question is DATA. DATA allows a program to read data from the end of its source file. It's usually used to store hard-coded data or part of the program itself. It's usually read from start to finish early in the program execution. But few bother to close the handle, so unrelated error message end up tagged with the number of the last line of that data.
<DATA> is default filehandle for __DATA__ or __END__ tokens in Perl.
What it means is, there should be a __DATA__ or __END__ sections towards the end of the perl script you are running. Whatever text you have after those tokens is considered by perl interpreter as a file and is made available to the program through the <DATA> file handle.
print while (<DATA>);
# End of Perl script. Whatever follows goes into <DATA> fh.
__DATA__
line 1
line 2
line 3
line 4
line 5
line 6

How to tell the line number when `die`?

I'd like to write sth like:
die "Error in file $0 line number $line_number_of_this_cmd_in_file \n";
In my perl script file.
Any help?
Thx a lot!
(perl 5)
If you do not put \n at the end of the string you pass to die, then perl will automatically add the line number.
Otherwise, the token __LINE__ will give you the current line number in your script (and __FILE__ gives the current file name).
Unless you meant the current line number of the file you just read from - that is available in $.
That is quite easy: Drop the \n at the end of the line and die will append whatever message you wrote with the name of the script and the line number.
For example:
die "Encountered error 15 ";
will result in it printing:
"Encountered error 15 at script.pl line 42\n"
or whatever is applicable.

Can someone explain this Perl code snippet?

This little piece of code has been a staple in a bunch of my scripts, but I took the syntax from another working script that someone else wrote and adapted it to fit my needs. I'm not even sure that the syntax used here is the best or most common way to open a file handler either.
The code is:
$fh = \*STAT_FILE;
open ($fh,">>".$stat_file) or die "Can't open $stat_file: $!\n";
my $print_flag = ( -z $stat_file );
I don't fully understand the first line and also the last line of the code above. Specifically, \*STAT_FILE and -z, respectively.
I know that, for the most part, the second line will open a file for appending or quit and throw an error. But again, I don't understand what purpose the $! serves in that line either.
Can someone explain this Perl code, line-by-line, to me in pseudo? Also, if the method above is not the preferred method, then what is?
Thanks in advance
Before perl 5.6, file handles could only be globs (bare words) or references to globs (which is what \*STAT_FILE is). Also, it's better to use 3-argument open (See the docs. Also see perlopentut). So you can now do:
open(my $fh, ">>", $stat_file) or die "Failed to open $stat_file: $!";
and forget about \*STAT_FILE.
-z is one of the file test functions (and takes a file name or file handle as an argument) and tests to see if the file has zero size.
$! is one of the Special Variables and contains the most recent system error message (in this case why you can not open the file, perhaps permission issues, or a directory in the path to the file does not exist, etc.).
You should learn to use perldoc, all of this is in perldoc:
perldoc perlfunc (specifically perldoc -f open and perldoc -f -X)
perldoc perlvar
The first row assign to the variable a reference (the backslash sign) to the typeglob (a fullsymbol table entry) STAT_FILE. This has been a quite idiomatic perl construct to pass filehandles as reported, just to name it, in the Larry Wall "Programming perl". The $! variable contains the error message reurned by the operating system.
So the whole meaning is:
line 1. put in the $fh variable a filehandle;
line 2. Open for append the file reporting the system message error should a fault happens;
line 3. Set a flag variable warning if the file has zero length