A Perl User Input program can't be executed in Ubuntu [closed] - perl

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
I'm a beginner of Perl, using Ubuntu 12.04 as my operating system.
My perl version is v5.14.2.
I wrote a simple program to read user input then print it.
#!/usr/bin/perl
print "please enter your name:";
my $name = ;
print "\n";
print "hello, $name\n";
When I tried to execute this program it turned out to be like this,
syntax error at input.pl line 3, near "=;"
Execution of input.pl aborted due to compilation errors`
But this is a sample code which I copied from a perl textbook, so it shouldn't be any mistake in the code.

that code makes no sense, as the compiler tells you:
my $name = ;
write something like:
my $name = <>;

To be more explicit:
my $name = <STDIN>;
which reads from the standar input, or:
my $name = readline(*STDIN);
If you do my $name = <>;, it will read either from a file specified on the command line or from STDIN if no file is specified

Related

Using command line argument as a file name in perl [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 5 years ago.
Improve this question
The following perl script takes an argument from the command line and use that as the output file name.
my $str = $ARGV[0];
my $out_handler= Bio::SeqIO->new(
-file => $str,
-format => 'Fasta'
);
It is similar to this example. while I run perl test.pl polg, I get this error
------------- EXCEPTION -------------
MSG: Could not read file 'polg': No such file or directory
STACK Bio::Root::IO::_initialize_io /opt/perl/lib/site_perl/5.14.2/Bio/Root/IO.pm:268
STACK Bio::SeqIO::_initialize /opt/perl/lib/site_perl/5.14.2/Bio/SeqIO.pm:513
STACK Bio::SeqIO::fasta::_initialize /opt/perl/lib/site_perl/5.14.2/Bio/SeqIO/fasta.pm:87
STACK Bio::SeqIO::new /opt/perl/lib/site_perl/5.14.2/Bio/SeqIO.pm:389
STACK Bio::SeqIO::new /opt/perl/lib/site_perl/5.14.2/Bio/SeqIO.pm:435
STACK toplevel test.pl:16
-------------------------------------
How can I fix that?
Your program is trying to read from that file and failing because it doesn't exist
Your example code on github has this
-file => '>chrom20.fasta'
so I suggest you should try
-file => ">$str"
or you can run your program as
perl test.pl >polg
Also, I'm sure you can think of a better name than $str for a scalar variable containing an output file name. $str is only fractionally better than $var

Unable to swap the variables using perl? [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 5 years ago.
Improve this question
Here by i tried to swap the variables using perl.
#!/usr/local/bin/perl
use strict;
use warnings;
my $v1=23;
my $v2=43;
$v1,$v2)=($v2,$v1)
print $v1,$v2;
Error:
syntax error at exchange.pl line 7, near ")
print"
Execution of exchange.pl aborted due to compilation errors.
Excepted output:
43,23
You forgot open parens and semicolon:
($v1,$v2)=($v2,$v1);
print $v1,$v2;
Expected output should be 4323 (4 chars and no comma), not 43,23.
To print 43,23:
print "$v1,$v2";

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.

Write to standard output [closed]

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.

$ARGV[0] is not working in Solaris SPARC [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 9 years ago.
Improve this question
I used $var=$ARGV[0] in some Perl code on a Solaris x64 machine and it is receiving the argument correctly.
But the same piece of code is not working in Solaris SPARC.
Any clue?
Also $_[0] is working in Solaris SPARC, but then it is not working in Solaris x64.
Is there any other way?
Try this program:
use strict;
use warnings;
print join ": ", #ARGV . "\n";
Run it with a bunch of command line arguments, and tell me what you're getting as an output. It should look something like this:
$ myprog.pl one two three four five
one: two: three: four: five
Next, try the same thing with this program:
use strict;
use warnings;
print join ": ", #ARGV . "\n";
my $value = $ARGV[0];
print qq(My value = "$value"\n);
Now, edit your question to show us the output you're getting. This way, we'll know what you mean. Also, give us at least a code snippet of what is not working, what you expect, and what you're getting.
Writing a quick etest program is always a good way to track down an issue, and can give you something to post on Stackoverflow if you're still stuck.