Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

CGI throws in an extra character. Why?

by Petras (Friar)
on Dec 01, 2004 at 05:35 UTC ( #411368=perlquestion: print w/ replies, xml ) Need Help??
Petras has asked for the wisdom of the Perl Monks concerning the following question:

Hi, everybody!

This is a chunk of code that is supposed to make a table of thumbnails. The form elements have a purpose--ultimately I want to be able to view a directory of images and rename them through a CGI script (untill I have a time to read Mastering Perl/TK, that is). But that's not the point.

The BIG QUESTION is, why, using Indigo Perl 5.8 and IE 6.029, why does a number one ("1") render at the bottom of the page? Where does that come from?

There is a lot of non-conforming code here. Feel free to blast my newbie-style in response, but that's not really what this post is about ;)

#! perl -wT use CGI; use CGI::Pretty; use strict; use File::Basename; #so I can use dirname(); use DirHandle; #so I can use opendir(); use strict; # eventual the path will come from an env variable from a calling webp +age my $path=dirname('c:/pictemp/start.gif'); my @files; opendir(DIR, $path); @files = readdir(DIR); # This ugly section drops the '.' and '..' from the directory array. # It looks juvenile, but it works. my $junk; if ($files[0] eq '.' ) { $junk = shift @files; } if ($files[0] eq '..' ) { $junk = shift @files; } my $query = CGI->new(); print $query->header( "text/html" ), $query->start_html(-title => "Renaming Digital Photos", -bgcolor => "#ffffcc" ); print "<form name=newNameForm method=post action=rename.cgi>\n"; print $query-> input( { -name=> "baseDir", -type=> "hidden", -value=> "$path"} ); print "\n<table border=1><tr valign=top>\n"; my $file; my $counter=0; my $tempFile; foreach $file (@files) { $tempFile=$path."/".$file; print "<td>\n\t".$file."<br>\n\t"; print $query->input ( { -type=>'hidden', -name=>"'hidden'.$counter", -value=>"$file" } ); print "\n\t"; print $query->input ( { -type=>'text', -name=>"'newName'.$counter", -size=>"30", -value=>$file } ); print "\n\t<img width=200 src=".$tempFile.">\n\t</td>\n"; if ( ++$counter % 3 == 0) { print "</tr><tr valign=top>\n"; } } print "</tr></table>"; print #query->end_html; closedir(DIR);

It displays a table, with appropriate file names, form fields, and thumbnails, but after the table an extra "1" shows up. What's going on?

Pease out,

Petras

Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

Edit by castaway - move readmore tags outside of code tags

Comment on CGI throws in an extra character. Why?
Download Code
Re: CGI throws in an extra character. Why?
by Your Mother (Canon) on Dec 01, 2004 at 05:43 UTC

    The print #query->end_html; at the end, I think. print is sucking up the true return value of the closedir because the end_html is commented out.

      Just curious, then. I really don't know how to check, so I'll just ask (ah, ignorance and laziness....). Does  closedir execute if it is part of a  print statement? print ++$someVar; would execute the incriment even though it is part of a print statement. Are there any rules as to what executes as part of another statement and what doesn't?

      Thanks,
      -P

        I'm not a guru and I hope I don't misspeak but here's an offering: print is a list operator so it's always going to wait for everything to its right, w/i its precedence space, to finish. Perl is really great for its DWIMness in the sense that statements/subs/methods often act the way you'd expect or hope just from looking at them. The rules for that stuff is mostly just precedence (how strongly an operator or function wants to grab and act upon what's around it).

        Check out perlop for precedence info in all its gl?ory.

Re: CGI throws in an extra character. Why?
by davido (Archbishop) on Dec 01, 2004 at 05:43 UTC

    At the bottom of your script, here's your problem:

    print #query->end_html;

    That should be...

    print $query->end_html;

    What's happening is that # begins a comment, which means that you're calling print with no arguments, which means it defaults to printing the contents of $_, which happen to be (in this case), the number '1'.


    Dave

      Hey, that's right on the money. Not very profound, but right on the money. Thank you very much!
        So, using a dollar sign is right on the money? :)
      you're calling print with no arguments, which means it defaults to printing the contents of $_, which happen to be (in this case), the number '1'.

      No, that's not right. Notice the ; is commented out too, so the statement does not end on that line. It's actually what Your Mother said -- the return value from closedir.

        You're right. That darn # character is still to blame though. ;)


        Dave

Re: CGI throws in an extra character. Why?
by Anonymous Monk on Dec 02, 2004 at 13:12 UTC
    B::Deparse would've answered your question. This is how perl sees your program:
    BEGIN { $^W = 1; } use CGI; use CGI::Pretty; use File::Basename; use DirHandle; use strict 'refs'; my $path = dirname('c:/pictemp/start.gif'); my @files; opendir DIR, $path; @files = readdir DIR; my $junk; if ($files[0] eq '.') { $junk = shift @files; } if ($files[0] eq '..') { $junk = shift @files; } my $query = 'CGI'->new; print $query->header('text/html'), $query->start_html(-'title', 'Renam +ing Digital Photos', -'bgcolor', '#ffffcc'); print "<form name=newNameForm method=post action=rename.cgi>\n"; print $query->input({-'name', 'baseDir', -'type', 'hidden', -'value', +"$path"}); print "\n<table border=1><tr valign=top>\n"; my $file; my $counter = 0; my $tempFile; foreach $file (@files) { $tempFile = $path . '/' . $file; print "<td>\n\t" . $file . "<br>\n\t"; print $query->input({-'type', 'hidden', -'name', "'hidden'.$counte +r", -'value', "$file"}); print "\n\t"; print $query->input({-'type', 'text', -'name', "'newName'.$counter +", -'size', '30', -'value', $file}); print "\n\t<img width=200 src=" . $tempFile . ">\n\t</td>\n"; if (++$counter % 3 == 0) { print "</tr><tr valign=top>\n"; } } print '</tr></table>'; print closedir DIR;
    print closedir DIR; will print 1 if closedir returns true.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://411368]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-23 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls