Variable scope in perl [closed] - perl

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

Related

what does it mean 'push #{bla bla }' in Perl? [closed]

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 read try to read perl code of annovar and there is a line like this:
push #{$genedb{$chr, $nextbin}}, [$name, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, [#exonstart], [#exonend], $name2];
Can someone explain what does it mean?
which values put which array or hash ?
Just run this program and Data::Dumper will show the results
#! /usr/bin/env perl
use warnings;
use strict;
use utf8;
use feature qw<say>;
use Data::Dumper;
my %genedb;
my $chr = 'G';
my $nextbin = 4143;
push #{$genedb{$chr, $nextbin}}, [1..10];
print Dumper(\%genedb);
exit(0);

Perl File Update After Each Print Statement [closed]

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

How to add optional argument in perl [closed]

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.

Processing Time [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does the following script take so many cycles to complete?
The document it is sifting through is 20590 lines long and each line consists of the following, or a variation thereof;
"10160354001 ALGIERS ALGERIA 36.70 3.60 290"
I am trying to make a database to match the first number to the last 3.
The actual program is however taking several seconds per line.
Script:
#! /usr/bin/perl
$path = shift(#ARGV);
open COORDS, $path or die "Couldn't open file\n";
my %coords = {};
foreach $line (<COORDS>){
$stat = substr $line,0,11;
#coords = grep(($_!=undef),split(/ /,substr $line,42));
$coords[$stat]=#coords;
}
print $coords['10160354001'];
close(COORDS);
$coords['10160354001'] = ... is an assignment to an array element, and a large one at that. This statement will cause Perl to allocate an array with room for at least 10160354002 elements.
You meant to use a hash:
$coords{$stat} = "#coords";
...
print $coords{'10160354001'};
use strict and use warnings would have alerted you to this and other problems with your code.

Read video file using Perl [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can anyone suggest to me how to read a video file from Perl without using any third-party tools?
I know the opencv library for Python and C. I am not sure which one to use for Perl.
UPDATE
I get the output as
3 bytes read
´ˇÙ
Argument "M-+M-^?M-t" isn't numeric in bitwise and (&) at
0
I am novice in perl and I am missing something. I am reading 3 bytes from the file till EOF. I want to mask it and do some manipulation on the bits. I am reading pack/unpack it really doesn't make a clue to me.
open (FILE, "<:raw", $InputFile) or die "Couldn't open";
my ($buf, $data, $n);
while (($n = read FILE, $data, 3) != 0) {
print "$n bytes read\n";
$buf = $data;
print $buf . "\n";
my $maskNumber = 0x4;
my $value = ($buf & $maskNumber);
print $value . "\n";
}
Perl's bit operators have string modes and numeric modes; if either parameter is a number, the numeric mode is used.
So I suspect you want something like:
$buf & "\0\0\4"