Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number


by stonecolddevin (Vicar)
on Jun 12, 2003 at 00:25 UTC ( #265232=snippet: print w/replies, xml ) Need Help??
Description: My version of CGI::SSI's include function
sub include {
        use Fcntl qw{ :flock }; 
        my $f_read = $_[0];
        # Read in original book data
          open DATA, "<$f_read" or die ("Could read open $f_read. Reas
+on: $!");
               #flock (DATA, LOCK_EX) or die ("Can't get an exclusive 
+lock on $f_read: $!");
            read (DATA, my $data, -s DATA);
          close DATA;
          my @data_arr = $data;
          return @data_arr;
Replies are listed 'Best First'.
Re: MyInclude
by simon.proctor (Vicar) on Jun 12, 2003 at 08:18 UTC
    You don't mention why you wanted to re-invent the wheel here. What purpose was there for this? Plus it doesn't look like it would even pass as a drop in replacement for the include method within the CGI::SSI module.

    I also wondered why you went to the trouble of using read only to create an array with the following line:
    my @data_arr = $data;
    Sorry but that doesn't look right (to my early morning eyes anyway :P ) surely it would have been better to:
    my @data_arr = <DATA>;
    In this instance?

    On a different note, you include the the flock call via Fcntl but then you have the flock call commented out. I think you need to clean that up a little bit.

      Good points, simon.proctor. I guess I used my @data_arr = $data; because I had used it in writing a guestbook and it had worked pretty well. I commented out the flock call because it isn't supported on my winblows box, and I suppose I forgot to un-comment it. As far as my desire to re-invent the wheel, I wanted to provide a quick access function snippet for people not using CGI::SSI, for people like me who haven't had a chance to install it.
      Well, that's my "defense". I'm sure it doesn't hold a candle to CGI::SSI's version, but it was quick, and that's what I wanted. Thanks for the pointers, I'll fix it next version.
      Hi, my name is Dhoss, I like to play Quake 3 arena, I drink Vanilla Coke, and I like long walks on the beach while the sun goes down.
        I use this kind of code in a sort of an html-template way: I first edit my html file with wysiwyg FrontPage.

        Then add some comments with some special character, to later replace with a dessired chunk of values processed by Perl.

        Then, at the script, I load the html file in a scalar and s/usbstitute the established comments with the fresh chunk of data processed with the script.

        I don't need any module to do all these!

        (Note: I am first trying to manage without the use of modules so as to learn the basics and common scripting habbits.

        Later, when needing to speed up code creation, I am going to study some basic code implementation:)

(jeffa) Re: MyInclude
by jeffa (Bishop) on Jun 13, 2003 at 13:15 UTC
    Using read() to slurp up an entire file is kinda silly. read() is more useful when reading in a very large file in small chunks. Since you are reading in the entire file, storing the contents into RAM, you might as well just slurp the file into a scalar. If this is all that your code is doing ... converting the contents of a file to an array then shouldn't it be named file2array or similar? Also, die()ing in the middle of a subroutine like that is not nice to the client who uses your code.

    Here is slightly different version that returns a scalar instead of an array.

    sub file2scalar { my $filename = shift; return "Error: $filename doesn't exist" unless -f $filename; open FH, $filename || return "Error: can't read $filename"; return do{local $/; <FH>}; }
    When you call this sub:
    my $include = file2scalar('foo.html');
    $include will either contain the contents of the file or an error message. Instead of differentiating between the two, i recommend just printing $include to the browers, much like PHP does when you give it's include() function a file that doesn't exist or can't be read. (of course, my real recommendation is to just go ahead and install CGI::SSI instead)


    (the triplet paradiddle with high-hat)
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://265232]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-06-18 08:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.