Home: Perl Programming Help: Beginner:
Question on book "Beginning Perl" and running examples



OB53
Novice

Oct 17, 2013, 9:15 PM


Views: 16616
Question on book "Beginning Perl" and running examples

I am running Perl version 5.16.3 an I have been following along in the book until chapter 6. I can't get the examples to work. To be specific here is the code:

1 #!/usr/bin/perl
2 # nl2.plx
3 use warnings;
4 use strict;
5
6 my $lineno = 1;
7
8 while (<>) {
9 if ($current ne $ARGV) {
10 $current = $ARGV;
11 print "\n\t\tFile: $ARGV\n\n";
12 $lineno=l;
13 }
14
15 print $lineno++;
16 print ": $_";
17 }_

I downloaded the book and programs from http://www.wrox.eom/books/1861003145.htm
Is it possible that the version of Perl that I am running has the book outdated?

The errors I get are:

c:\>nl2.plx
Global symbol "$current" requires explicit package name at C:\nl2.plx
line 9.
Global symbol "$current" requires explicit package name at C:\nl2.plx
line 10.
Execution of C:\nl2.plx aborted due to compilation errors.
c:\>


(This post was edited by OB53 on Oct 17, 2013, 9:25 PM)


hwnd
User

Oct 18, 2013, 1:53 AM


Views: 16607
Re: [OB53] Question on book "Beginning Perl" and running examples

You need to add the following before your while loop since you're using strict.


Code
my $current;



OB53
Novice

Oct 18, 2013, 10:03 AM


Views: 16595
Re: [hwnd] Question on book "Beginning Perl" and running examples

Thanks, but that doesn't work.
I tried both adding 'my $current;' to the code as well as deleting 'use strict;' from the code and neither works.


Zhris
Enthusiast

Oct 18, 2013, 10:45 AM


Views: 16590
Re: [OB53] Question on book "Beginning Perl" and running examples

Hi,

I see three problems with the snippet:

1) $current requires an explicit package name.
2) $lineno is assigned the letter "l" when I think it meant "1".
3) There is an under slash at the end of the snippet.

This version works for me:


Code
#!/usr/bin/perl  
# nl2.plx
use warnings;
use strict;

my $current = '';
my $lineno = 1;

while (<>) {
if ($current ne $ARGV) {
$current = $ARGV;
print "\n\t\tFile: $ARGV\n\n";
$lineno=1;
}
print $lineno++;
print ": $_";
}


Chris


OB53
Novice

Oct 18, 2013, 11:17 AM


Views: 16582
Re: [Zhris] Question on book "Beginning Perl" and running examples

I copied your code to my file and it still does not work;
From the command line I type: nl2.plx nlexample.txt. I checked and the txt file exists in the same directory as the Perl file. I am running Perl version 5.16.3. Could this be the difference


FishMonger
Veteran / Moderator

Oct 18, 2013, 11:33 AM


Views: 16577
Re: [OB53] Question on book "Beginning Perl" and running examples

Saying "it still does not work" is a very poor problem description.

In what way is it not working?

How does the output differ from what you expect?

Are you receiving any errors/warnings?


FishMonger
Veteran / Moderator

Oct 18, 2013, 11:48 AM


Views: 16572
Re: [OB53] Question on book "Beginning Perl" and running examples

I don't have a copy of "Beginning Perl", but if that's one of the example scripts it uses, then I'm not very impressed. That is a very poor way of writing that script.

Here's a much better way to write that script.

Code
#!/usr/bin/perl 

use warnings;
use strict;

my $file = shift;
$file or die "missing required argument";

unless(-e $file and -f $file) {
die "File $file does not exist or is not a plain text file";
}

open my $fh, '<', $file or die "failed to open '$file' $!";

print "\n\t\tFile: $file\n\n";
while (my $line = <$fh>) {
print "$.: $line";
}



(This post was edited by FishMonger on Oct 18, 2013, 11:49 AM)


OB53
Novice

Oct 18, 2013, 12:17 PM


Views: 16565
Re: [FishMonger] Question on book "Beginning Perl" and running examples

