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

Hi everyone, I'll admit, I'm brand spankin' new to Perl and programming in general (I have some experience with HTML but that was years ago). I picked up Perl as a hobby and I'm very interested in becoming decent at it. I was advised to come here by my programming friends - they have many great things to say about this community, and so, here I am :) Although I am learning from couple of books (Apress; Beginning Perl, 3rd Ed. and Perl for Dummies 5th Ed.) I am having trouble with my first assignment from the Beginning Perl book fro Apress - the assignment is to create a basic Hello World.pl file and execute it. So here is the deal, I'm on Ubuntu, and I follow the instructions as detailed below from my book: 1)The book tells me to crack open a text editor and write this:
#!/usr/bin/perl use warnings; print "Hello World!\n";
2) Next, the book instructs me to go my command terminal and create two directories. The way it instruct me, is that it tells me to type the following in the command terminal:
$ mkdir begperl $ cd begperl
3) Next the books instructs me to save the file from step 1 as helloworld.pl and save it in the directory that was made. What confusing to me is, I do not know exactly which directory it's referring me to - I should also point out I'm somewhat new to Ubuntu as well (been using it for 2 weeks now) so perhaps I just dont know how to save it properly? 4) The book asks me to go to my command terminal and type the following: $ perl helloworld.pl and the book shows that it's supposed to show up on the screen as Hello World! Thats really where I run into my problem. You see, every time I try to run it, it tells me it cannot be ran, because no such files or directory exist. But, I followed every step to the letter, I dont know why it's saying it doest find anything. In fact, I navigated to the actual 2 directories I was instructed to create, and I found the .pl file there. So I'm actually stuck, and I'm not sure exactly what I'm doing wrong. Please help out dear monks :) -Samurai Monkey

Replies are listed 'Best First'.
Re: I come in peace for I seek your wisdom dear monks :)
by mr_mischief (Monsignor) on Dec 08, 2010 at 03:23 UTC

    This seems to be more a question about your shell than about Perl. Since using one is important to you learning the other, though, I'll try to help.

    When you say you navigate to the directory begperl and see the file helloworld.pl, do you mean that's where you are "in" when you run the command perl helloworld.pl and get the error? (You can tell what directory you are in by running the pwd command.) If you are in your home directory and the program is in the begperl subdirectory under your home directory, you'd need to run the command as something like perl begperl/helloworld.pl instead.

    Does this help?

      Thank you very much for your help - yes it def, solved my problem. Aren't the answers to seemingly complex problems usually an easy one? lol I didn't realise that there was a default folder on the Ubuntu terminal was set on. Any clue as to how you can set it to a different default folder? Again, thank you very much for your help :)
        There are ways to do that. The default default is a pretty good one tested by millions of other users for years. If you give it a couple of weeks of use you'll probably agree. Plus, if you change it now, it'll be a nonstandard hurdle to getting advice from people who wouldn't expect that sort of change.
Re: I come in peace for I seek your wisdom dear monks :)
by 7stud (Deacon) on Dec 08, 2010 at 03:31 UTC

    When you open a command terminal, you start in some default directory. The starting directory can be set, but you'll have to figure out how to do that on some Ubuntu/Unix forum (or search google). You might have to create a file(if it doesn't already exist) with a specific name like .profile, add some lines to it, and put it in a specific directory. It also depends on what "shell" you are using. For all intents and purposes, you can equate "the shell" with the command window. The ls -al command will reveal all the files in a directory--including hidden files that start with a dot.

    The following:

    $ mkdir begperl $ cd begperl
    commands your operating system to mkdir(make a directory) called begperl inside the current directory. So if your default directory (the one the command window opens in) is:

    /users/mike/my_programs

    the mkdir command would have created the directory /users/mike/my_programs/begperl.

    Then the cd (change directory) command says to search the current directory for the directory called begperl and switch to that directory if found. You can always use an absolute path to switch to any directory without knowing what the current directory is:

    $cd /users/mike/my_programs/begperl

    I suggest you set your prompt($) to indicate what directory you are in. You will have to figure out how to do that on an Ubuntu/Unix forum as well. After you set the prompt to display the directory, the prompt will look like this:

    /users/mike/my_programs$ cd begperl /users/mike/my_programs/begperl$

    Finally, when you write:

    $perl helloworld.pl

    that commands your operating system to look for a program called 'perl' and have perl excute a file in the current directory called helloworld.pl.

      Great answer, and it def. helps - thank you too mate :)
