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 6 years ago.
Improve this question
I'm running a perl script with a lot of input options, one of them being:
'errorcode=s{1,}' => \#ecodes,
I have a die at the end of the GetOptions if anything entered doesn't match the input. However if I input '--ecode 500' the program runs.
Why isn't the script dying? If I try something else like '--testing 123' it does die.
I'm guessing you have a option with a required argument such as
"foo=s" => \$foo,
and that you did something like
program --foo --ecode 500
which puts --ecode in $foo and 500 in #ARGV.
$ perl -MGetopt::Long -e'
use feature qw( say );
GetOptions("foo=s" => \$foo)
or die "usage\n";
say "ok <$foo> <#ARGV>";
' -- \
--ecode 500
Unknown option: ecode
usage
$ perl -MGetopt::Long -e'
use feature qw( say );
GetOptions("foo=s" => \$foo)
or die "usage\n";
say "ok <$foo> <#ARGV>";
' -- \
--foo --ecode 500
ok <--ecode> <500>
Related
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
I'm asking about most simple way to
Get www content (with curl I think)
Search and replace text with tree regular expression (s/a/b/; and s/as/da' etc)
Must I define variables every time when I want use a Perl command? In bash I piped the output from curl:
curl www.google.pl | sed 's/a/b'
I want to omit of these $dupa lines
$#!/usr/bin/perl
$dupa = `curl -s https://api.binance.com/api/v1/ticker/allPrices`;
$dupa =~ s/"},\{"/\n/g;
$dupa =~ s/":"/=/g;
$dupa =~ s/","/\n/g;
$dupa =~ s/\[{"//g;
print $dupa;
Always use strict in Perl code. No exceptions!
Don't shell out to curl to make an HTTP request. Use a Perl module like LWP::Simple.
The data you are downloading is JSON. Don't manipulate it as a string; use a JSON parser, like JSON::XS, to convert it to a data structure.
Here's a start:
use strict;
use LWP::Simple qw( get );
use JSON::XS qw( decode_json );
use Data::Dumper qw( Dumper );
my $data = decode_json(get("https://api.binance.com/api/v1/ticker/allPrices"));
print Dumper($data);
You can now manipulate the data structure in the $data variable; for instance:
for my $item (#$data) {
print "$item->{symbol} : $item->{price}\n";
}
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 6 years ago.
Improve this question
I am looking for a Perl script which does the following:
takes two options, like script.pl -option1 -option2
Option 1 has two choices, say choice1 and choice2 and I have shell code
to do both actions (which I hope I can port to Perl)
Here option 2 is the path and is optional. If it's not specified it uses the current directory
Desired script
checks arguments
if choice 1 :
go to path (arg2)
run a code (i have it ready)
If choice 2
go to path (arg2 )
run a code
(not tested)
#! /usr/bin/perl
use warnings;
use strict;
my ($action, $path) = #ARGV;
$path //= '.';
chdir $path or die "Cannot chdir to $path.\n";
my $script = {
choice1 => 'script1.sh',
choice2 => 'script2.sh',
}->{$action};
die "Invalid choice $action.\n" unless defined $script;
0 == system $script or die "Status: $?";
Run as script.pl choice1 /path.
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 9 years ago.
Improve this question
Can anyone tell me why I'm getting this:
usage: gen-non-random.pl <count> <outputfile>
From the code below:
#!/usr/bin/perl -w
#
# Script to generate non random values, to demonstrate a bad randomness graph
# for my "Howto Analyse SessionIDs".
#
# written by:
$version = "0.0.4";
$filename = "gen-non-random.pl";
$usage = "usage: $filename <count> <outputfile>\n";
$count = $ARGV[0] or die ("$usage\n");
$output = $ARGV[1] or die ("$usage\n");
print ("-- $filename Version: $version\n");
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use Math::Random;
use Digest::MD5 qw(md5_hex);
open (OUT, ">$output") or die ("Can't open $output\n");
for ($i=0; $i<$count;$i++)
{
# generate a random number
$random = random_uniform();
# cut out char 3-9 of $random and put it in $randsub
$randsub = substr($random, 2, 6);
# get seconds and microseconds since epoch
($seconds, $microseconds) = gettimeofday;
# get the last two chars of the seconds and put them into $s
$s = substr($seconds, 8, 2);
# sleep for a while
usleep $randsub;
# put together the last two digits of seconds and the microseconds
$time = $s . $microseconds;
$md5_time=md5_hex($time);
# print out the stuff we put together above
print OUT ("$md5_time\n");
}
close (OUT) or die ("Can't close $output\n");
print ("$count values written to $output\n");
exit;
I am new to programming so i need really simple answer please! I do not own this code I am using for my research paper at University. Also, could someone please explain to me what Usage actually is i can't seem to find a good explanation for it?
Thanks.
You're getting that error because you're not using the program correctly:
usage: gen-non-random.pl <count> <outputfile>
This basically means you have to provide a count and output file as arguments, such as:
perl gen-non-random.pl 42 outfile.txt
This will generate forty-two numbers and output them to the outfile.txt file.
It's the two lines near the start, checking ARGV[0/1] and die-ing if you don't provide them, that are outputting this message and exiting the program.
Hmmm. I can't run the above code because Time::HiRes::ualarm() is not implemented on Windows. That said, it appears to be generating a MD5 has string of the current time (in integer form) after sleeping for a random number of seconds, then dumping the result into a text file. You are getting the usage message mentioned above because the program expects input. Try running it from the command line like so:
perl gen-non-random.pl 10 MyResults.txt
I suspect that will dump 10 HD5 hash results into a file called "MyResults.txt".
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
This is part of Shell script.
This is to check disk Usage.
I want to know how this could convert to perl script ?
Please help me for this.
DFCMD="df -P"
for fs in `eval $DFCMD`; do
FS=`echo $fs | awk ' { print $1 } '`
done
Thanks
perl -le 'print /(\S+)/ for `df -P`'
There is probably a 1liner to do it, but I'd do this (un tested)
my #command = qw/df -P/;
open(my $in, "-|", #command) or die "Can't run `#command`: $!";
while(<$in>) {
my ($first_col) = split;
print "$first_col\n";
}
well, in perl, it is often use the system() function to execute the shell command, but the system() function can not return the output of the command.You can use backticks (``) to get the output. Use the out put, you can convert the shell script to perl script
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my #output = split('\r\n', `df -P | awk '{print \$1}'`);
6 print #output;
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.