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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of Stict at odds with symbolic references?

Status
Not open for further replies.

korneel

IS-IT--Management
Mar 11, 2001
6
EU
Hello,

I 'm quite new to Perl, and have a problem with a parse function I found. It parses an html page, and passes on the values of the variables in a cgi script to the html page. Problem is, my ISP requests that i use "use strict", and the parse subroutine (see below) does not seem to like that:

# parse_file opens and prints a template file, substituting %%var%% for the
# value of $var:
sub parse_file {
open (FILE, $_[0]) or error("$_[0]: $!\n");
while (<FILE>) {
s/%%(\w+)%%/${$1}/ig;
print;
}
close (FILE);
return;
}

I get the error message:

Can't use string (&quot;artnr&quot;) as a SCALAR ref while &quot;strict refs&quot; in use.

This seems logical when I think about it, but leaves me with the problem of having to find another solution to pass the variables on to the html page. I have been looking around, but could not find something as of yet.

Any suggestions would be very helpful.

Thanks,
Korneel.
 
Erm...

Stict must be Strict of course.

Any help very much appreciated,

Korneel.
 
Sorry to keep you waiting..... I'm a little backed up in my own work. Maybe, coincidentlally, other members are also. Welcome to TTs, by the way. I hope you get quicker better help in the future.

I don't see anything in the code you posted that might cause that problem Usually, when I get a complaint from 'strict' it has something to do with the string or variable that it lists in its complaint (eg. artnr). Sometimes, the complaints are more obtuse, but, usually the complaint points to the 'area' of the problem. I don't see that string (artnr) used in the code you posted. Does it exist anywhere else in your code? Or, might it be a value of a var that is being referenced?




keep the rudder amid ship and beware the odd typo
 
Hello goBoating,

Many thanks for that.

I'll be able to answer your questions in greater detail tonight (GMT, noonish PST ), but for now:

I want the subroutine to transmit the values of the variables in my perl script to the html page. The variabels come from a flat database (read in by another subroutine).

All variabels are declared at the start of the script by a use vars(var var var), as recommended in one of the online references at perl.com (solved the error message of the type &quot;Global symbol &quot;%s&quot; requires explicit package name&quot;). The exact syntax of the line I can't recall right now, but will look up tonight. In fact, I'll post the whole script tonight... (much easier talking).

So, more to come,
& Thanks again, Korneel.

 
Well, here it is.

As said, I'm new to perl. This is an adaptation of an existing script but I stripped and changed a lot. Must have done something wrong, But what?

Thanks!

Korneel.


#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;


# Section 1: config variables

use vars qw($self $file_root $mainfile $kbfile $logfile $count @params $function $id @entries %entries $artnr $omschr $plaatje $maten $przw $prkl $str $field @ids $last %titles @line $aantal_prod);

$self = $ENV{'SCRIPT_NAME'};


$mainfile ='boubuframefiles/catmiddle.html';

$kbfile = 'artfile1.txt';
$logfile = 'kblog.log';
$count = 0;

@params = ( &quot;function&quot;, &quot;ID&quot;, &quot;str&quot;, &quot;srcmode&quot;);



# Section 2: script manager

# check for invalid input data:
&secure_params();

# handle the request, depending on what $function is:
$function = param('function');
if ($function eq &quot;display&quot;) {
&read_kb($kbfile);
&display(param('ID'));
&parse_file($mainfile);
} elsif ($function eq &quot;search&quot;) {
&read_kb($kbfile);
&search(param('str'), param('srcmode'));
}

# bye bye!
exit;

# Section 3: main subroutines

sub display {
$id = shift;
&record_log($id);

@entries = split(&quot;\t&quot;, $entries{$id});
$artnr = $entries[1];
$omschr = $entries[2];
$plaatje = $entries[3];
$maten = $entries[4];
$przw = $entries[5];
$prkl = $entries[6];

return;
}

sub search {
($str, $field) = @_;

foreach (@ids) {
if (${$field}{$_} =~ /$str/i) {
$last = $_;

print &quot; <li><a href=\&quot;$self?function=display&ID=$_\&quot;>$titles{$_}</a></li>\n&quot;;

$count++;
}
}

if ($count == 1) {
&display($last);
} else {
print &quot;</ol>\n<p>Found <b>$count</b> results.</p>\n&quot;;
}
return;
}


# Section 4: cross-function subroutines

# secure_params returns an error if the entry text contains naughty characters:
sub secure_params {
foreach (@params) {
print &quot;securing params: invalid characters found in $_.&quot; unless (param($_) =~ /^([-_\/\@\s\w.]*)$/);
}
return;
}

# read_kb loads the DB file into memory, using an array of ID strings (@ids),
# and two hashes, %titles and %entries, whose keys are the ID string of the
# appropriate entry:
sub read_kb {
open (FILE, $_[0]);
while (<FILE>) {
chomp;
s/^\&quot;//g; s/\t\&quot;/\t/g; s/\&quot;\t/\t/g; s/\&quot;$//g;
unless (/^\;/) {
@line = split(&quot;\t&quot;, $_, 3);
push(@ids, $line[0]);
$titles{$line[0]} = $line[1];
$entries{$line[0]} = $line[2];
}
}
$aantal_prod=length(@ids);
close (FILE);
return;
}

# parse_file opens and prints a template file, substituting %%var%% for the
# value of $var:
sub parse_file {
open (FILE, $_[0]) or error(&quot;$_[0]: $!\n&quot;);
while (<FILE>) {
s/%%(\w+)%%/${$1}/ig;
print;
}
close (FILE);
return;
}


# record_log simply writes the ID requested into a text file.
sub record_log {
$id = shift;
open (LOGFILE, &quot;>>$logfile&quot;);
print LOGFILE join(&quot;\t&quot;, ($id, $titles{$id})), &quot;\n&quot;;
close (LOGFILE);
return;
}

 
Hello,

Could anyone help me out?

Thanks!

Korneel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top