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:
syntax errors in my first perl program

 



mattomynameo
New User

Apr 12, 2010, 10:16 AM

Post #1 of 5 (423 views)
syntax errors in my first perl program Can't Post

Just can't figure this out...Frown I'm really trying to learn and become familiar w/ perl and regex.

syntax error at ipdomain_extract.perl line 38, near "/ ) "
(Might be a runaway multi-line )) string starting on line 34)
syntax error at ipdomain_extract.perl line 48, near "}"
Execution of ipdomain_extract.perl aborted due to compilation errors.

Here's my code, many thanks to those who can help!

Code
#!/usr/bin/perl 
#
# This program examines all files in $directory,
# assumes that they are email messages, extracts
# and prints the IP address of the sender and all
# domains mentioned in the message.

$directory='./hardham';
opendir(DIR, $directory);
while (defined($file = readdir(DIR))) {
if (-T "$directory/$file") { #if textfile
open(INFO, "$directory/$file");
@lines = <INFO>; #@lines holds all lines in $file
&extract_print;
}
}
closedir(DIR);

exit;

sub extract_print {
local($header, $ip, @domains)=(0, '', '');
foreach $line (@lines) {
if ($header == 0) { #extract and print IP address
if ($line =~ /^Received/) {
if ($line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) { #if $line has an IP, store it in $1
if(($1 !~ /^10./) && ($1 !~ /^192./)) {
$ip = $1; #external ips only
}
}
}
if ($line =~ \s) { #empty line means in the message
$header++;
}
} else { #extract and print domain names
if ($line =~ /\bhttp:\/\/.*?(\w+\.\w{2,3})(\/|\b)/ ) {
push(@domains, $1);
}
}
}
print "\nFile: $file\n";
print "IP: $ip\n";
foreach $domain (@domains) {
print "Domain: $domain\n";
}
}



FishMonger
Veteran / Moderator

Apr 12, 2010, 1:09 PM

Post #2 of 5 (419 views)
Re: [mattomynameo] syntax errors in my first perl program [In reply to] Can't Post

The problem is with this line>

Code
			if ($line =~ \s) {	#empty line means in the message


Add these 2 pragmas and fix all of the problems they point out.

Code
use strict; 
use warnings;



mattomynameo
New User

Apr 12, 2010, 1:31 PM

Post #3 of 5 (417 views)
Re: [FishMonger] syntax errors in my first perl program [In reply to] Can't Post

Thanks for the response, FishMonger

I've rewritten my code so it reads:

Code
#!/usr/bin/perl 
#
# This program examines all files in $directory,
# assumes that they are email messages, extracts
# and prints the IP address of the sender and all
# domains mentioned in the message.

use strict;
use warnings;

our ($directory, $file, @lines) = ('', '', '');

$directory='./hardham';
opendir(DIR, $directory);
while (defined($file = readdir(DIR))) {
if (-T "$directory/$file") { #if textfile
open(INFO, "$directory/$file");
@lines = <INFO>; #@lines holds all lines in $file
&extract_print;
}
}
closedir(DIR);

exit;

sub extract_print {
my($header, $ip, $domain, @domains, $line)=(0, '', '', '', '');
foreach $line (@lines) {
if ($header == 0) { #extract and print IP address
if ($line =~ /^Received/) {
if ($line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) { #if $line has an IP, store it in $1
if(($1 !~ /^10./) && ($1 !~ /^192./)) {
$ip = $1; #external ips only
}
}
}
if ($line =~ \s) { #empty line means in the message
$header++;
}
} else { #extract and print domain names
if ($line =~ /\bhttp:\/\/.*?(\w+\.\w{2,3})(\/|\b)/ ) {
push(@domains, $1);
}
}
}
print "\nFile: $file\n";
print "IP: $ip\n";
foreach $domain (@domains) {
print "Domain: $domain\n";
}
}



All I really did was declare variables that were used. I think I got the local variables set right in the function, but the other variables ($directory, $file, @lines, ...) aren't supposed to be local I don't think, so I used our() instead of my() after some googling.. Anyway, here's what the consol is spitting out now:

Code
syntax error at ipdomain_extract.perl line 43, near "/ ) " 
(Might be a runaway multi-line )) string starting on line 39)
Global symbol "$ip" requires explicit package name at ipdomain_extract.perl line 49.
Global symbol "$domain" requires explicit package name at ipdomain_extract.perl line 50.
Global symbol "@domains" requires explicit package name at ipdomain_extract.perl line 50.
Global symbol "$domain" requires explicit package name at ipdomain_extract.perl line 51.
syntax error at ipdomain_extract.perl line 53, near "}"
Execution of ipdomain_extract.perl aborted due to compilation errors.


I don't get why it thinks $ip and $domain and @domains are global symbols when they're in a my(). Any more hints to lead me in the right direction? How does one give a variable an explicit package name?

muchas gracias!!!


FishMonger
Veteran / Moderator

Apr 12, 2010, 2:59 PM

Post #4 of 5 (415 views)
Re: [mattomynameo] syntax errors in my first perl program [In reply to] Can't Post

The "our" keyword creates package globals and can be made available to a parent script. The "my" keyword creates lexical vars and at most have file scope. Lexical vars declared outside of a block have file scope. With some rare exceptions, all vars should be declared with the "my" keyword and they should be declared in the smallest scope that they require.

Your syntax error could be corrected by changing:

Code
if ($line =~ \s) {

To:

Code
if ($line =~ /^\s*$/) {



mattomynameo
New User

Apr 12, 2010, 4:44 PM

Post #5 of 5 (411 views)
Re: [FishMonger] syntax errors in my first perl program [In reply to] Can't Post

FishMonger, thanks a bunch! I've got it working now, and I understand why it wasn't working.

 
 


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

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