Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
When I write a file in Perl, the complete content of each Print statement is getting updated on the file once the program is complete.
Is there a way in which the file can be updated after each print statement is executed ?
I tried setting $|=1, but it does not seem to work.
Setting $| to non-zero enables autoflush on only the currently selected output file handle. By default this is STDOUT unless you have called select to change it
That means that, if you have opened a new handle to a file, $| will not affect its behaviour
Instead, you can use the IO::Handle module's autoflush method. There is no need to use IO::Handle as IO::File, which subclasses IO::Handle, is loaded on demand by any version of perl since v5.14
It would look like this
open my $fh, '>', 'myfile.txt' or die $!;
$fh->autoflush;
After this, anything sent to the file using print $fh is immediately flushed to disk
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have been trying to use the seek function in a Perl script whose input file is in .gz format. I have opened the file using the following set of commands
if ($ARGV[0] =~ /.gz$/) {
open (FH1, "gunzip -c $ARGV[0] |") || die ("cant open file");
}
else {open (FH1, "<$ARGV[0]") || die ("cannot open file");
}
When seek function is used for a normal text file it is working fine, If is give a .gz file as input seek function is not working properly.
Is there any alternative for the seek function in this situation other than closing and opening the file wherever seek is used
The core IO::Uncompress::Gunzip module has limited support for seek when used to read a gzipped file (Instead of using an external program like you're doing):
Provides a sub-set of the seek functionality, with the restriction that it is only legal to seek forward in the input file/buffer. It is a fatal error to attempt to seek backward.
Note that the implementation of seek in this module does not provide true random access to a compressed file/buffer. It works by uncompressing data from the current offset in the file/buffer until it reaches the uncompressed offset specified in the parameters to seek. For very small files this may be acceptable behaviour. For large files it may cause an unacceptable delay.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to do command line argument parsing for a directory path in perl. I want to make this argument as optional.
so when the user gives path, it showed be assigned to a variable $release_model else it will execute other code I have written for finding directory from main directory. I am new to perl but somehow coded following. Can anybody help?
Getopt::Long
my $command_line = $GetOptions(\%Opts, "help|h","email=s#","test","model");
if($command_line==0){
print "$PROGRAM: no arguments are given";
Usage{};
}
else die "No arguments were given"
But it doesn't accept model as optional argument and throws error.
I just started working with perl.
It is quite hard to guess what exactly you are after as the code provided contains lots of errors and other features not described. But to start learning with something simple, here is something that I hope matches your requirements.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $release_model = '/path/to/"main directory"'; # default value
GetOptions( 'model=s' => \$release_model )
or die("Error in command line arguments\n");
print "Release model is: $release_model\n";
If you save this to a file (e.g. my_program.pl) and make it executable then you can see it provides these features:
If you call it without arguments ./my_program.pl, the default value of $release_model will be used.
If you call it with argument model (e.g. ./my_program.pl --model /another/directory), the provided value will be assigned to $release_model.
If you call it with wrong arguments (e.g. ./my_program.pl --mdoel), it prints reasonable error message and exits.
Try it yourself. And go and read some tutorial on Perl if you want to do some serious work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a perl process which run every day automaticaly, it takes about 20 minutes and finnish and print every opertion to stdout worht to mention every operation takes about second .
Sometimes it seems that the stdout is stucked and i need to click enter in order for the process to continue running .
could someone experience such issue/and suggestion how to handle this issue ?
below a snapshot of my code:
foreach (<>) {
chomp;
if (validate($_) == 0) {
print "$_ validated with Success\n";
}
else {
print "$_ validated with failure\n";
}
}
As Bill Ruppert writes, you need to use while (<>) instead of foreach (<>) in order to allow the print statements to be executed after each line of input is read.
As Joop Eggen writes, you need to make sure your standard output is not written in blocks in order to make sure that each line of output appears immediately after a print statement has been issued. This can be done by setting the $| variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am using CGI in strict mode and a bit confused with variables. I am reading a file that has two lines. Storing both in two variables. But when i try outputing them using html, it says global variable error
This is what I am doing
open TEXT, "filename";
$title = <TEXT>;
$about = <TEXT>;
close TEXT;
but this gives the global variable error. whats the best way to fix this?
You need to declare variable with my to make its scope local. This is the best practice and compulsory when using strict
use strict;
use warnings;
open my $fh, '<', 'filename' or die $!;
my ( $title, $about ) = <$fh>;
close $fh;
Further improvements:
Avoided bareword file handles (e.g. FILE). Instead use local file handles such as my $fh
Used error handling with die when dealing with file processing
Combined assignment of $title and $about as suggested by #Suic
use warnings to display what's going wrong as pointed out by #TLP
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove every lines in a text file that contain the word "script" and then write whatever left into another file. But keep the originl file.
like this:
open the file
delete any line with the word "script"
then output whatever left after delete to another file.
perl -ne '/script/ or print' file > newfile
grep -v script original.file > new.file
Or if you really need perl:
#!/usr/bin/perl
use strict;
use warnings;
open(my $in, '<', 'input.txt')
or die "Cannot open input.txt: $!";
open(my $out, '>', 'output.txt')
or die "Cannot open output.txt: $!";
while (<$in>) {
print $out $_ unless /script/;
}
close($in);
close($out);
Finally, if you are only looking to match "script" if it is a word (and not part of a bigger string like "prescription" or "scripting") then change:
/script/
To:
/\bscript\b/