Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Print hello world not working

by ferbio (Initiate)
on Jul 15, 2020 at 03:57 UTC ( [id://11119341]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, The simplest script #!/usr/bin/perl use strict; use warnings; print "Hello, World...\n"; Works fine in my computer (Printing in the CMD screen "Hello World") but in other computers did not print anything on the screen after installing the same perl version. What is happening? Please help.

Replies are listed 'Best First'.
Re: Print hello world not working
by haukex (Archbishop) on Jul 15, 2020 at 06:54 UTC
    Dear Monks, The simplest script #!/usr/bin/perl use strict; use warnings; print "Hello, World...\n"; Works fine in my computer (Printing in the CMD screen "Hello World") but in other computers did not print anything on the screen after installing the same perl version. What is happening?

    That's pretty unusual, and I think you'll have to explain exactly how you're calling this script on both computers so we can help you better. Since you say "CMD" I guess this is Windows? Are you using ActivePerl or Strawberry Perl, and what version? When you open the command prompt on both computers and type perl -v, it should output the same thing; that's assuming your PATH environment variable is set up correctly; if for example you're using the portable edition of Strawberry Perl, you'll have to navigate to its folder first (Update: or use its batch file to start the shell). It should also do the same when you type perl scriptname.pl for your script filename. Or, are you double-clicking the scripts to run them? Note that this sometimes requires additional steps to get working. (By the way, please use <code> tags to format code and data.)

Re: Print hello world not working
by kcott (Archbishop) on Jul 15, 2020 at 07:24 UTC

    G'day ferbio,

    Welcome to the Monastery.

    The most likely cause is your shebang line: '#!/usr/bin/perl'. On one computer, perl is installed in /usr/bin/; on another computer, it's installed elsewhere.

    You need to tell us which OSes you're using and show all output. It seems odd that you get no output at all: perhaps no "Hello, World...", but were there any other messages. Here's what I get when I try to run perl from both an existing and non-existing location:

    $ /usr/bin/perl -e 'print "Hello, world!\n"' Hello, world! $ /usr/not_bin/perl -e 'print "Hello, world!\n"' -bash: /usr/not_bin/perl: No such file or directory

    Putting your posted code — but using an invalid path to perl in the shebang line — into a script and running:

    $ cat crap.pl #!/usr/not_bin/perl use strict; use warnings; print "Hello, World...\n"; $ ./crap.pl -bash: ./crap.pl: /usr/not_bin/perl: bad interpreter: No such file or +directory

    Take a look at perlintro for a gentle introduction of this concept; perlrun has a more detailed discussion.

    [Note: Please put all code, data and program output within <code>...</code> tags. Doing so will mean that HTML rendering (e.g. converting a sequence of whitespace characters into a single space) will not affect what you post. It also allows us to access a verbatim copy. See "How do I post a question effectively?" and "Writeup Formatting Tips".]

    — Ken

Re: Print hello world not working
by BillKSmith (Monsignor) on Jul 16, 2020 at 17:11 UTC
    Most Perl books include this program along with directions to store it in a file (I am assuming 'hello.pl') and execute that file. This is intended to verify that perl is installed correctly and that you understand how to run it. You indicate that you have passed this test on your windows computer, but are having trouble on a second computer. If the second computer is also a windows computer, there probably is a difference in the installation. We can help if you tell us how you tried to run the file. Did you click on the file icon, type hello.pl, or type perl hello.pl? Please, also tell us what did happen. Did you get an error message? What was it? Did the cmd prompt return? Did a strange window appear briefly? I also recommend that you try to check the perl version. Type perl -v. Again if this does not work as expected, tell us what did happen. While you are waiting for our next reply, you can experiment with the other ways of running your file.

    If the second computer is not running windows, we also need to know anything you can tell us about its operating system and perl. Are you using the perl that was shipped with the OS?

    Bill
Re: Print hello world not working
by perlfan (Vicar) on Jul 16, 2020 at 02:06 UTC
    That's not how you one-liner, playa. When I drop the line #!/usr/bin/perl use strict; use warnings; print "Hello, World...\n"; directly in the commandline I get nothing also because # is a comment and there's no shebang evaluation in the interactive prompt. Throw that mess in a file:
    #!/usr/bin/perl use strict; use warnings; print "Hello, World...\n";
    Set it executable, chmod 700 hello.pl, then run it with ./hello.pl.

    If that's not it I have no idea what you're doing because any other interpretation of your post would throw an error from something. Putting it in a file will at least give you a feedback if that's not where your perl is located.

      Further reading

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Print hello world not working
by Anonymous Monk on Jul 15, 2020 at 08:00 UTC
    What OS are computers using? I end shebang with space dash dash
      I don't know why you would do that? That appears unnecessary to me. I guess that is like a null option. On Windows, the Perl path in the shebang line is ignored but options after that path like perhaps -w are parsed and used. I don't think a double dash followed by nothing means anything on any OS.
        -- is how you'd generally denote options for something else other than the first executable that's being called.

        For example for perl, perlrun says that: A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments..

        What this means roughly, IIRC, is that you use it to pass of options after -- to the perl script you're calling with perl.

        For example,

        $ perl -MSome::Module ./myperlscript.pl -- --my-opt1 foo --my-opt2 bar
        What this also means, I think, is that it's useless when part of the shebang line. Or maybe not, idk. Seems odd being in the shebang, though.

        Update - after thinking about it, I can see how this might be considered a defensive programming tactic. E.g., if somehow some script kiddie was seeking the add options to your shebang, having -- would make anything after moot. Similarly, adding __END__ at the end of your perl scripts would also neutralize someone blindly appending to your files.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found