简体   繁体   English

Perl:在子例程中修改ImageMagick对象

[英]Perl: modify an ImageMagick object in a subroutine

I'm using Perl (5.16) and ImageMagick (6.8.8). 我正在使用Perl(5.16)和ImageMagick(6.8.8)。 I'd like to send a new ImageMagick object to a subroutine by reference and modify the object there, but I get "Can't call method "Read" on unblessed reference". 我想通过引用将新的ImageMagick对象发送到子例程并在那里修改该对象,但是我收到“无法在无祝福的引用上调用方法“读取””。 Obviously, I'm not treating the object properly in the subroutine. 显然,我没有在子例程中正确处理对象。 Can anyone help? 有人可以帮忙吗? Thanks. 谢谢。

my $im=Image::Magick->new;
ModifyImage(\$im,$f);

sub ModifyImage  {
    my $im=shift;
    my $file=shift;
    my $res = $im->Read($file);
    warn $res if $res;
}

Your Image::Magick object $im already holds a reference to the data. 您的Image::Magick对象$im已经保存了对该数据的引用。 You don't need to take a reference to the variable, and your call should look like 您不需要引用该变量,并且您的调用应类似于

ModifyImage($im, $f);

And I would write the subroutine like this 我会这样写子程序

sub ModifyImage {
    my ($im, $file) = @_;

    my $res = $im->Read($file)
    warn $res if $res;
}

to make it more concise, and to make it clear that $im and $file are parameters. 为了更加简洁,并明确说明$im$file是参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM