我已经为此挣扎了一段时间,并且不熟悉Perl的核心对象系统。这是一个较大项目的简化部分,问题就发生在这里。我试图编写一个类, Package 另一个类,将.xlsx电子表格文件输出到磁盘上。除非我在do_一些子程序。如果我包括die语句,它将停止,但.xlsx文件仍将写入光盘,直到die语句的点。它将不包括#如果我省略die语句,它将完全按照预期工作,打印所有内容并优雅地关闭。
是什么原因导致这个“ioctl”错误,我如何修复它?这在我的大项目失败,并将输出文件到光盘,但不包括从子程序写入的行,即使没有die语句。任何帮助将不胜感激。谢谢!
#!/usr/bin/perl
package TestClass;
use Excel::Writer::XLSX;
use POSIX qw(strftime);
use Data::Dumper;
use strict;
use diagnostics;
sub new{
my $class = shift;
my $workbook = Excel::Writer::XLSX->new('test.xlsx');
my $worksheet = $workbook->add_worksheet('demo');
my $format = $workbook->add_format(
color => 'black',
bold => 1
);
my $self = {
format => $format,
workbook => $workbook,
worksheet => $worksheet
};
bless $self, $class;
$worksheet->merge_range( 0,0, 0,2, (strftime '%F %I:%M %P', localtime), $format );
# $self->{worksheet}->write( 2,2, "Check", $format );
$worksheet->write( 2,2, "Check", $format );
&do_something( $self, 3,3, "Mate" ); # Main problem
$worksheet->write( 4,4, "Match", $format ); # Doesn't write with 'die'
$workbook->close();
return $class;
}
sub do_something{
my ( $self, $y, $x, $txt ) = @_;
print Dumper( $self->{worksheet} ), "\n";
print ref( $self->{worksheet} ), "\n\n";
# Line below writes but dies.
$self->{worksheet}->write( $y,$x, $txt, $self->{format} ) or die "Didn't work: $!";
return 1;
}
1;
运行时出现错误消息:
Uncaught exception from user code:
Didn't work: Inappropriate ioctl for device at ./TestClass.pm line 47.
TestClass::do_something(TestClass=HASH(0x55c438170d88), 3, 3, "Mate") called at ./TestClass.pm line 33
TestClass::new("TestClass") called at -e line 1
调用Dumper()和ref()的输出:
sudo perl -e "require './TestClass.pm';TestClass->new();"
$VAR1 = bless( {
'_dim_rowmin' => 0,
'_rstring' => '',
'_filter_cols' => {},
'_tempdir' => undef,
'_default_row_pixels' => 20,
'_margin_top' => '0.75',
'_fh' => undef,
'_external_background_links' => [],
[ Snipped for brevity ]
'_default_date_pixels' => 68,
'_xls_rowmax' => 1048576,
'_col_size_changed' => 0,
'_set_rows' => {},
'_outline_on' => 1,
'_str_total' => \2,
'_write_match' => [],
'_shape_hash' => {}
}, 'Excel::Writer::XLSX::Worksheet' );
Excel::Writer::XLSX::Worksheet
屏幕截图时,一切都工作,因为它应该和'死'是注解了:Spreadsheet img
1条答案
按热度按时间laawzig21#
从文件来看,
write方法返回:
$self->{worksheet}->write
在成功时返回零。当调用 * 成功 * 时,你实际上导致了程序失败。将or
更改为and
。这里停止使用
$!
。$self->{worksheet}->write
不是一个系统调用,也不会发出设置$!
或errno
的信号(C变量$!
代理)。$!
即使出错也没有意义。