I tried this code and received the following error:
'missing required argument at C:>\nl2.plx line 7'

FYI I am typing the command 'nl2.plx nlexample.txt'
and the txt file does existg


OB53
Novice

Oct 18, 2013, 12:19 PM


Views: 16561
Re: [FishMonger] Question on book "Beginning Perl" and running examples

I get no errors nor anything else. not even a flashing coursor


FishMonger
Veteran / Moderator

Oct 18, 2013, 12:21 PM


Views: 16559
Re: [OB53] Question on book "Beginning Perl" and running examples

Sounds like you're running this on Windows and the .PL file type extension config is not configured correctly.


OB53
Novice

Oct 18, 2013, 12:28 PM


Views: 16555
Re: [FishMonger] Question on book "Beginning Perl" and running examples

nope, Running in Perl 5.16.3 (installed on a windows machine)
all examples worked until chapter 5.


FishMonger
Veteran / Moderator

Oct 18, 2013, 12:31 PM


Views: 16548
Re: [OB53] Question on book "Beginning Perl" and running examples

Run the command line test and post the output.
c:\test>perl -MData::Dumper -e "print Dumper \@ARGV" 1 2 3 4 5 6 7 8 9 10

Then run the same test as a script.

Code
#!/usr/bin/perl 

use warnings;
use strict;
use Data::Dumper;

print Dumper \@ARGV;


Here's what you should expect.

Code
c:\test>perl -MData::Dumper -e "print Dumper \@ARGV" 1 2 3 4 5 6 7 8 9 10 
$VAR1 = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10'
];

c:\test>arg_test.pl 1 2 3 4 5 6 7 8 9 10
$VAR1 = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10'
];



(This post was edited by FishMonger on Oct 18, 2013, 12:35 PM)


OB53
Novice

Oct 18, 2013, 12:48 PM


Views: 16542
Re: [FishMonger] Question on book "Beginning Perl" and running examples

All I get is:
$VAR1 = [];


OB53
Novice

Oct 18, 2013, 12:54 PM


Views: 16537
Re: [OB53] Question on book "Beginning Perl" and running examples

and if I put the code in the Perl Directory, I get the following when ran:
c:\Perl64>dumper.plx
Global symbol "$VAR1" requires explicit package name at C:\Perl64\dumper.plx line 11.
Execution of C:\Perl64\dumper.plx aborted due to compilation errors.
c:\Perl64>


FishMonger
Veteran / Moderator

Oct 18, 2013, 1:00 PM


Views: 16536
Re: [OB53] Question on book "Beginning Perl" and running examples

As I thought, your file type association is misconfigured.

The registry value for the "command" setting should be:

Quote
"C:\strawberry\perl\bin\perl.exe" "%1" %*


The key part is "%1" % which enables the reading of the args.

Here's the entire exported registry key.

Quote
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Perl_program_file]
@="Perl program file"

[HKEY_CLASSES_ROOT\Perl_program_file\shell]

[HKEY_CLASSES_ROOT\Perl_program_file\shell\Execute Perl Program]

[HKEY_CLASSES_ROOT\Perl_program_file\shell\Execute Perl Program\command]
@="\"C:\\strawberry\\perl\\bin\\perl.exe\" \"%1\" %*"


Adjust the path as needed then save the file. Double-click on it to execute it (which adds the key to your registry).
Attachments: perl_file_type.reg (0.69 KB)


OB53
Novice

Oct 18, 2013, 1:20 PM


Views: 16530
Re: [FishMonger] Question on book "Beginning Perl" and running examples

upon opening regedit I opened [HKEY_CLASSES_ROOT\Perl_program_file]

I saw nothing like @="Perl program file"

All I see is:

PerfFile
Defaultlcon
shell
open
command
Perl
Defaultlcon
shell
Open
command
PerlScript
CLSID
OLEScript
PerlScriptFile
Defaultlcon
ScriptEngine
Shell
Open
Open2
ShellEx
PropertySheetHandlers

I cant get the tabs to show up here


FishMonger
Veteran / Moderator

Oct 18, 2013, 1:38 PM


