Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open and pass info to another script

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
What is the best/proper way to have one script run another and pass neede information to it?

I have a cgi web page that can be used to request a report. Certain options can make it take a long time to generate the report and the web page will timeout and kill the script.

So what I did is I created a separate script that can generate the report an email it to the user if those options are selected. Now I just need to get the cgi script to invoke the other one and pass it some needed information, so of which is in hashes.

The low-tech plan I have at the moment is to have the cgi script write all the needed information to a uniquely named text file and then run the pl script from a system command with the name of that file. The pl script would then open that file and read the contents back into it's own variables and carry on.

I'm just wondering if there is a better way to do it.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
well you could just pass all information to it in ARGV instead of putting it in a file. You could also just fork the process off.

One thing you can do is write all your information out to a file/DB and have a cron'd script that reads it all in and does something with it every X amount of minutes.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
The biggest thing that you should do is turn the report into an object. You set the configuration of that object but do not actually generate the report.

You then create a method in that class that queues the report by freezing it and saving it to either the file system or to a database record. Something like the following would work (not tested):

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Data::Dumper[/green][red];[/red]
[black][b]use[/b][/black] [green]IO::File[/green][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]constant[/green] [purple]QUEUEDIR[/purple] => [red]'[/red][purple]/whereever/you/want/me[/purple][red]'[/red][red];[/red]

[gray][i]# ...[/i][/gray]
[gray][i]# class definitions[/i][/gray]
[gray][i]# ...[/i][/gray]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]queue[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$self[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red];[/red]

	[gray][i]# Verify Queue Directory[/i][/gray]
	[olive][b]if[/b][/olive] [red]([/red]! [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] QUEUEDIR || ! [url=http://perldoc.perl.org/functions/-X.html][black][b]-w[/b][/black][/url] QUEUEDIR[red])[/red] [red]{[/red]
		[blue]$![/blue] = [red]"[/red][purple]Can't write to [/purple][red]"[/red] . QUEUEDIR[red];[/red]
		[url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url][red];[/red]
	[red]}[/red]

	[gray][i]# Create Unique Filename[/i][/gray]
	[black][b]my[/b][/black] [blue]$fileprefix[/blue] = [maroon]strftime[/maroon][red]([/red][red]"[/red][purple]%Y_%m_%d_%H%M[/purple][red]"[/red], [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red])[/red][red];[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$filename[/blue], [blue]$fh[/blue][red])[/red][red];[/red]
	[black][b]my[/b][/black] [blue]$cntr[/blue] = [fuchsia]1[/fuchsia][red];[/red]
	[url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red]
		[blue]$filename[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple][blue]$fileprefix[/blue].%05d.report[/purple][red]"[/red], [blue]$cntr[/blue]++[red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][blue]$cntr[/blue] == [fuchsia]100_000[/fuchsia][red])[/red] [red]{[/red]
			[blue]$![/blue] = [red]"[/red][purple]Can't create [/purple][red]"[/red] . QUEUEDIR . [red]"[/red][purple]/[blue]$filename[/blue][/purple][red]"[/red][red];[/red]
			[black][b]return[/b][/black][red];[/red]
		[red]}[/red]
	[red]}[/red] [olive][b]until[/b][/olive] [red]([/red][blue]$fh[/blue] = IO::File->[maroon]new[/maroon][red]([/red]QUEUEDIR . [red]'[/red][purple]/[/purple][red]'[/red] . [blue]$file[/blue], O_RDWR|O_CREAT|O_EXCL[red])[/red][red])[/red][red];[/red]
	
	[gray][i]# Dump Data[/i][/gray]
	[url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$Data::Dumper::Terse[/blue] = [fuchsia]1[/fuchsia][red];[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$fh[/blue] [maroon]Dumper[/maroon][red]([/red][blue]$self[/blue][red])[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]$fh[/blue][red];[/red]
	
	[black][b]return[/b][/black] [fuchsia]1[/fuchsia][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]constant - Perl pragma to declare constants[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Data::Dumper - stringified perl data structures, suitable for both printing and eval[/li]
[li]IO::File - supply object methods for filehandles[/li]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

All you would need to do is simply call the queue function from whereever you created the report object.

Code:
[blue]$report[/blue]->[maroon]queue[/maroon][red]([/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't queue report: [blue]$![/blue][/purple][red]"[/red][red];[/red]

You then create a script that can be either spawned on demand or is run as a daemon. The method doesn't matter too much. However, whatever method that you use will simply load the reports and then generate the results. Something like the following would roughly work (not tested).

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Slurp[/green] [red]qw([/red][purple]read_file[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]constant[/green] [purple]QUEUEDIR[/purple] => [red]'[/red][purple]/whereever/you/want/me[/purple][red]'[/red][red];[/red]
[black][b]use[/b][/black] [green]constant[/green] [purple]REST[/purple] => [fuchsia]10[/fuchsia][red];[/red] [gray][i]# In seconds;[/i][/gray]

[olive][b]for[/b][/olive] [red]([/red][red];[/red][red];[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, QUEUEDIR[red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [/purple][red]"[/red] . QUEUEDIR . [red]"[/red][purple]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@files[/blue] = [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
	
	[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [blue]@files[/blue][red])[/red] [red]{[/red]
		[olive][b]next[/b][/olive] [olive][b]unless[/b][/olive] [blue]$file[/blue] =~ [red]/[/red][purple]^[purple][b]\d[/b][/purple]{4}_[purple][b]\d[/b][/purple]{2}_[purple][b]\d[/b][/purple]{2}_[purple][b]\d[/b][/purple]{4}.[purple][b]\d[/b][/purple]+.report$[/purple][red]/[/red][red];[/red]
		
		[black][b]my[/b][/black] [blue]$data[/blue] = [maroon]read_file[/maroon][red]([/red]QUEUEDIR . [red]'[/red][purple]/[/purple][red]'[/red] . [blue]$file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
		[black][b]my[/b][/black] [blue]$report[/blue] = [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [blue]$data[/blue][red];[/red]
		[blue]$report[/blue]->[maroon]generate[/maroon][red];[/red]

		[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url][red]([/red]QUEUEDIR . [red]'[/red][purple]/[/purple][red]'[/red] . [blue]$file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't delete [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]
	
	[url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red]REST[red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]constant - Perl pragma to declare constants[/li]
[li]constant - Perl pragma to declare constants[/li]
[/ul]
Other Modules used :
[ul]
[li]File::Slurp[/li]
[/ul]
[/tt]

Anyway, as I hope that you can see, the biggest choice to make is to turn your report into an object. This enables you to freeze and thaw it at will and to delay the actual generation until whatever point you desire. This method could have just as easily applied to saving to a database record instead. Or via socket communication. Since the report is encapsulated in an object, you can even add logic to that class that would determine whether it was worthwhile to delay the generation, or if it is simply enough to go ahead and generate on the fly. The point is these decisions are made and changed easily if all that is required is a simply call to $report->generate or $report->queue.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top