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

I'm looking for some guidance - I've been trying to get this done all afternoon and the script just keeps aborting! I am putting a different comment into each of a group of html files so I can send each one to a different sub routine. I tried a few things -here's one example: (the labels are stuff like  <!--a -->, <!--t1 -->, etc)
sub routine{ $prepage = "/path/to/page"; print "Content-type: text/html\n\n"; #open file: open (PAGE, "$prepage") || die "Couldn't get prepage ched"; @prep=<PAGE>; close (PAGE); $tag =~ m/<!--..? -->/; if ($tag eq '<!--a -->') { &suba; exit;} if ($tag eq '<!--b -->') { &subb; exit;} }
The script works fine without this sub routine so I'm messing up big time - but where? Any light that might be shed would be most welcome. Thanks

Replies are listed 'Best First'.
Re: simple search problem (but not for me!!)
by PrakashK (Pilgrim) on Oct 11, 2002 at 17:22 UTC
    You are reading the file into @prep, but are not really searching @prep for the pattern.

    $tag =~ m/<!--..? -->/;
    This line searches for the pattern in the variable $tag, which may or may not have been defined (considering it is a global, not a lexically scoped, variable). Consider the following instead:
    $page = join "", @prep; # since the lines are in an array if (my ($tag) = $page =~ m/<!--(.+) -->/) { # call the appropriate sub based on the tag if ($tag eq 'a') { suba; exit; } elsif ($tag eq 'b') { subb; exit; } # ...etc... }
    I have not attempted above to change your algorithm or data structure, although I would use a hash containing references to the subroutines, like:
    my $subs = { a => \&suba, b => \&subb, # etc. } if ($subs->{$tag}) { $subs->{$tag}->(); } else { warn "Unknown tag <$tag> in file <$prepage>\n"; }

    Also, I would not read the whole file at once, but instead:

    my $tag; while (<PAGE>) { if (m/<!--(.+) -->/) { $tag = $1; last; # assuming there is only one tag in each file } } close <PAGE>; # proceed to call the tag handler
    HTH,
    /prakash
Re: simple search problem (but not for me!!)
by lestrrat (Deacon) on Oct 11, 2002 at 17:06 UTC

    So basically, when you encounter a special comment tag in your html file, you jump to a subroutine, eh? Sounds kinda bizarre, but anyway...

    sub foo { # ....initialization stuff...., blah blah while( my $line = <SOURCE> ) { if( $line =~ /<!--a -->/ ) { # could do index(), if looking f +or exact match like this sub_a(); last; } elsif( $line =~ /<!--b -->/ ) { # ditto sub_b(); last; } } }
      Thanks very much for your reply. I've been trying to apply your solution to my script though without success. I turned it into a standalone script but it still keeps failing and I can't track down the reason why (I've checked for syntax errors etc and it comes out ok) so I'm obviously off base with mu application still. What I have is:
      #!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; $prepage = "/path/to/page"; print "Content-type: text/html\n\n"; #open file: open (PAGE, "$prepage") || die "Couldn't get prepage ched"; @prep=<PAGE>; while( my $line = <PAGE> ) { if( $line =~ /<!--a -->/ ) { # could do index(), if looking for exact match like this suba(); last; } elsif( $line =~ /<!--b -->/ ) { # ditto subb(); last; } } close (PAGE); sub suba{ print "suba says hi!"; } sub subb{ print "subb says hi!"; }
      So what am I doing wrong now?!!

        In the first line after your open, you do @prep=<PAGE>;. This reads your whole file into the array @perp. You then completely ignore this array and attempt to process the file line by line with while( my $line = <PAGE> ) { but you have already read the whole file, so there is nothing left to read. Try commenting out (or simply deleting) that first line after the open and see what difference that makes.

        open (PAGE, "$prepage") || die "Couldn't get prepage ched"; @prep=<PAGE>; # <<<< Why???? while( my $line = <PAGE> ) {

        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: simple search problem (but not for me!!)
by Zaxo (Archbishop) on Oct 11, 2002 at 17:06 UTC

    You have neglected to loop through @prep to actually check the file's content. You will need to modify your $tag binding and comparison to an assignment in list context.

    I don't see exactly what you're doing with this, but would one of the Template modules help?

    After Compline,
    Zaxo