problem with HangUP

L

lucas

$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); }; #Catch HanUPs, and reread all loaded files

All I want this to do is catch a HangUP signal, run the LoadFiles() routine
and return to what it was doing. However this exits the program. It
doesn't die, it just exits. Any idea on how I can stop this from exiting?

thx
 
A

Anno Siegel

lucas said:
$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); }; #Catch HanUPs, and reread all loaded files

All I want this to do is catch a HangUP signal, run the LoadFiles() routine
and return to what it was doing. However this exits the program. It
doesn't die, it just exits. Any idea on how I can stop this from exiting?

No.

The code you show doesn't exit. If it exits for you, that happens in the
code you don't show. We can't correct code we don't get to see.

Anno
 
L

lucas

Anno said:
No.

The code you show doesn't exit. If it exits for you, that happens in the
code you don't show. We can't correct code we don't get to see.

the sub routine doesn't exit either. i know this because i run it at the
start of the program. And i know that the $SIG(HUP) shouldn't exit, but it
does. i put a 'print "DONE LOADING\N"; ' in the $SIG, as you can see, and
it is printed to screen, so the LoadFiles() returns properly.


$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); print "DONE LOADING\n"; };

sub LoadFiles {
my $adfilterfile = shift;

#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
@filters = <FILE>;
close(FILE);
for($_=0;$_<@filters;$_++) {
@filters[$_] =~ s/[\r|\n]//g; #Rid of \r \n!
if (@filters[$_] eq '') { pop(@filters); $_--; next; } #skip entry if
it's empty
print ">@filters[$_]\n" if ($debug);
}
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}
 
L

lucas

lucas said:
the sub routine doesn't exit either. i know this because i run it at the
start of the program. And i know that the $SIG(HUP) shouldn't exit, but
it
does. i put a 'print "DONE LOADING\N"; ' in the $SIG, as you can see, and
it is printed to screen, so the LoadFiles() returns properly.


$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); print "DONE LOADING\n"; };

sub LoadFiles {
my $adfilterfile = shift;

#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
@filters = <FILE>;
close(FILE);
for($_=0;$_<@filters;$_++) {
@filters[$_] =~ s/[\r|\n]//g; #Rid of \r \n!
if (@filters[$_] eq '') { pop(@filters); $_--; next; } #skip entry
if
it's empty
print ">@filters[$_]\n" if ($debug);
}
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}


I just found that it always has an exit code of "4", if this helps at all
 
A

Anno Siegel

Don't top-post please. You're not exactly new to this group, aren't you?
the sub routine doesn't exit either. i know this because i run it at the
start of the program.

Where? The code you show doesn't call "the sub" (I suppose LoadFiles()).
And i know that the $SIG(HUP) shouldn't exit, but it
does. i put a 'print "DONE LOADING\N"; ' in the $SIG, as you can see, and
it is printed to screen, so the LoadFiles() returns properly.

The code below is incomplete, and it doesn't run under strict and warnings.
To run it, people must make all kinds of amendments, which also means
making assumptions about the content of variables and files that you don't
show. So we still don't know what code you ran.

Show a complete, runnable piece of code, tell us what you expect it to
do and explain how what it does is different.

I have annotated your code and pointed out some mistakes. I don't know
if these have anything to do with your perceived problem.
$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); print "DONE LOADING\n"; };

