Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to get screenshort using PERL?

by murugaperumal (Sexton)
on Apr 28, 2010 at 06:01 UTC ( [id://837233]=perlquestion: print w/replies, xml ) Need Help??

murugaperumal has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to get screenshort using PERL?
by arc_of_descent (Hermit) on Apr 28, 2010 at 06:17 UTC
Re: How to get screenshort using PERL?
by Ratazong (Monsignor) on Apr 28, 2010 at 06:19 UTC
Re: How to get screenshort using PERL?
by Khen1950fx (Canon) on Apr 28, 2010 at 08:04 UTC
    If you have ImageMagick, then you could try this:
    #!/usr/local/bin/perl use strict; use warnings; my $file = '/root/Desktop/'; sleep 5; $file = system("import -window root MyScreenshot.png"); print $file, "\n";
    Update: For a more condensed method:
    use strict; use warnings; system("import -window root MyScreenshot.png");
      #!/usr/local/bin/perl use strict; use warnings; my $file = '/root/Desktop/'; sleep 5; $file = system("import -window root MyScreenshot.png"); print $file, "\n";

      Ouch! That really hurts my eyes!

      • Why do you sleep before taking a screenshot?
      • Why do you assign a useless constant to $file?
      • Are you sure system() returns a file or a filename? Hint: It doesn't!
      • So why do you assign the return value to a variable named $file?
      • Why do you mess with the notoriously problematic default shell instead of using the multiple-argument form of system?
      • Why do you print the return value of system to STDOUT?

      At least, you use strict and warnings.

      use strict; use warnings; system("import -window root MyScreenshot.png");

      Better, but still problematic: You still invoke the default shell, begging for trouble. All error checks are missing. Yes, system() may fail. For example, because there is no import utility -- e.g. when ImageMagick is not installed. Or because there is some other tool named import, with completely different parameters a and a completely different purpose.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        You still invoke the default shell

        Actually, this particular example wouldn't invoke a shell, because there are no shell metacharacters in the command.

        $ strace -f -eexecve perl -e 'system("import -window root MyScreenshot +.png")' execve("/usr/local/bin/perl", ["perl", "-e", "system(\"import -window +root MySc"...], [/* 31 vars */]) = 0 Process 20945 attached (waiting for parent) Process 20945 resumed (parent 20944 ready) [pid 20945] execve("/home/almut/bin/import", ["import", "-window", "ro +ot", "MyScreenshot.png"], [/* 31 vars */]) = -1 ENOENT (No such file +or directory) [pid 20945] execve("/usr/local/sbin/import", ["import", "-window", "ro +ot", "MyScreenshot.png"], [/* 31 vars */]) = -1 ENOENT (No such file +or directory) [pid 20945] execve("/usr/local/bin/import", ["import", "-window", "roo +t", "MyScreenshot.png"], [/* 31 vars */]) = -1 ENOENT (No such file o +r directory) [pid 20945] execve("/usr/sbin/import", ["import", "-window", "root", " +MyScreenshot.png"], [/* 31 vars */]) = -1 ENOENT (No such file or dir +ectory) [pid 20945] execve("/usr/bin/import", ["import", "-window", "root", "M +yScreenshot.png"], [/* 31 vars */]) = 0

        As you can see, the import is being run directly.

        (Add a ';' (metacharacter) at the end of the command, and it will run the command via a shell...)

Re: How to get screenshort using PERL?
by roboticus (Chancellor) on Apr 28, 2010 at 14:40 UTC

    murugaperumal

    You really need to define the question fully. Perl doesn't have to run on "a screen", so the question is incredibly ambiguous. There are many types of screens, so I couldn't help you even if I wanted to. (Are you running on an X11 terminal? How about Windows? What type of window? Perhaps a serial terminal? Not all of them have a method to save a screen. Perhaps you're running on a teletype? Just rip off the paper and tape it into your document. Maybe it's a web server program? Just provide the appropriate JavaScript to capture the screen and send it back.

    If you're going to be a successful programmer, you need to learn to recognize the assumptions you're making. As well as understand the context in which your program runs, and the context in which your client(s) think so you can figure out what the assumptions are. If you can't do that, you need to spend some time practicing on it to be successful. To do so, you might go through all the nouns in your question, and think of their definition. Then think of any possible alternate definitions. When you find alternate definitions, it normally means that you haven't qualified your statement enough to remove the assumption(s). When you're talking with a client, keep in mind that they're normally not going to be a programmer. Each profession has its own terminology/jargon, and you have to verify that what the customer is saying means what you think it means. Frequently a client will use computer jargon incorrectly, and if you don't catch it, you won't be able to fulfill the requirements and waste your time, which will cost you money. Don't be afraid to rephrase statements with different jargon or different examples to verify that the statement means the same thing to both of you, after all, your client wants the correct result as much as you do, and will understand that a little bit of time spent clarifying things will pay off handsomely in the course of the project.

    </pedantic_mode>

    ...roboticus

Re: How to get screenshort using PERL?
by Anonymous Monk on Apr 28, 2010 at 06:18 UTC
Re: How to get screenshort using PERL?
by strredwolf (Chaplain) on Apr 29, 2010 at 03:16 UTC

    Perl cannot short out a screen unless there is a electrical problem in your monitor. :)

    Now, for a X11 screenshot using Perl and GTK2, use this code: Grabbing that mouse cursor in X11

    Information doesn't want to be free. It wants to be feline.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://837233]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found