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

I have a file called whatever.pl In this file there is the following subroutine:
sub banner_rotator { $banner = ""; $link_image = "1"; # 1 = YES; 0 = NO srand(time ^ $$); $num = rand(@images); # Pick a Random Number # Print Out Random Filename and Base Directory if ($link_image eq '1' && $urls[$num] ne "") { $banner .= "<a href=\"$urls[$num]\" target=\"_blank\">"; } $banner .= "<img src=\"$basedir$images[$num]\""; if ($border ne "") { $banner .= " border=$border"; } if ($align ne "") { $banner .= " align=$align"; } if ($alt[$num] ne "") { $banner .= " alt=\"$alt[$num]\""; } $banner .= ">"; if ($link_image eq '1' && $urls[$num] ne "") { $banner .= "</a>"; } $banner .= "\n"; }
I am trying to display a random banner in various html generated pages using ssi. Is there anyway to get this subroutine to display an image in my html generated pages using ssi, so that I don't have to using two separate banner rotators?! I am a newbie :)

Replies are listed 'Best First'.
Re: Calling a subroutine and dispalying an image using ssi
by tachyon (Chancellor) on Jul 07, 2001 at 23:26 UTC

    This is really two questions. First how do I use server side includes to execute a script and second how do I write a cgi script to select/display a random image within the constraints enforced by SSI.

    To use a SSI to execute a CGI script you just embed a link like this in your HTML:

    <!--#include virtual="/cgi-bin/myscript.cgi"-->

    Of course support for SSI will need to be enabled on the server for this to work, but provided SSI is enabled for this HTML doc the output of the script 'myscript.cgi' will be embedded into the HTML.

    The output of this CGI script may only be text. It can not be a graphic image or other binary data. Thus we can't output the image directly but fortunately we dont need to. All we need to do is output a complete image tag that points to the desired image. This image tag will get included by the server and thus the image will get displayed by the browser.

    Here is some code for a very simple script to rotate five banners:

    #!/usr/bin/perl -w use strict; my @banner = ( '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>'); # seed random generator and select an image from array srand (time()^($$+($$<<15))); my $image = int rand @banner; # print our header and random image link for SSI print "Content-type: text/html\n\n"; print $banner[$image]; exit;

    You could of course generate the image tags dynamically by reading a directory but hard coding them is much faster and as you will call this banner script for every page you want it to be fast. I have not included the A HREF link tags you will want for brevity.

    Hope this helps

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      I think you got what I was trying to say... I have a program uses the banner_rotator subroutine and displays the banner in a header.txt file as $banner. I am using a banner rotater right now that uses img tags OR ssi to display a random banner. I use this on all of my .htm files outside of my cgi-bin. I wan't to use the banner_rotator from my whatever.pl file so that I don't have to update two scripts when I add a new banner.
        You are asking how you can put the code in one file, then reference it from multiple ssi scripts? Look up require. Hard-code the full absolute path to begin with, since relative paths in cgi are another challange.

        —John

Re: Calling a subroutine and dispalying an image using ssi
by tachyon (Chancellor) on Jul 08, 2001 at 04:03 UTC

    Oh OK barking up the wrong tree. Yes it's easy make your script that contains the sub banner_rotator a module. A module is simply perl's version of a library and allows you to call code in one script from another. To make the script with this sub a module follow these easy steps.

    1 Add this code at the top of the script after the shebang line:

    #!/usr/bin/perl
    
    package Mysubs; # this allows us to identify this package
    

    You can pick any name you want but usually you would use the same name as the script.

    2 Change the name of the script to say Mysubs.pm. The name does not matter but the script needs to end with a .pm suffix. You can use a sym link to ensure that this does not break anything to link the old and new names. Alternatively you can use a 'require Mysubs.pl' as noted below if you don't want to change the name/use a symlink.

    3 Finally a module must return true. To make sure your new module returns true place this at the end of it:

    1;
    

    Yup, that's it, just add a 1.

    OK you are now the proud owner of a perl module! Now to use the banner_rotator sub from your SSI script all you need to do is this:

    #!/usr/bin/perl # you may need to do this: BEGIN{push @INC, "/path/to/mysubs/module"} # so that Perl can find your module when you do this: use Mysubs; # you can now call banner_rotator sub (or any sub) like this: &Mysubs::banner_rotator;

    Notes @INC tells Perl where to look for modules - it is effectively Perl's PATH variable for module lookups. To save explaining about where/why/how perl will look just tell it as shown! The use Mysubs; tells perl to use the first perl script called 'Mysubs.pm' it finds when searching the directories in @INC. It is also possible to require 'Mysubs.pl'; and avoid the name change to a .pm suffix if you want. The &Mysubs:: part before banner_rotator tells perl to look for this sub in the Mysubs package (it is easiest to call the module and package to same to save confusion here).

    Hope this helps. Let me know if you have any problems. If you get a login you can /msg me directly :-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print