What is $adfilterfile? A global variable? It is nowhere declared, nor
does it have a value.
sub LoadFiles {
my $adfilterfile = shift;

This variable "$adfilterfile" is lexically scoped to the sub block. It
has nothing to do with "$adfilterfile" outside the sub. Apparently you
expect them to be the same, since you never do anything with "$adfilterfile"
in the rest of the routine. Maybe that's your error, but who knows.

If so, LoadFiles should return the array at the end, and the call should
catch it.
#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);

What is "$debug"? It is nowhere declared, nor does it have a value.
Is it supposed to be true or false during the runs you mention?
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
@filters = <FILE>;

"@filters" is undeclared.
close(FILE);
for($_=0;$_<@filters;$_++) {

Why the C-style indexed loop? It is rarely needed and only encumbers
your code.
@filters[$_] =~ s/[\r|\n]//g; #Rid of \r \n!

"@filters[ $_]" is a one-element array slice. That is better written as
"$filters[ $_]", and "warnings" would have told you so. The same applies
to two more uses of "@filters[ $_]".

"s/[\r|\n]//g" doesn't do what you think it does. It kills "|" besides
"\r" and "\n", and it kills the first appearance of any of those, not
the one at the end of the line.

What you mean is (untested) "s/[\r\n]$//" or "s/(?:\r|\n)$//", but
usually "chomp" does that job. Why do you think it doesn't in your
case?
if (@filters[$_] eq '') { pop(@filters); $_--; next; } #skip entry if
it's empty

Ugh. Just say "@filters = grep /./, @filters" in any convenient place
before the loop.
print ">@filters[$_]\n" if ($debug);
}
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}

So this is what LoadFiles should look like (untested, and minus debugging
statements):

sub LoadFiles {
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
chomp( my @filters = grep /./, <FILE>);
print "$_\n" for @filters;
$adfilterfile;
}

....and call it as

$adfilterfile = LoadFiles;

This is just a translation of what you have into more reasonable Perl. It
still doesn't make much sense. In particular, the array @filters is
only used to print the values, it's gone after the sub returns.

From your treatment LoadFiles it is apparent that you have no clear grasp
of the workings of sub arguments and return values. You still seem to
reach for global variables when these should be used. After three years
of Perl programming you should know better.

Anno
 
G

GreenLight

lucas said:
sub LoadFiles {
my $adfilterfile = shift;

#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
@filters = <FILE>;
close(FILE);
for($_=0;$_<@filters;$_++) {
@filters[$_] =~ s/[\r|\n]//g; #Rid of \r \n!
if (@filters[$_] eq '') { pop(@filters); $_--; next; } #skip entry if
it's empty
print ">@filters[$_]\n" if ($debug);
}
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}

lucas:
I don't know about your actual problem, but I am wondering if you had
ever considered writing your code like this:

sub LoadFiles {
my $adfilterfile = shift;

#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile:
$!\n";
while (<FILE>) {
chomp;
next if //;
push @filters, $_;
print ">$_\n" if ($debug);
}
close(FILE);
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}

It is a bit cleaner-looking amd more "conventional".
Mind you, I'm not saying that this is "the way" to go, but you might
consider it.

Also, one thing that I often do to reduce the "if ($debug)"-type
statements is to define a sub called "chatter":

sub chatter {
if ($debug) {print shift;}
}

This way, I can code it like this:

chatter("$$: ", $_ = @filters," Strings Loaded\n");
 
L

lucas

GreenLight said:
lucas:
I don't know about your actual problem, but I am wondering if you had
ever considered writing your code like this:

sub LoadFiles {
my $adfilterfile = shift;

#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile:
$!\n";
while (<FILE>) {
chomp;
next if //;
push @filters, $_;
print ">$_\n" if ($debug);
}
close(FILE);
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}

It is a bit cleaner-looking amd more "conventional".
Mind you, I'm not saying that this is "the way" to go, but you might
consider it.

Also, one thing that I often do to reduce the "if ($debug)"-type
statements is to define a sub called "chatter":

sub chatter {
if ($debug) {print shift;}
}

This way, I can code it like this:

chatter("$$: ", $_ = @filters," Strings Loaded\n");

Thanks for the code, it is nicer looking. I've never consider using push
when reading from a text file. Think i'll give it a try

Thanks again,
 
L

lucas

Anno said:
Don't top-post please. You're not exactly new to this group, aren't you?
the sub routine doesn't exit either. i know this because i run it at the
start of the program.