Re: I come in peace for I seek your wisdom dear monks :)
by Marshall (Canon) on Dec 08, 2010 at 06:16 UTC
    $ perl helloworld.pl and the book shows that it's supposed to show up on the screen as Hello World! Thats really where I run into my problem. You see, every time I try to run it, it tells me it cannot be ran, because no such files or directory exist.

    On a Unix system, in order to execute a Perl script, it has to have what is called "execute" permission. Each file has "read, write, execute" permission levels for each of "user, group, other". "man" (short for manual) is a command that you will become very familiar with! Type "man chmod" at the command prompt.

    chmod 755 helloworld.pl is a common thing to do and this results in (-rwxr-xr-x), meaning that you as a user have rwx (read,write,execute permissions) to this file, the group that you belong to has r-x (read and execute, but not write)and all others have have the same permission (r-x) as your group. The first dash - at the beginning has special meaning that is not important now.

    At the command prompt, type "ls -al". This should show the files and directories and this rwxrwxrwx notation, each rwx "set" is 3 bits in octal and is for user,group, other. 755 in octal is 111 101 101, the "write" bit is turned off for the group and for others, but is turned on for "user", meaning you. This scenario is common.

    Now that you have the helloworld.pl program marked as exectuable (chmod 755 helloworld.pl). A Unix system looks into the file in order to decide what to do. That is why the first line in that file should be:
    #!/usr/bin/perl
    This is called the "shebang" line. The # means that this is a comment..sort of..except when its not..and this is one of those cases. This looks like a comment, but it is way more than a comment. As the first line, this means: run the Perl interpreter on the rest of this file.

    Once you have given execute permission to the file, you can just type "helloword.pl" and it will run. Typing "perl helloworld.pl" explicitly starts the Perl interpreter and sends it the helloworld.pl file. Oh, also be aware that Unix is case sensitive. Helloworld.pl is different than helloworld.pl

    Get helloworld.pl working in your default login directory before trying to move it or get it to run in a sub-directory.

      On a Unix system, in order to execute a Perl script, it has to have what is called "execute" permission.
      No.

      It may, or may not, be needed. It's certainly not needed in the way the book told the OP how to execute the script: perl helloworld.pl. Not also that if you try to execute a program for which you need execute permission, the shell will not complain about a missing file or directory name, but tell it will either tell you "command not found" (if a $PATH search failed to find it - this only looks for programs with execute permission), or "permission denied" (if you try execute a program by using an absolute or relative path).

        What I said is true and I think that I covered your case with:
        Once you have given execute permission to the file, you can just type "helloword.pl" and it will run. Typing "perl helloworld.pl" explicitly starts the Perl interpreter and sends it the helloworld.pl file.
        In the above, yes, "perl helloworld.pl", helloworld.pl does not need execute permission because helloworld.pl is an input file to the command "perl". Normally one would give the .pl file execute permissions and omit the need to type "perl" explictly. Note: On Unix the "file extension" of .pl doesn't matter. On Windows, there are a couple of non-obvious steps required to get a .pl file to execute in all situations without having to type "perl" first. But that is not the question here.

        If the OP cannot execute the .pl file, either the OP is in the wrong directory or has the wrong permissions or has the wrong shebang line or the path does not have the perl command.

        My advice of:

        Get helloworld.pl working in your default login directory before trying to move it or get it to run in a sub-directory.
        still appears solid to me.
      Marshall, while the advice is appreciated, the OP is confused enough already. There is no need to add to that by mentioning different ways to run Perl scripts, especially when it brings in several confusing topics like permissions and the shebang.

      Elda Taluta; Sarks Sark; Ark Arks

        Yes, there is definitely an "information quanta" that can be conveyed in a single post and I did exceed that. It happens sometimes. Your point is well taken.