使用初始化值错误

时间:2018-06-12 18:41:00

标签: perl initialization imagemagick

我继承了一个使用Image::Magick的Perl程序 更改图像。

Perl代码

#!/usr/bin/perl

#use strict;
use warnings;

use Data::Dumper;
use Net::SMTP;
use Image::Magick;
use File::Find::Rule;
use File::stat;
use File::Type;
use File::Copy "cp";

my $maxFileSize        = 400;
my $errorLog           = 'C:/Users/bdmsadmin/Desktop/test/testlog.txt';
my $startDir0          = 'C:/Users/bdmsadmin/Desktop/test/img';
my $badImageTag        = 'Kofax standard Multi-Page TIFF Storage Filter v3.03.000';
my $alreadyModifiedTag = 'Image Modified by xtenderImageCheck.pl for correction of incorrect scanning settings';
my $fileTypeBMP        = 'image/x-bmp';
my $fileTypePDF        = 'application/pdf';
my $fileTypeJPEG       = 'image/jpeg';
my $fileTypeTIFF       = 'image/tiff';
my $ffr                = 'File::Find::Rule';
my $fileExt            = '*.bin';
my $compressionGROUP4  = 'Group4';
my $compressionJPEG    = 'JPEG';
my $compressionLZW     = 'LZW';
my $rule               = $ffr->file->name( '*.jpg', '*.jpeg', '*.tif', '*.png', '*.bin' )->start( $startDir0 );
my $warnstring         = 'Exception 3';
my $warnstring2        = 'Deprecated and troublesome old-style JPEG compression mode';
my $imageComment       = 'Dummy';
my $datestring         = localtime();

open( ERRORLOG, ">>$errorLog" ) or die "Error opening $errorLog: $!";

while ( my $file = $rule->match() ) {

    my $copy = $file;
    $copy =~ s/\.bin$/.org/i;
    cp( $file, $copy ) or die print "Copy failed";

    my $sb = stat( $file );

    if ( $sb->size > $maxFileSize ) {

        print "Processing $file\n";

        my $objFileType = File::Type->new();
        my $fileType    = $objFileType->checktype_filename( $file );

        next if $fileType eq $fileTypePDF;

        my $image = Image::Magick->new();

        open IMAGE, ">>$file" or die "Error opening $file: $!\n";
        binmode( IMAGE );

        my $x = $image->ReadImage( "$file" );
        if ( $x ) {
            print ERRORLOG "ERROR: $file --> $x\n";
        }

        my $warncheck  = index( $x, $warnstring );
        my $warncheck2 = index( $x, $warnstring2 );

        next if $x and ( $warncheck < 0 or $warncheck2 >= 0 );

        my $imageType    = $image->GetAttribute( 'type' );
        my $imageComment = $image->GetAttribute( 'comment' );

        #next if $imageComment eq $alreadyModifiedTag;
        print "$image\n";
        print "$imageComment\n";
        print "$alreadyModifiedTag\n";

        if ( $imageComment eq $alreadyModifiedTag ) {
            print "$file has already been optimized. Skipping...\n";
            print ERRORLOG "$file has already been optimized. Skipping...\n";
            next;
        }

        if ( $imageType eq 'Bilevel' ) {

            $image->SetAttribute( option => 'quantum:polarity=min-is-white' );
            $image->SetAttribute( comment => $alreadyModifiedTag );
            $image->SetAttribute( monochrome => 'true' );
            $image->SetAttribute( mime   => $fileTypeTIFF );
            $image->SetAttribute( magick => 'tif' );
            $image->SetAttribute( compression => $compressionGROUP4 );

            $image->Write( filename => $file );

            close( IMAGE );

            undef( $image );
        }
        elsif ( $imageType eq 'Grayscale' ) {

            $image->SetAttribute( option => 'quantum:polarity=min-is-white' );
            $image->SetAttribute( comment => $alreadyModifiedTag );
            $image->SetAttribute( monochrome => 'true' );
            $image->SetAttribute( mime   => $fileTypeTIFF );
            $image->SetAttribute( magick => 'tif' );
            $image->SetAttributes( compression => $compressionJPEG, quality => 60 );
            $image->Write( filename => $file );

            close( IMAGE );

            undef( $image );
        }
        elsif ( $imageType eq 'TrueColor' ) {

            $image->SetAttribute( option => 'quantum:polarity=min-is-white' );

            print ERRORLOG "TrueColor \n";

            $image->SetAttribute( mime   => $fileTypeTIFF );
            $image->SetAttribute( magick => 'tif' );
            $image->SetAttribute( alpha  => 'off' );
            $image->SetAttributes( compression => $compressionLZW );
            $image->SetAttribute( comment => $alreadyModifiedTag );
            $image->Write( filename => $file );

            close( IMAGE );

            undef( $image );
        }
        else {
            print ERRORLOG "ERROR:---> No Handler for this image type! -->File: $file of type: $imageType\n";
            close( IMAGE );
            undef( $image );
        }
    }

    my $sFile = -s $file;
    my $sCopy = -s $copy;

    print ERRORLOG "Size $file = $sFile\n";
    print ERRORLOG "Size $copy = $sCopy\n";

    if ( $sFile > $sCopy ) {
        print ERRORLOG "Deleting $file\n";
        unlink( $file );
        my $original = $file;
        $original =~ s/\.org$/.bin/i;
        rename( $copy, $original ) or die print ERRORLOG"Rename failed";
    }
    else {
        print ERRORLOG "Deleting $copy\n";
        unlink( $copy );
    }
}

close( ERRORLOG );

open my $fileHandle0, ">>", "$startDir0/optimized.txt" or die;
print $fileHandle0 "$startDir0 optimized on $datestring\n";
close $fileHandle0;

exit 0;

我在这行收到错误

if ($imageComment eq $alreadyModifiedTag) 
  

在...的字符串eq中使用未初始化的值$ imageComment

之前两行的打印行给出了不同的错误

  

在连接(。)或字符串中使用未初始化的值$ imageComment ...

当我打印出变量时,它是空白的,尽管事实上我已经初始化它(据我所知)。更熟悉Perl的同事建议我不要使用strict,但这没有帮助。我很确定它与示波器有关,但我不知道Perl能够解决这个问题。它看起来像是在范围内,但我不能让它工作。我尝试添加my并查看类似帖子,但我无法弄清楚。任何帮助表示赞赏。

0 个答案:

没有答案