Views: 16521
Re: [OB53] Question on book "Beginning Perl" and running examples


Quote
I cant get the tabs to show up here

You need to use the "Code" tags around your code to retain the indentation. That's the "Code" button at the bottom-left of the edit window.

Check each of the "command" keys and find the one that has the path to perl.exe in the "Data" column.

Edit that value and add "%1" %* at the end.


OB53
Novice

Oct 18, 2013, 2:03 PM


Views: 16515
Re: [FishMonger] Question on book "Beginning Perl" and running examples

I have done a few things with no change in results.
I got my laptop which I installed Perl two months ago. ran the same files with the same errors.
I saw your attachment and downloaded it and then opened it and it said changes were made. Ran code again, no change in results.
as far as your suggestion {"Code" tags around your code to retain the indentation. That's the "Code" button at the bottom-left of the edit window}, I have no idea what that refers to.
Attachments: registry.jpg (201 KB)


OB53
Novice

Oct 18, 2013, 2:11 PM


Views: 16511
Re: [OB53] Question on book "Beginning Perl" and running examples

Actually, everything you are trying to help me with is nice, but it doesn't solve my problem with the book "Beginning Perl". I am only up to chapter 6 and so far none of the code has worked. What am I to do with the next 8 chapters. I am not saying I don't appreciate your help, in fact I do. But I feel bad that I keep coming back again and again.


FishMonger
Veteran / Moderator

Oct 18, 2013, 2:11 PM


Views: 16511
Re: [OB53] Question on book "Beginning Perl" and running examples


Quote
as far as your suggestion {"Code" tags around your code to retain the indentation. That's the "Code" button at the bottom-left of the edit window}, I have no idea what that refers to.


When you're typing in your response, look at the buttons along the bottom edge of that input text box.


OB53
Novice

Oct 18, 2013, 2:22 PM


Views: 16505
Re: [FishMonger] Question on book "Beginning Perl" and running examples

OK, I see it in the registry now after running your file:
Here is what it says now:
"C:\strawberry\perl\bin\perl.exe" "%1" %*
And you want me to change it to:
@="\"C:\\strawberry\\perl\\bin\\perl.exe\" \"%1\" %*"
I just want to be sure. Playing with registry is not my forte.


FishMonger
Veteran / Moderator

Oct 18, 2013, 2:33 PM


Views: 16499
Re: [OB53] Question on book "Beginning Perl" and running examples

You'll need to adjust the path so that it matches your system.

The @= syntax is only used in the .reg file. If you're manually editing the registry via regedit.exe, then you don't use the @=.

What version of Windows are you using? Depending on the Win version and regedit.exe version and perl distro (ActiveState or strawberry perl), the exact location and registry keys will change.


FishMonger
Veteran / Moderator

Oct 18, 2013, 2:37 PM


Views: 16497
Re: [OB53] Question on book "Beginning Perl" and running examples

If you don't want to mess with manually editing the registry, then you should completely uninstall perl then reinstall. The default install should fix the reg entries for the file type association, unless you deselected that option during the install.


OB53
Novice

Oct 18, 2013, 2:49 PM


Views: 16495
Re: [FishMonger] Question on book "Beginning Perl" and running examples

I made the reg change to: C:perl\\bin\\perl.exe\" \"%1\" %*"
I ran the code with same result. I rebooted PC and still same result. I do have one question though, what are the double slashes for? \\strawberry\\ etc


(This post was edited by OB53 on Oct 18, 2013, 2:56 PM)


OB53
Novice

Oct 18, 2013, 2:55 PM


Views: 16491
Re: [FishMonger] Question on book "Beginning Perl" and running examples

I am running windows 7, 64 bit, version 6.1 build 7601 Svc Pk 1.
The Perl that I installed is: strawberry-perl-5.16.3.1-64bit
The reg edit ver is the same as windows... See attached


(This post was edited by OB53 on Oct 18, 2013, 3:01 PM)
Attachments: regeditver.jpg (93.0 KB)


FishMonger
Veteran / Moderator

Oct 18, 2013, 2:57 PM


Views: 6435
Re: [OB53] Question on book "Beginning Perl" and running examples

The backslash is the escape character and when doubled up in the .reg file, it escapes the backslash used in the path. If you're manually editing the registry, then you don't need/want to use double backslashes.


OB53
Novice

Oct 18, 2013, 3:26 PM


Views: 6432
Re: [FishMonger] Question on book "Beginning Perl" and running examples

So then it should be like this: "C:\strawberry\perl\bin\perl.exe\" \"%1\" %*"


(This post was edited by OB53 on Oct 18, 2013, 3:28 PM)


OB53
Novice

Oct 18, 2013, 3:39 PM


Views: 6426
Re: [OB53] Question on book "Beginning Perl" and running examples

No go, at least now I get a flashing cursor. not at c prompt though. A control+C gave me this: "^vTerminating on signal SIGINT(2)", which I never got before.


OB53
Novice

Oct 18, 2013, 4:48 PM


Views: 6418
Re: [OB53] Question on book "Beginning Perl" and running examples

any suggestions as to where to go from here?


FishMonger
Veteran / Moderator

Oct 18, 2013, 6:19 PM


Views: 6408
Re: [OB53] Question on book "Beginning Perl" and running examples

You don't need/want to escape the quotes.

It should be:
"C:\strawberry\perl\bin\perl.exe" "%1" %*

or
"C:\strawberry\perl\bin\perl.exe" "%1"

The first one is what is configured on my work computer and the second one is what I have on my laptop.


OB53
Novice

Oct 18, 2013, 8:40 PM


Views: 6404
Re: [FishMonger] Question on book "Beginning Perl" and running examples

Neither one works. I have a questions though. Is this like the old PATH statement in windows 3.1? Reason I ask is I have no directory of "C:\Strawberry". And you say that on your work PC you have "C:\Strawberry\perl\bin\perl.exe" "%1" %* is that correct in having the last % not in quotes but rather as %*
BTW my working path for perl is "C:\Perl64\perl\bin\perl.exe"


FishMonger
Veteran / Moderator

Oct 18, 2013, 9:29 PM


Views: 6396
Re: [OB53] Question on book "Beginning Perl" and running examples

It appears that you're not reading some of my posts.

I mentioned several times that you need to adjust the path in my posts to match your perl installation.


FishMonger
Veteran / Moderator

Oct 19, 2013, 11:30 AM


Views: 6388
Re: [OB53] Question on book "Beginning Perl" and running examples


In Reply To
Neither one works. I have a questions though. Is this like the old PATH statement in windows 3.1? Reason I ask is I have no directory of "C:\Strawberry". And you say that on your work PC you have "C:\Strawberry\perl\bin\perl.exe" "%1" %* is that correct in having the last % not in quotes but rather as %*
BTW my working path for perl is "C:\Perl64\perl\bin\perl.exe"


The registry value should point to your working path to perl i.e.,
"C:\Perl64\perl\bin\perl.exe" "%1"

If that doesn't work for you, then your perl installation is broken and you should do a complete uninstall/reinstall of perl.


OB53
Novice

Oct 26, 2013, 3:08 PM


Views: 6350
Re: [FishMonger] Question on book "Beginning Perl" and running examples

Well, after a couple uninstalls and reinstalls, I got to the bottom of my problem. When I ran my first Perl program I got an error saying " 'program.plx' is not recognized as an internal or external command, operable program or batch file." Upon seeing this I recognized what I thought the problem was and that being the same as one from back in my old DOS days. Since I was running the program from a different directory than where Perl was located it wouldn't run. So I went into the windows directory where the program was located and associated it with Perl. End of my problem. So when I ran each program from the command line they worked. That is until chapter 6. So when it came time to run "nl2.plx nlexample.txt", that is exactly what I typed.
The book read "> perl nl2.plx nlexample.txt", but my mind processed that as "perl>nl2.plx nlexample.txt". I reread the line countless times before I finally saw it. When I finally typed the all important command "perl" before program, they all started working. End of problem.
Thanks to all for your input and help even though it was a typo on my part. The reason that I explained my problem here is that if it happened to me, it just might happen to someone else and perhaps someone will remember this.
Frown