Question about Perl

Hello,

I’m new to Perl programming and I actually posted this question in another forum, but later found out that forum barely had any visitors and thus my question was never really answered. So I’m posting this here hoping someone can help me out :slight_smile:

I’m learning Perl from few handy books and the problem goes like this:

First, using Textpad, I typed the following statement:

[B]#!/usr/bin/perl

use warnings;

print "Hello, World!
";
[/B]
After that, I saved it as Helloworld.pl

I did it this way because this was an exercise from the book I’m learning from (Beginning Perl, 3rd Edition) - this is how the book instructed me to do so. Second, my book then asks me to pop open cmd and create two directories through the cmd (I’m using Windows Vista; 64).

One directory as c:> mkdir begperl
second directory as c:> cd begperl

I did it their way, and really no directories were created. I only saw two files (0 bytes worth) on my c folder. So then I just created those two folders manually on the c drive.

My book then asks me to go to cmd again, and type this statement in: $ perl helloworld.pl (it’s the file I saved before) - the book tells me that it should produce the phrase “Hello World!” however the command prompt tells me that $ is an unknown character.

Something was fishy to me, because when I was programming with Python, Python came with it’s own Python cmd, and I expected Perl to have the same, but it didn’t. Then I looked in the internet, and I went to Perl.org and I downloaded Strawberry Perl Interpreter from Perl.org. This one DOES come with it’s own cmd called “Perl (command line).”

I fire that up, and I type in print "Hello World!
";
…and it tells me “Unable to initialize device PRN.”

I looked on Google, and someone else was having that problem as well apparently, and he posed the same question on a tech website (link listed below). One reply suggested it’s called the “missing PRN bug” or something and advised him to type instead perl -e “print ‘Hello World!’”

THAT worked for me (apparently it worked for the guy who asked too). I later learned that -e is something you use for single line statements (as stated in my book).

So my question to you is, what do I do? Every time I write a program, do I have to attach the -e for each statement? If so, I don’t think the underlying problem would really be solved. I just dont know what this “unable to initialize device PRN” message means. Does it mean perl did not install properly and I have to reboot my PC? Could you set me straight as to what’s really going on?

Thank you very much for any help you send my way. I highly appreciate it.

I’m new to Perl programming and I actually posted this question in another forum, but later found out that forum barely had any visitors and thus my question was never really answered. So I’m posting this here hoping someone can help me out

