Let's make this a little nicer:
#!/usr/bin/perl -w use strict; # you're safer with this use CGI; my $query = new CGI; print "Content-type: text/html\n\n"; my $name = $query->param('name'); my $file; { # let's be safe here with filehandles, too local *FILE; open (FILE, $name) || error("Can't find $name: $!"); my $first_line = <FILE>; chomp ($file = <FILE>); # takshaka gets bonus points for ac +tually testing it close FILE; } if ($file eq $query->param('pass')){ # more logically phrased print qq[<HTML> <frameset cols="100%,290%"> <frame src="commands.cgi?name=$name]; print qq[" name="commands"> <frame src="base.cgi?name=$name]; print qq[" name="game"> </frameset> </HTML>]; } else { error("Sorry, that was the wrong pass/name, please try again."); } sub error { my $text = shift; print qq[<HTML>$text</HTML>]; exit; }
There, that ought to work better. You did a couple of (syntactially valid, yes) things that could come back to haunt you, so I cleaned them up. First, you read the file in list context, into an array. If you just want the second line of it, we'll throw the first one away into a temporary variable and grab the second in another statement. That'll save reading the whole thing in if it's a potentially large file. (I don't particularly like the temporary variable throwaway thing here, but it's a good way to illustrate the point. @foo1 will give warnings with strict because it ought to be written $foo1 as it returns a scalar, not a list.)

Notice also the chomp statement, to remove the input record separator (\n, a newline) from $file. That'll make things match successfully. Most of the other stuff just makes it work under the strict pragma. There's also a little more error checking. Be sure to do that.

I like to wrap my open calls in blocks so I can use local on the filehandles. It doesn't matter much in small scripts, but as they have a way of growing into larger files, if you already have a filehandle named FILE active somewhere else, you'll clobber it here.

And, yes KM, there ought to be an exit at the end of the error sub, so let's redact that in. :)


In reply to RE: Re: If by chromatic
in thread If doesn't work by MAS4891

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.