Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
Every time I substitute these variables only the $code variable prints. I never see the $stats variable print.
I have tried many variations but I am clearly not interpreting something correctly.
Any Ideas Thanks
my $statholder = "<BODY>"; my $code = "<BODY>"; my $stats = "<!--#exec cgi='\/cgi-local\/axis\/ax.pl'-->"; $checksize1 =~ s/$statholder/$code $stats/g;

Replies are listed 'Best First'.
Re: problem with substituting
by jarich (Curate) on Nov 20, 2002 at 03:10 UTC
    When I run the code:
    my $checksize1 = "fhish <BODY> fhosh"; my $statholder = "<BODY>"; my $code = "<BODY>"; my $stats = "<!--#exec cgi='\/cgi-local\/axis\/ax.pl'-->"; print "[$checksize1]\n"; $checksize1 =~ s/$statholder/$code $stats/g; print "[$checksize1]\n";
    it outputs:
    [fhish <BODY> fhosh] [fhish <BODY> <!--#exec cgi='/cgi-local/axis/ax.pl'--> fhosh]
    So, so far as I can tell it's working perfectly. Perhaps you need to explain again, what the problem is? Perhaps with some sample data and an indication of what you want to see.

    Are you hoping that when $stats is used in the substitution that the script in /cgi-local/axis/ax.pl will be executed and the result of that used?

    If so this isn't the way to go about it. Try explaining what you're trying to do a little more clearly.

    jarich

      $checksize is being written to an .shtml file. I was hoping to execute the /cgi-local/axis/ax.pl would be executed in that file. Thanks
        How about you either show us more code, or offer us some pseudo-code. I think you might mean the following, but you might also mean something entirely different.
        my $checksize1 = qq{<html><head></head><body>Huge wad of html</body></ +html>}; # Run ./cgi-local/axis/ax.pl over $checksize1 # Note, calling qx{} is the same as using backticks # calling qx{} in an array context means each # separate line returned is added as the next # element in an array. If you want it all in # a single scalar, call it in scalar context. # doing things this way assumes that ax.pl is # happy to take info on STDIN my @results = qx{ ./cgi-local/axis/ax.pl < $checksize1 };
        Note the lack of substitute. If you want to replace $checksize1 with the result of calling ax.pl then all you need to do is:
        $checksize1 = qx{ ./cgi-local/axis/ax.pl < $checksize1 };
        Now, if you ONLY wanted to run ax.pl over the BODY of the huge wad of html, or any other small section then the method you have to use is different. You'll need to extract that small section and then rejoin the strings. Something like:
        my $checksize1 = qq{<html>\n<head>\n</head>\n<body>\nHuge wad of html\ +n</body>\n</html>}; my ($head, $body, $foot) = $checksize1 =~ m{(.*)<body>(.*)</body>(.*)$ +}si; $body = qx{./cgi-local/axis/ax.pl < $body}; $checksize1 = qq{$head<body>\n$body</body>$foot};
        Note however that HTML is MUCH more complicated that this (consider the possiblity of having a close body tag within your input...) and that (at the least) you'll need to ensure that your BODY tags are consistant such that you can make a regular expression that will consistantly find and match them. (Note also that .* isn't a recommended grab all). You will need to have a better regexp than I have here. You might even consider using one of the HTML modules (like HTML::Parser).

        If all you are trying to do is to run ax.pl over the .shtml file once it's been written to, your job is much easier.

        open(FILE, "> blah.shtml") or die "$!"; print FILE $checksize1; close(FILE); my $res = qx{./cgi-local/axis/ax.pl blah.shtml}; if($res ....){} # don't forget to ensure that ax.pl worked.

        If none of this helps you do what you need to do, then please try responding with more than 2 sentences explaining in detail what it is that you are trying to do, and giving us clear test data and expected results.

        Hope this helps.

        jarich

Re: problem with substituting
by Louis_Wu (Chaplain) on Nov 20, 2002 at 03:04 UTC
    Every time I substitute these variables only the $code variable prints. I never see the $stats variable print.

    I don't see the print statements. How are you printing $stats, and how is that different from the way you print $code?

    'Course, I don't know much about CGI, so maybe that HTML comment is what you're talking about.

Re: problem with substituting
by pg (Canon) on Nov 20, 2002 at 03:08 UTC
    What do you mean by 'print'? what is the value of $checksize1? What are you try to accomplish? Please give more info.
      sorry about that.. I suppose is what I meant was it is never writing to the variable $checksize.