Read and compare images in folder using Perl - perl

I am trying to read and compare large number of image files in a folder. But I am not able to read the image file.
Code snippet :
use strict;
use warnings;
use Image::Compare;
opendir my $ref_dir, "./" or die "Cannot open directory: $!";
my #ref_files = readdir $ref_dir;
closedir $ref_dir;
print $#ref_files;
my($cmp) = Image::Compare->new();
$cmp->set_image1(
img => './'.$ref_files[0], #one instance - reading first file in the folder
type => 'bmp',
);
The code is throwing the following error - "Unable to read image data from file './.': 'Could not open ./.: Permission denied' at C:/Perl64/site/lib/Image/Compare.pm line 162. "
The code works fine if I provide the file name directly - img => './image.bmp'. So it can't be a permission issue.

The error message seems pretty clear.
Unable to read image data from file './.': 'Could not open ./.: Permission denied
The first file you're getting back from opendir() is the current directory (.) - and it's no surprise that Image::Compare can't get the image data that it wants from that file!
Perhaps you should add a filter to only return the files you're interested in.
my #ref_files = grep { -f } readdir $ref_dir;

Related

Write text to file in Flutter(Dart)

I want to be able to create a new file and write a string into it. Examples that I can find all seems to pertain to writing to an existing file.
String fileName = 'myFile.txt';
String contents = 'hello';
void writeToFile(String fileName, String contents){
File outFile = File('content://com.android.providers.media.documents/document/document/document_root/' + fileName);
outFile.writeAsString(contents);
}
This results in the following error, as expected
Unhandled Exception: FileSystemException: Cannot open file, path = 'content://com.android.providers.media.documents/document/document/document_root/myFile.txt' (OS Error: No such file or directory, errno = 2)
How can I create a new file in which I can write my contents?
Are you sure that the path exists? writeAsString does create the file if it doesn't exists, but it doesn't create the full folder structure up to the file you're trying to write to. Also, you might not have the rights to write in the path you've specified.
Anyway, you'd better use this plugin to get the folders' path instead of hard-coding them.

Specman e error: No match for file when using "for each line in file"

I have a my_text.txt file and the next e file in the same directory:
extend my_unit {
run() is also {
for each line in file "my_text.txt" do {
// ...
};
};
};
I get the next run error:
*** Error: No match for file 'my_text.txt'
Why do I get the error? How the for loop over all lines in a text file should be used?
Thank you for your help
The file name is looked for relatively to your working directory (from which you run Specman), not relatively to the source file in which the code is written.
It does consider SPECMAN_PATH, but probably your SPECMAN_PATH does not point directly to that directory.

Download multiple files from SFTP using perl Net::SFTP::Foreign

I am using the below code to download files from SFTP and I am getting error message after one file download. Then I am rerunning the program and then it downloads only one file again and when trying to download the next file in the array it get failed. I couldn't understand the reasons for this failure and kindly review the below code and share opinions.
#!/usr/bin/perl
use strict;
use Net::SFTP::Foreign::Constants qw(:error :status);
use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Attributes;
my $SFTP_CON=Net::SFTP::Foreign->new("sftpserver.test.com",user=>"testuser",password=>"pass");
my #SFTP_Files=('/test/1.txt','/test/2.txt','/test/3.txt');
my $root_dir='/test/';
$local_dir='/u/test/';
foreach my $file_to_dwn(#SFTP_Files){
my $file_to=$file_to_dwn;
$file_to=~s/$root_dir/$local_dir/igs;
unless($SFTP_CON->get("$file_to_dwn","$file_to")){
if (($SFTP_CON->error == SFTP_ERR_REMOTE_STAT_FAILED or
$SFTP_CON->error == SFTP_ERR_REMOTE_OPEN_FAILED) and
$SFTP_CON->status == SSH2_FX_NO_SUCH_FILE) {
print "Remote file does not exist!";
exit;
}
else {
print "Transfer Failed";
exit;
}
print $SFTP_CON->error."\n";
exit;
}
}
I am getting "Transfer Failed", when the program tries to download the one after another (second file download).
Please do needful.

Images uploaded with Via Perl Net::FTP get corrupted

Why am I always getting corrupted image file when uploading to FTP server? .gif image doesn't get corrupted, only .jpeg/jpg and .png get corrupted.
sub png{
my $ftp=Net::FTP->new($fhost)or die &ftpErr;
$ftp->login($hostname, $hostpass);
my $img=$ftp->put("$file");
$ftp->get($img);
$ftp->quit;
our $image="$img";
our $shot=$window->Photo(-format=>'png',-file=>"$image");
$window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
sub jpeg{
my $ftp=Net::FTP->new($fhost)or die &ftpErr;
$ftp->login($hostname, $hostpass);
my $img=$ftp->put("$file");
$ftp->get($img);
$ftp->quit;
our $image="$img";
our $shot=$window->Photo(-format=>'jpeg',-file=>"$image");
$window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
You are transferring the files in the default mode, which is ASCII. This mode translates line ends. To transfer binary files use binary mode:
$ftp->binary;
$ftp->put(...);
$ftp->get(...);

Error reason from Perl Net::FTP

I'm using Net::FTP to transfer files up to a mainframe and I'm testing failure conditions.
My code is basically along the following lines:
my $ftp = Net::FTP->new ("mainframe.com", Timeout => 20);
if (! $ftp) {
logMessage ("Could not connect to host: $!");
return;
}
if (! $ftp->login ("paxdiablo", "demigodemporeroftheuniverse")) {
logMessage ("Could not log in to host: $!");
$ftp->quit ();
return;
}
if (! $ftp->put ("myfile.txt", "'CANT.WRITE.TO.THIS'")) {
logMessage ("Could not put file: $!");
$ftp->quit ();
return;
}
I know I can't create the data set CANT.WRITE.TO.THIS since I don't have the required permissions but, when I try, the only message I see is:
Could not put file:
There is no indication in $! as to what the problem was. I've looked in the Net::FTP doco and all it says is:
put ( LOCAL_FILE [, REMOTE_FILE ] )Put a file on the remote server. LOCAL_FILE may be a name or a filehandle. If LOCAL_FILE is a filehandle then REMOTE_FILE must be specified. If REMOTE_FILE is not specified then the file will be stored in the current directory with the same leafname as LOCAL_FILE.Returns REMOTE_FILE or the generated remote filename if REMOTE_FILE is not given.
I also cannot find anything there about retrieving the specific error (like $ftp->getLastError() or something similar).
How can I indicate to the user why the transfer failed?
On an earlier iteration, I resorted to putting the file, then getting it again and checking contents locally. I'd really rather not have to inflict such kludgy code on people again.
From Net::FTP:
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $#"
Note $#.
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
Note $ftp->message