Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Newbie cmd prompt problem

by nquiton (Novice)
on Sep 11, 2013 at 23:02 UTC ( [id://1053591]=perlquestion: print w/replies, xml ) Need Help??

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

I downloaded the latest version of strawberry 64 bit perl. I'm trying to practice but every time I try to run my program at the cmd prompt (I've used 2 differed types) the window disappears or I have to launch the program several times to get it to run. I'm using my teachers example so I know the code is good. Any suggestions. I've already uninstalled and re installed.

Replies are listed 'Best First'.
Re: Newbie cmd prompt problem
by boftx (Deacon) on Sep 11, 2013 at 23:14 UTC
    I'm using my teachers example so I know the code is good.

    If you think about the old saw that says "Those who can, do. Those who can't, teach." then you might want to re-evaluate your confidence level in that code. :)

      LOL. So true but it ran on the machine in class so I know the code was good.
        Are you sure it was a live demo and not a slideshow or video you were looking at?
Re: Newbie cmd prompt problem
by kcott (Archbishop) on Sep 11, 2013 at 23:43 UTC

    G'day nquiton,

    Welcome to the monastery.

    The answer to that will depend on how you're actually running your program. Possibilities include:

    > perl prog_name > prog_name > perl -e "... perl code ..."

    Also, does: (a) a new window appear, display some program output (possibly too quickly to read), then disappear; or (b) the window containing the initial command prompt disappear as soon as the program finishes?

    A possible workaround would be to add this line to your code after processing is completed but before the program exits:

    print "Hit enter when done: "; <>;

    I don't have an MSWin machine to demonstrate that on, but it should look something like this:

    $ perl -Mstrict -Mwarnings -E ' say "Your program runs and displays output here ..."; print "Hit enter when done: "; <>; ' Your program runs and displays output here ... Hit enter when done:

    When I hit Enter, it returns to the command prompt; in your case, I presume the window would now disappear. The point is, it gives you a chance to actually read the output (or, indeed, do whatever you want with it: paste into a text file, take a screenshot, etc.).

    There may be configuration options to alter the behaviour you're seeing: unfortunately, I can't help with that, but maybe another monk can.

    -- Ken

      Thanks for trying to help. I really appreciate it! re:(a) a new window appear, display some program output (possibly too quickly to read), then disappear; or (b) the window containing the initial command prompt disappear as soon as the program finishes? Both a and b. That is what is so frustrating. Well, that and my brain doesn't think in code. Yes, sometimes it disappears as soon as the program finishes and sometimes nothing happens when I hit enter, no error, it just has the path that I typed. Other times it runs and tells me what lines have errors. Re: your suggestion. I'm only 2 days into this class so I'm not sure what you mean. Here is my code and yes I know there are errors but won't be able to find them until I can run it. Can you add what you were telling me to do so I can try it? #Noel use strict; my $numone; my $numtwo; print "Please enter first number "; $numone=<STDIN>; print "Please enter second number "; $numtwo=<STDIN>; if ($numone < $numtwo); { print $numone; print $numtwo; } elsif { print $numtwo; print $numone; }
        "Thanks for trying to help. I really appreciate it! ..."

        While I'm happy to help, you need to help us as well. Posting all your text and code in a single block without any markup makes it very difficult to read. Put your text into paragraphs within <p>...</p> tags and your code within <code>...</code> tags. Details can be found in (or linked from) "Writeup Formatting Tips"; and "How do I change/delete my post?" explains how to fix what you've already done.

        After putting your code into some sort of readable layout, it would appear that what I'm talking about is pretty much the same thing that you're learning about:

        $ perl -Mstrict -Mwarnings -E ' my $numone; my $numtwo; print "Please enter first number "; $numone = <STDIN>; print "Please enter second number "; $numtwo = <STDIN>; #if ($numone < $numtwo); { <--- WRONG: no semicolon here if ($numone < $numtwo) { print $numone; print $numtwo; } # elsif { <--- WRONG: should be else else { print $numtwo; print $numone; } print "Hit enter when done: "; <>; ' Please enter first number 456 Please enter second number 123 123 456 Hit enter when done:

        So, in this context, "<>" is just a short-hand form of "<STDIN>": see "perlop: I/O Operators" for the full story. Also, I don't assign the user input to a variable because I don't want it and, in fact, don't really care what the user enters. This works the same:

        $ perl -Mstrict -Mwarnings -E ' ... previous code unchanged ... print "Hit enter when done: "; <STDIN>; ' Please enter first number 456 Please enter second number 123 123 456 Hit enter when done: I'm all done with this!

        -- Ken

        clearly I don't know how to insert my code. Yicks.
      I think I know what the problem is. Apparently when naming your files you should not use spaces. When I changed the file name and removed my spaces it ran. Now I know for sure I'll never be a programmer because I have LOTS of errors:-)
        "I think I know what the problem is. Apparently when naming your files you should not use spaces. When I changed the file name and removed my spaces it ran."

        I generally refrain from using spaces in filenames because spaces are already used to separate the program name from switches, input files, and so on: basically it's a real nuisance that's easily avoided. However, it's not actually illegal and you will encounter third-party filenames that do include spaces: when you do, just quote them (i.e. "some filename with spaces"). I'd tend to write that filename as some_filename_without_spaces, which is just as easy to read but has none of the associated problems.

        "Now I know for sure I'll never be a programmer because I have LOTS of errors:-)"

        You'll learn a lot more from making mistakes and fixing them, than if you wrote all your code perfectly. What's more, sorry to be the one to tell you this but, don't worry at all about mistakes after only 2 days of coding: you'll still be making mistakes with 20 years experience under your belt.

        Also, don't worry too much about the number of errors: a simple mistake like a missing semicolon at the end of a statement, or using the wrong quote to terminate a string, can result in dozens of errors. When I frst ran the code you posted, I got:

        syntax error at -e line 9, near ");" Unmatched right curly bracket at -e line 12, at end of line syntax error at -e line 12, near "}" syntax error at -e line 16, near "}" Execution of -e aborted due to compilation errors.

        But, as you can see from my previous post, there were only two small errors that were easily fixed.

        -- Ken