Where? The code you show doesn't call "the sub" (I suppose LoadFiles()).
And i know that the $SIG(HUP) shouldn't exit, but
it
does. i put a 'print "DONE LOADING\N"; ' in the $SIG, as you can see,
and it is printed to screen, so the LoadFiles() returns properly.

The code below is incomplete, and it doesn't run under strict and
warnings. To run it, people must make all kinds of amendments, which also
means making assumptions about the content of variables and files that you
don't
show. So we still don't know what code you ran.

Show a complete, runnable piece of code, tell us what you expect it to
do and explain how what it does is different.

I have annotated your code and pointed out some mistakes. I don't know
if these have anything to do with your perceived problem.
$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); print "DONE LOADING\n"; };

What is $adfilterfile? A global variable? It is nowhere declared, nor
does it have a value.
sub LoadFiles {
my $adfilterfile = shift;

This variable "$adfilterfile" is lexically scoped to the sub block. It
has nothing to do with "$adfilterfile" outside the sub. Apparently you
expect them to be the same, since you never do anything with
"$adfilterfile"
in the rest of the routine. Maybe that's your error, but who knows.

If so, LoadFiles should return the array at the end, and the call should
catch it.
#load ad filter strings into memory
print "$$: Loading in Ad Filters from $adfilterfile..." if($debug);

What is "$debug"? It is nowhere declared, nor does it have a value.
Is it supposed to be true or false during the runs you mention?
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile: $!\n";
@filters = <FILE>;

"@filters" is undeclared.
close(FILE);
for($_=0;$_<@filters;$_++) {

Why the C-style indexed loop? It is rarely needed and only encumbers
your code.
@filters[$_] =~ s/[\r|\n]//g; #Rid of \r \n!

"@filters[ $_]" is a one-element array slice. That is better written as
"$filters[ $_]", and "warnings" would have told you so. The same applies
to two more uses of "@filters[ $_]".

"s/[\r|\n]//g" doesn't do what you think it does. It kills "|" besides
"\r" and "\n", and it kills the first appearance of any of those, not
the one at the end of the line.

What you mean is (untested) "s/[\r\n]$//" or "s/(?:\r|\n)$//", but
usually "chomp" does that job. Why do you think it doesn't in your
case?
if (@filters[$_] eq '') { pop(@filters); $_--; next; } #skip entry
if
it's empty

Ugh. Just say "@filters = grep /./, @filters" in any convenient place
before the loop.
print ">@filters[$_]\n" if ($debug);
}
print "$$: ", $_ = @filters," Strings Loaded\n" if($debug);
}

So this is what LoadFiles should look like (untested, and minus debugging
statements):

sub LoadFiles {
open(FILE,"$adfilterfile") || die "Error opening $adfilterfile:
$!\n"; chomp( my @filters = grep /./, <FILE>);
print "$_\n" for @filters;
$adfilterfile;
}

...and call it as

$adfilterfile = LoadFiles;

This is just a translation of what you have into more reasonable Perl. It
still doesn't make much sense. In particular, the array @filters is
only used to print the values, it's gone after the sub returns.

From your treatment LoadFiles it is apparent that you have no clear grasp
of the workings of sub arguments and return values. You still seem to
reach for global variables when these should be used. After three years
of Perl programming you should know better.

Anno


thanks for the heads up on cleaning up my code. s/[\r\n]$// was actually
given to me by somebody else, it's not my own
 
L

lucas

lucas said:
$SIG{HUP} = sub { print STDERR "$$: HUP Caught\n";
LoadFiles($adfilterfile); }; #Catch HanUPs, and reread all loaded files

All I want this to do is catch a HangUP signal, run the LoadFiles()
routine
and return to what it was doing. However this exits the program. It
doesn't die, it just exits. Any idea on how I can stop this from exiting?

thx


I finally found the problem I was having. This program listens on a port,
and what was happening was when a HangUP was receieved, and the sub routine
finished, the script was returning true to $new_sock = $socket->accept();
(i think), and obviously $new_sock would be undefined, and the script died.

Thanks for all your posts,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top