Unfortunately SitePoint hasn’t mentioned Perl in like 10 years. I don’t know why they have this forum : (

Is Beginning Perl the Apress book? Or Pakt Press? Black and orange/yellow? I remember seeing one like that in the bookstore but I didn’t recognise the author name. Or is it the Simon Cozens book? That one is getting a bit old and I don’t know if he’s updated anything. Hopefully you are starting your Perl career with at least 5.8.x if not higher.

Anyway I would say so long as you know how to make directories in Windows (I have no clue, it’s all so much easier in Unix/Linux), do it that way. I don’t know DOS except from some barely-remembered time back in my childhood, and I seem to vaguely remember that it was “dir” instead of “mkdir” there… ?

did it this way because this was an exercise from the book I’m learning from (Beginning Perl, 3rd Edition) - this is how the book instructed me to do so. Second, my book then asks me to pop open cmd and create two directories through the cmd (I’m using Windows Vista; 64).

One directory as c:> mkdir begperl
second directory as c:> cd begperl

If that were bash, you’re not making a second directory: you’re first creating a directory (make directory called “begperl”) and then you go to that directory with cd begperl (change directory (to) “begperl”). You should have a single file which initially contains nothing (though I would think it does have some (small) filesize… on my system empty dirs are 4 kb).

My book then asks me to go to cmd again, and type this statement in: $ perl helloworld.pl (it’s the file I saved before) - the book tells me that it should produce the phrase “Hello World!” however the command prompt tells me that $ is an unknown character.

$ typically isn’t something you type in, but in code examples it’s there to show your “command prompt”, and $ is typical for non-root users in many Linux systems (it may be something else if you are root like #). But I have no clue what the"command prompt" is in Windows. I don’t believe you were meant to type the $.

Was this book written assuming a unix environment or bash?

On my machine (Linux), after I save the perl file, I can manually make it an executable, and then I don’t have to use the command “perl” to run them: I can just
$ helloworld.pl
and it runs (again I’m not typing the $ there). If I don’t, I need to use the “perl” command and also the path if I’m not in my Perl folder.

If I’m at /home/mallory and perl scripts are at /home/mallory/perlstuff
then I would need to
$ perl perlstuff/perlscript.pl
to run it. But that’s Linux.

So you are running Strawberry Perl? While I hear that’s the more awesome/working way to use Perl on Windows, I can’t imagine Perl and Windows being a nice match. Seems like it would be harder to learn, especially if the book is writing commands for bash instead of whatever’s on Windows.

Also if you are in Windows are you supposed to start with #!/usr/bin/perl? My perl is definitely there but Windows doesn’t use that kind of file system. Or is Windows just ignoring that?

“Unable to initialize device PRN” <– something to do with Windows thinking this Perl file is a “print” file. Or anyway, it’s an error Windows gives people when they are doing printing and trying to “print” stuff.

The problem with -e is you have to escape stuff like quotes everywhere as opposed to inside a full perl file. You definitely don’t want to have to do that. You should be able to call a saved file as an executable.

Hm… yeah the fact that you can run this immediately with the -e argument but not as an executable is probably because of your shebang line… that is supposed to tell your OS where “perl” (the interpretor itself) is. Yours can’t be there unless you’re running Cygwin (and I’m not totally sure how that works with Cygwin anyway).

Besides the irc channel at Strawberry Perl (irc://irc.perl.org/win32), your best place for answers will probably be Perlmonks. There are a good number of people there using Strawberry Perl.

*edit I re-read and see you’re on 64-bit Windows, so I dunno where to get 64-bit SP help other than PerlMonks

Stomme poes,

Thank you very much for your response Stomme poes:) Do you think I’ll have a better time learning perl if I install ubuntu?

I have ubuntu and I just find anything with bash easier to learn Perl on because I’m not also learning some weird command line stuff at the same time.

Be aware though that Ubuntu uses perl to Do Stuff, so I’ve heard of some people installing another perl so they aren’t playing with their system perl. But I’ve been using system perl.

Seems like you have masses of questions. It all gets a bit confusing.

Firstly there is no reason not to use perl in a windows environment.

Like stommepos that $ is not need it is clearly being used to indicate a command prompt.

If you type

perl filename.pl

then windows will use the PATH environment variable to try and locate perl.exe so the perl bin directory needs to be on the path.

I believe if Windows knows about the association between the .pl extension and perl.exe then you can just type

filename.pl

but I must admit I don’t use this.

You can enter a type of perl console by using

perl -d

at the command prompt and then type CTRL/Z.

Your really entering the debugger with input coming from the console (the CTRL/Z terminates input from the console and gives you the debug prompt.

Firstly there is no reason not to use perl in a windows environment.

But, would you want to learn Perl in a Windows environment when your books seem written for a unix-y command line?

then windows will use the PATH environment variable to try and locate perl.exe so the perl bin directory needs to be on the path.

Question, though, is Windows getting confused by the shebang line?

But, would you want to learn Perl in a Windows environment when your books seem written for a unix-y command line?

I am not familiar with the book but it is clearly about Perl and not the operating system. If you have Unix use Unix if you have Windows use Windows. The command prompt can be a $ in Windows command line if it helps.

is Windows getting confused by the shebang line?

Windows takes no notice of the shebang line if you are just executing from the command line. The only thing, as far as I am aware, that does is Apache - provide you tell it to do so.