I'm trying to fix a problem for a day and after many unfructuous searches i'm here !
I'm trying to use the binary convert from the ImageMagick suite on a Perl script to crop an picture.
When i try this simple code :
use Shell qw(convert);
convert("-crop", "50%x50%", $Src, "new_image.png");
On a single file that works perfect.
But when i try to implement it on the project i'm working on (i'm on internship ,fixing features now), the message "Can't exec convert : Bad file descriptor" appears !
I checked the path of $Src, try to remplace by hard code, set PATH, nothing change.
BUT
if i try a simple:
convert(-h);
That Works perfect !
I supposed something in the previous code may be interfering but not sure.
Ps : The project is launched from a php server hosted in local, donno if it change something but if never !
Thanks a lot for reading and thank too for answering !
---EDIT---
Actual code as asked
Use Shell qw(convert);
.
.
.
sub checkSubScreen
{
#Parameters init
my ($this, $Scr0, $Scr1) = #_;
convert("-crop", "50%x50%", $Scr0, "tmp/scr0_crp.png");
convert("-crop", "50%x50%", $Scr1, "tmp/scr1_crp.png");
....
I can't show you the entire code it's really too big but just ask and i'll try to show !
The error message comes from Shell. I don't know what anyone thinks they are gaining by using Shell instead of the more traditional system/qx/pipe open/IPC::Open3 toolkit, and in particular I don't see what you are gaining by using Shell.
Try the more traditional system call
#args = ("50%x50%", $Src, "new_image.png");
system("convert", #args) and warn "convert #args: exit code was $?";
and see if you get a more traditional error message.
Ok finally i decided to use the PerlMagick Api it works, i think my problem was due to the environment and probably very specific.
Thks for answers !
Related
I would like to translate all of the system commands in my script to Win::32::Process::Create commands. CPAN tells me the syntax:
Win32::Process::Create($obj,$appname,$cmdline,$iflags,$cflags,$curdir)
So, I tried to apply it:
Win32::Process::Create( $Win32processObj,
"C:\\Perl64\\bin\\perl.exe",
"'C:\\Users\\script.pl','$arg'",
0,
NORMAL_PRIORITY_CLASS,
"." ) || die "Failed to create process.\n";
When I run this, I don't get an error, but I don't start a new process either...
When I use GetProcessID(), I get a pid, but it doesn't correspond to anything in the tasklist... (I'm assuming the created process ends before I can see it displayed in the tasklist).
According to your comment, Windows says it created the process. Accoding to your question, you even have its process id of the process you claim never got created. I'm gonna go out on a limb and say you are mistaken.
You should now check what code the process ended with. perl -E'say $!=THECODE;' might give you a hint. But chances are it's because you tell Perl to execute a file named 'C:\Users\script.pl' (as opposed to C:\Users\script.pl).
I need to read program output using Net::SSH2. My problem is some of that data hided under the bottom of program output. In ssh-mode I need to enter "Return" on my keyboard to look up next. This is awkward for using in a perl-script =).
I know that Net::OpenSSH does it well, but I really need to use Net::SSH2. Does anybody know, how can I get it?
Thnx!
UPD: Some code below
my $ch = $ssh2->channel();
$ch->blocking(0);
$ch->shell();
print $ch "dir\n";
print $_ while <$ch>;
In this code I print output of command with "--More--" prompt of the terminal at the bottom.
Simple Net::OpenSSH "capture" method returns a whole data at the same time:
my #dirlist = $ssh->capture('dir');
Is it possible to do same thing using Net::SSH2?
I'm working on a simple Perl script to monitor a log file using the File::Tail module, but I can't seem to get the module working properly.
The idea is to use it over IRC, but this didn't seem to work so after tinkering with the interactive interpreter I've narrowed the problem down to File::Tail. I've cut it down the following basic example to monitor the file and nothing happens at all when new entries are appended to the file:
#!/usr/bin/perl -w
use strict;
use File::Tail;
my $file = File::Tail->new("/var/log/apache2/error.log");
while(defined(my $line = $file->read))
{
print "$line\n";
}
Can anyone suggest what the problem might be? I've gone through the perldoc entry and this is virtually copied from there so I can't really see that I've made any glaring errors. I'm running Ubuntu Lucid.
Is it possible that this is a permissions error? Can the user you're running the script as access /var/log/apache2/error.log?
If all else fails, implement it yourself!
use Fcntl qw(:seek);
while (1) {
while (<$fh>) {
...
}
sleep(1);
seek $fh, 0, SEEK_CUR;
}
...Which I wouldn't have had to type if I had simply linked you here or here.
I've just realised that I've been a complete tool...
It works perfectly - I just didn't leave it long enough before hitting Ctrl-C. D'oh! Oh well, lesson learned! Thanks for your help anyway!
I need to understand the working of this particular program, It seems to be quite complicated, could you please see if you could help me understanding what this program in Perl does, I am a beginner so I hardly can understand whats happening in the code given on the following link below, Any kind of guidance or insights wrt this program is highly appreciated. Thank you...:)
This program is called premove.pl.c
Its associated with one more program premove.pl,
Its code looks like this:
#!perl
open (newdata,">newdata.txt")
|| die("cant create new file\n");#create passwd file
$linedata = "";
while($line=<>){
chomp($line);
#chop($line);
print newdata $line."\n";
}
close(newdata);
close(olddata);
__END__
I am even not sure how to run the two programs mentioned here. I wonder also what does the extension of the first program signify as it has "pl.c" extension, please let me know if you know what it could mean. I need to understand it asap thats why I am posting this question, I am kind of short of time else I would try to figure it out myself, This seems to be a complex program for a beginner like me, hope you understand. Thank you again for your time.
First, regarding the perl code you posted:
See:
perlfunc open
perlfunc chmop
perlop on IO Operators, especially the null filehandle.
This is some atrociously bad code. I rewrote it for you without all the obfuscation and foolishness. This should be a bit more understandable.
#!perl
use strict;
use warnings;
use IO::File;
my $newdata = IO::File->new( 'newdata.txt', '>' )
or die "Unable to open newdata - $!\n";
while( my $line = <> ) {
$newdata->print( $line );
}
The main thing that is likely to be confusing is the <> or null filehandle read. Here it grabs a line from any files listed in program args, or if no arguments provided, it reads STDIN.
This script is essentially cat filename > newdata.txt
As for premove.pl.c, I'm no internals expert, but it looks like the author took an example for how to embed a Perl interpreter in a C program and pasted it into an oddly named file.
It looks to me like its will compile down to something equivalent to perl. In short, another useless artifact. Was the person who produced this paid by the line?
If this is the state of the code you've inherited, I feel sorry for you.
It looks like someone tried to run a Perl-to-C converter on your program so they could compile it to a C object. They probably thought this would make it faster. I'm guessing that you could ignore the .c file and use that Perl script directly.
Let's say I have the following simple script:
print "ID : ";
$ID = <>;
system (`comp program $ID`);
exec "task --shell";
When I use:
perl foo.pl | tee log.txt
The showing up problem is getting on screen a blink sign echo (waiting for the ID enter) before I even see the "ID : " (print instruction).
I need to keep on a log file all running output script (very long), notice that at the start of the run I have an interactive part that also need to be kept.
Is there a way inside PERL script or outside it, that keep the output stream exactly as it shown out on the screen, and what do you think is the simple & efficient way to conduct it?
I've noticed the IO::Tee , File::Tee moudles and Log4perl - someone can help me find the best way to use it ?
The best approach I have seen is to use script program.
You run script (program) - it gives you shell. in this shell you run anything you want. When you're done, you exit this shell, and everything that was on your screen will be in typescript file. Including all control codes.
There is also "bonus" - you can replay saved session with scriptreplay.
It's possible that your script is asking for user input before printing the "ID: " prompt because you don't have autoflush turned on. Try adding the following:
$|++;
print "ID : ";
$ID = <>;
system (`comp program $ID`);
exec "task --shell";
If this works then you might look at the Perl documentation about output buffering. perldoc -q buffer will explain what's happening in more detail as well as show some alternate ways of turning on autoflush.
If this doesn't solve the problem then it's possible that the tee command is buffering your output. In this case I would use script or screen as suggested by depesz or Manni
You could run everything inside a screen session with logging enabled.