Gossamer Forum
Home : General : Perl Programming :

PLEASE HELP: External subroutines + open = $!

Quote Reply
PLEASE HELP: External subroutines + open = $!
Ok, I've been struggling with this for days, now...

Problem: I have a CGI script that is getting too long, and I would like to separate the code into files for further expansion and options, however upon doing so the "open" and "opendir" commands in my script have ceased to execute.

Everything works fine except this, but this is a MAJOR problem, since the
script is mainly for data modification (stored in a text file).

Here's an example...

file 1:
Code:
#!/usr/bin/perl -w
use CGI qw(:all);

require "file2.pl"; // see below

print header;
print "<html><body>";
&file2sub(); # execute the external subroutine
print "<br>end";
print "</body></html>";

file2.pl:
Code:
sub file2sub() {
open(NEWFILE, "file.txt") || print "error accessing /data/file.txt";
print <NEWFILE>;
close(NEWFILE);
};

1;

file.txt:
Code:
success! the file was read by the external subroutine!<br>

If anyone can figure this out, or knows what I'm doing wrong: please, please let me know!

It would be greatly appreciated.
Thanks,
Jeff
Quote Reply
Re: [jeffx] PLEASE HELP: External subroutines + open = $! In reply to
Quote:
success! the file was read by the external subroutine!<br>

I would use something like;

Code:
sub file2sub {

open(NEWFILE, $_[0]) || die "error accessing /data/". $_[0] . ". Reason: $!";
print <NEWFILE>;
close(NEWFILE);

}

... and call it with;

Quote:
&file2sub('file.txt');

This way, you can use the same routine over as many times as you want, to show however many files you want :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!