Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another regexp problem

Status
Not open for further replies.

tanderso

IS-IT--Management
Aug 9, 2000
981
US
I'm just full of these today ;-) I get the error: Use of uninitilized value at ... line ..., pointing to these lines:

$line =~ s/<!--#include(.*?)=[&quot;']\/(.*?)[&quot;'](.*?)-->/
grab(&quot;$global_inc\/$2&quot;,$path,$inc_path,$global_inc)/eg;

$line =~ s/<!--#include(.*?)=[&quot;'](.*?)[&quot;'](.*?)-->/
grab(&quot;$path\/$inc_path\/$2&quot;,$path,$inc_path,$global_inc)/eg;

The variable $line is defined, as is the subroutine grab(). In fact, these lines are *in* the subroutine grab(). $path, $inc_path, and $global_inc are also defined. Any ideas?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
first best guess,

Are you defining variables that you are passing to grab before you get to these two replace statements? If they are not being set until you get into grab, then they are null when each of these replace statements is evaluted. If those values are static, there is no need to pass them to the sub, just set them in the sub or set them globally and then, do the concatenation in the sub....

maybe?

'hope this helps....



keep the rudder amid ship and beware the odd typo
 
Here's the entire subroutine (changed a little since I last posted but still giving the error):

sub grab
{
my ($include_file,$path,$inc_path,$global_inc,$script) = @_;
my $line = &quot;&quot;;
if (($include_file =~ /\.inc/)||($include_file =~ /\.asp/))
{
# print &quot;getting $include_file...<br>\n&quot;;
$line = get($include_file);
}
else
{
# print &quot;including $include_file...<br>\n&quot;;
open (HTML,$include_file) || &amp;CgiDie(&quot;Could not open $include_file: $!&quot;);
my (@DATA)=<HTML>;
close(HTML);
$line = join(&quot;\n&quot;,@DATA);
}

# insert a script in the HTML head if provided
$line =~ s/(<head(.*?)>)(.*?)(<\/head\s*>)/$1$3$script$4/gis;

$line =~ s/<!--#include(.*?)=[&quot;']\/(.*?)[&quot;'](.*?)-->/grab(&quot;$global_inc\/$2&quot;,$path,$inc_path,$global_inc,$script)/eg;
$line =~ s/<!--#include(.*?)=[&quot;'](.*?)[&quot;'](.*?)-->/grab(&quot;$path\/$inc_path\/$2&quot;,$path,$inc_path,$global_inc,$script)/eg;

return ($line);
}

All of the parameters passed into the subroutine are strings that have been defined. I am passing them because I am using strict even though they could otherwise be global.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I'm also getting that error on the new substitution directly above the other two. And the really weird part is that now I'm getting it on the regexp in this sub:

sub html_encode
{
my ($str) = @_;
$str =~ s/([^0-9A-Za-z])/sprintf(&quot;&amp;#%d;&quot;,ord($1))/eg;

return $str;
}
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Ok, this makes no sense. I write this:

if ($line&amp;&amp;$global_inc&amp;&amp;$path&amp;&amp;$inc_path)
{
$line =~ s/<!--#include\s.*?=\s?[&quot;']\/(.*?)[&quot;'].*?-->/grab(&quot;$global_inc\/$1&quot;,$path,$inc_path,$global_inc)/egi;
$line =~ s/<!--#include\s.*?=\s?[&quot;'](.*?)[&quot;'].*?-->/grab(&quot;$path\/$inc_path\/$1&quot;,$path,$inc_path,$global_inc)/egi;
}

And it still tells me there is an uninitialized value. Won't the &quot;if&quot; statement evaluate to false if one of the values is uninitialized? Could it be misinterpreting something in the regexp &quot;<!--#include&quot; as a variable or function? Could it think the # is a comment?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I can't point to your error, but i can say that i often get the same error from regular expressions when they are improperly matching the string. It may be that the (.*?) part isn't actually catching anything. Try making a simpler version just to print out the $1. I don't know for sure, but whenever this happens to me, i start building the regex from scratch with a copy of the string to be matched and the text to extract it from open right next to the script window.
Good luck,
Martin
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Tom,
can you post a little of the HTML that you are working on? I can make the error go away, but I am having to alter the code so much that I am probably getting away from what you are trying to do. With a copy of one of the HTML lines you are trying to match, we could troubleshoot the matching easily. Then, if the matching/replacing is working, we can look at the larger picture.

Another thought..... do you mean to be calling grab from within itself? <...ponder...ponder... think......scratch head a little>

Another thought..... are you sure $file is getting set?


keep the rudder amid ship and beware the odd typo
 
Sorry I haven't gotten back to you guys lately, I've been incapacitated with a bad case of strep. I guess that's what happens when you have the stress of needing two major redesigns done in two weeks, a Perl proxy app done in two days, three clients asking for meetings in as many days, a broken furnace, defending against a speeding ticket, a dentist appointment, errors in my credit report, errors in my driving record, being overcharged by insurance and utilities, ... and the list goes on. I guess it was just a matter of time.

I think I've fixed my regex problem now.

if (length($line)) {$line =~ s/.../.../egi;}
if (length($line)) {$line =~ s/.../.../egi;}

$line doesn't only have to exist, it also appears to require a non-zero length to be used in a regular expression. In the second line, I can see how that might happen -- the first regexp might remove everything from $line. I still don't see how that could ever happen at the first line, but it doesn't matter... as long as it works. And it appears to be working now.

Thanks for all of your help.

BTW, $include_file should always exist. The first time it is named explicitly by hard-coded variables. Each additional time, it is the result of a match from these two lines. And, yes, grab() is recursive. It reads a server-side include and pastes the contents into the output. If the include contains includes, then those are also translated. Unfortunately, I couldn't get that first case to work where it executes inc and asp files. I was hoping to be able to have asp parsed, but that doesn't seem possible unless it is a whole, independent file. Does anyone know if IIS can send Perl output through the asp parser?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Sorry to hear about the stress, Tom. We are not superstitious people, but, my wife says that she suspects that trouble events come in 3's. You've corrupted that theory. Sounds like you are getting them by the dozens! Hang in there and don't forget to enjoy life sometime..... [red]go Boating[/red] or something ;^)

I don't play with IIS, so I am sorry, I can't help you over there in MicroSoft land.


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top