Re: Newbie cmd prompt problem
by Anonymous Monk on Sep 11, 2013 at 23:12 UTC
      The command box disappears. I'm running perl on a Windows 7 machine. I drag the file from its location into a cmd prompt window and one of two things happens. 1) It looks as though it may have run the program but the cmd window flashed on the screen and then disappears (no it's not in the task bar) OR 2) cmd window stays on my screen but I have to drag it into the window several times to get it to run. The whole thing seems very buggy. I don't know how else to describe it.

        Perl preaches there's more than one way to do it ... but that's completely the wrong way to do it.

        Go to "Start" -> "Run" and enter 'cmd' (without the quotes) and press the "OK" button

        Get familiar with Command Prompt (DOS) commands like 'cd' to change directory into the directory where your Perl script is. From there, type:

        C:\> perl myscript.pl

        where 'myscript.pl' is the name of your Perl script.

        Alternatively, add the line:

        system "pause";

        as the very last line in your Perl script. That's the "poor man's way" of keeping the Command Prompt open when you double-click a Perl script to run versus running it from the command line.

        1) It looks as though it may have run the program but the cmd window flashed on the screen and then disappears (no it's not in the task bar)

        what happens when you type "perl .....file.pl"?

        2) cmd window stays on my screen but I have to drag it into the window several times to get it to run.

        uh oh, "cmd window" and "the window"? that is weird, dragging windows to run a program is not normal operation -- what are you talking about?

Re: Newbie cmd prompt problem
by 5mi11er (Deacon) on Sep 11, 2013 at 23:31 UTC
    Well, in my opinion, the way to "run perl scripts" from the command line, is not to drag an icon to said window.

    Figure out where the script is, cd into the appropriate directory, and type "perl <script-name>", or you can specify the whole path in place of <script-name> if you'd rather not cd into the appropriate directory.

    -Scott

      It doesn't seem to matter which way I do it. The result is the same. The Windows version of Perl lets to drag from one window to anther. It's kind of a neat feature. Yep.... I'm a Windows girl:-) I will never grow up to be a programmer:-)
Re: Newbie cmd prompt problem
by hdb (Monsignor) on Sep 12, 2013 at 05:29 UTC
Re: Newbie cmd prompt problem
by soonix (Canon) on Sep 12, 2013 at 08:10 UTC
    just two cents more:
    • if you type the command in, you have more control over what's executed (and how) than when you drag a file icon into the command line window.
    • if we can't dissuade you from using a GUI for executing your programs anyway :-) - then you should take it farther and use an IDE (integrated development environment) like PADRE

Log In?
Username:
Password:

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

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

    No recent polls found