CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 

First page Previous page 1 2 Next page Last page  View All


OB53
Novice

Oct 17, 2013, 9:15 PM

Post #1 of 34 (1531 views)
Question on book "Beginning Perl" and running examples Can't Post

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

Post #2 of 34 (1522 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #3 of 34 (1510 views)
Re: [hwnd] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #4 of 34 (1505 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #5 of 34 (1497 views)
Re: [Zhris] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #6 of 34 (1492 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #7 of 34 (1487 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #8 of 34 (1480 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #9 of 34 (1476 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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


FishMonger
Veteran / Moderator

Oct 18, 2013, 12:21 PM

Post #10 of 34 (1474 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #11 of 34 (1470 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #12 of 34 (1463 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #13 of 34 (1457 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

All I get is:
$VAR1 = [];


OB53
Novice

Oct 18, 2013, 12:54 PM

Post #14 of 34 (1452 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #15 of 34 (1451 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #16 of 34 (1445 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #17 of 34 (1436 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post


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

Post #18 of 34 (1430 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #19 of 34 (1426 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #20 of 34 (1426 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post


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

Post #21 of 34 (1420 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #22 of 34 (1414 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #23 of 34 (1412 views)
Re: [OB53] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #24 of 34 (1410 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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

Post #25 of 34 (1406 views)
Re: [FishMonger] Question on book "Beginning Perl" and running examples [In reply to] Can't Post

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)

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives