http://qs1969.pair.com?node_id=279993


in reply to Random Imaging

That is some ugly code. It generates incorrect HTML (it has so many errors, that you should read "HTML for dummies" again), not that my fix will go through a validator...
The code is completly unmaintainable and gives the impression that you have absolutely no idea of what you are doing.
#!c:\perl\bin\perl.exe -w use strict; my @imgArray = ( { file => "./pics/blink.gif", h => 320, w => 240, title => "Blink 128 (1)", }, { file => "./pics/sum41.gif", h => 316, w => 500, title => "Sum 41", }, #and so on ... ); #dont use srand, unless you want a PREDICTABLE series of numbers every + time! my $num = rand(@imgArray); my $img = $imgArray[$num]; print "Content-type: text/html\n\n"; print <<HTML; <html> <head> <title></title> </head> <body bgcolor=lightgrey> Index: $num <br>Image: $$img{file} <br> <br> <center>Random imaging with Perl.....</center> <br> <br> <center> <img src="$$img{file}" border=1 height="$$img{h}" width="$$img{w}"> <br> <br> <font>$$img{title}</font> </center> </body> </html> HTML


T I M T O W T D I

Replies are listed 'Best First'.
Re: Re: Random Imaging
by freddo411 (Chaplain) on Aug 01, 2003 at 17:41 UTC
    I think that Cine could be making a good point here; that it is important to seperate the programatic logic from the HTML presentation, even in a small script. His example code does a good job of that. For this reason I would mod parent up ++.

    However, the tone of the his message is a bit off-putting. For this reason I would mod parent down --.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

      I've only been a member here for like 5 hrs, is mod parent up ++ and mod parent down -- some type of rating thing on here?
        Yes, that is correct. This feature shows up after you've accumulated a bit of experience. You can read about it here:

        What is reputation?

        and

        Voting/Experience System

        -------------------------------------
        Nothing is too wonderful to be true
        -- Michael Faraday

Re: Re: Random Imaging
by appex32 (Initiate) on Aug 01, 2003 at 15:40 UTC
    I don't understand how I'm generating all this horrible html code, when the only problem I seem to have is that the picture is not displayed. If you want to see what comes up go here: http://165.190.25.157/cgi-bin/image.pl

      The problem is that you are populating your IMG tags with this path:

      /htdocs/pics/{random image file}

      However, your pics are here:

      http://{your site IP}/pics/{random image file}

      Without knowing your filesystem, it's hard to say exactly where, but somewhere you are introducing "/htdocs/" to the path. My guess is the "./" part of the path, but you've said changing that has no effect.

      Off the top of my head: could this be a config issue with your server? Perhaps you are pointing at the directory above your docroot (ie htdocs) instead of the htdocs directory?

      AH

      Update: I copy/pasted your code and ran it on my machine. When I removed the "." before the "./pics" it worked. If you do that and you are still getting "/htdocs/pics" I'd say it's almost certainly a config issue.

        Do you mean the code that I originally posted or the code that you gave me? Cause I got the code that I wrote working. It turns out it was a problem with my server configuration, just a little typographical error that threw it off.
        Yeah i did figure out what the problem was, I had a stupid gramatical error in the config file of the server. Although the code i have may not be the greatest, according to some, it did work correctly once i fixed that config error. Thanks for your help.
      I don't understand how I'm generating all this horrible html code
      No that is obvious.
      when the only problem I seem to have is that the picture is not displayed
      Looks can be deceiving.
      But your problem is obvious, you dont have a directory called /pics on your webserver, yet that is the link you send to the browser! It does seem you have a /cgi-bin/pics/ but that tries to execute the image as a cgi-script, which does not go well.

      T I M T O W T D I
        I see. So you're saying that the way I have the program now it is not recognizing the image files as images but rather as cgi scripts, and to remedy that I have to do it the way that you did which is actually read the images in and place them into an array? Also, if I say that I don't understand how I'm doing something, that means I don't and its not obvious to me, no matter how obvious it is to you or anyone else.
Re: Re: Random Imaging
by appex32 (Initiate) on Aug 04, 2003 at 16:04 UTC
    When you use $$img{...}, what does using two $'s mean...does that signify that you are using an array of hashes rather than just a simple array where you would use only one $ to call a particular value? Thanks
      When you use $$img{...}, what does using two $'s mean...does that signify that you are using an array of hashes rather than just a simple array
      Yes and no. An array of hashes are really an array of references to hashes. Therefore when you fetch something from the array, you get a hash reference back.
      To actually use the hash, you then have to de-reference it. There are two syntactic ways of doing that $reference->{} or $$reference{}
      I like the latter best, mostly because its shorter and because it is coloured yellow in emacs...
      Read the perlref manpage

      T I M T O W T D I