perl 如何解决在void上下文中无用地使用私有变量,在除法(/)中使用未初始化值$interval以及在[closed]处非法除以零

laik7k3q  于 2023-11-22  发布在  Perl
关注(0)|答案(1)|浏览(209)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8年前关闭。
Improve this question
我没有任何使用Perl的经验,所以我不能解决这个问题。你能帮我解决这个问题吗?

  1. Error 1 : Useless use of private variable in void context at ./createnodelist.pl line 51.
  2. Error 2 : Use of uninitialized value $interval in division (/) at ./createnodelist.pl line 10.
  3. Error 3 : Illegal division by zero at ./createnodelist.pl line 10.

字符串
基本上,下面的脚本应该加载证书列表,并生成一个节点列表,该节点分配给节点:

  1. line 51 print FILE "\n";
  2. line 10 my $jph = ceil(60/$interval);


源代码:

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use POSIX;
  5. my $interval = $ARGV[0];
  6. my $path = '/etc/puppet/modules/puppet/scripts/puppet_cron';
  7. # Calculate how many jobs per hour we have
  8. my $jph = ceil( 60 / $interval );
  9. # Load list of all nodes
  10. my @nodes = `/usr/sbin / puppetca -la | /usr/ bin / cut -d ' ' -f 2`; #
  11. chomp(@nodes);
  12. # Count number of nodes
  13. my $node_count = scalar(@nodes);
  14. # Count number of nodes per group
  15. my $nodes_per_group = $node_count / $interval;
  16. # Process nodes list and assigne minutes for each node
  17. open( FILE, ">$path/nodes.list" );
  18. for ( my $i = 0; $i < $interval; $i++ ) {
  19. my $minute;
  20. my $group = $i + 1;
  21. my $node_n;
  22. # Process nodes in group
  23. for ( $node_n = 0; $node_n < $nodes_per_group; $node_n ) {
  24. # Assign minutes to a node
  25. my $node = shift(@nodes);
  26. if ($node) {
  27. print FILE "$node;";
  28. for ( my $n = 0; $n < $jph; $n++ ) {
  29. $minute = $i + ( $interval * $n );
  30. if ( $minute
  31. < 60 ) # Only print minutes that actually exist
  32. {
  33. print FILE "$minute";
  34. }
  35. if ( $n != $jph - 1 ) {
  36. print FILE ',';
  37. }
  38. }
  39. }
  40. $node_n++;
  41. print FILE "\n";
  42. }
  43. }
  44. close(FILE);
  45. exit 0;

mfpqipee

mfpqipee1#

解决Perl问题的第一条规则:
设置use strict;use warnings;
第二:阅读错误信息。
你的问题在第10行:除以零。唯一的可能性是$interval为零。或者未定义。或者未初始化。如果你打开strict/warnings,那么它可能会给予你一个关于后者的警告。
第51行:在void上下文中无用地使用私有变量-基本上意味着该语句没有做任何事情。可能意味着FILE没有被正确打开。
编辑:发布您的代码:$interval被设置为$ARGV[0]。您在命令行上提供了什么参数?如果没有提供值,那么您将得到除以零错误,因为$interval未定义。
从你的评论-你调用它没有参数。因此$interval将永远是未定义的,这将给你给予你想要的错误消息。要么:

  • 使用参数调用脚本
  • $interval设置默认值,例如my $interval = $ARGV[0] || 60;

对于你的第二个问题-在void上下文中无用地使用私有变量:

  1. for ( $node_n = 0; $node_n < $nodes_per_group; $node_n ) {

字符串
你没有递增$node_n,它是在一个空的上下文中使用的。因此出现了警告。

  1. for ( $node_n = 0; $node_n < $nodes_per_group; $node_n++ ) {

展开查看全部

相关问题