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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to alternate between STDIN and input file based on wkg dir.

Status
Not open for further replies.

CyBorg1958

Programmer
Feb 14, 2008
5
SE
Hi!
My script is triggered by the formatting process in our billing system, and when that happens it wants to use STDIN and -OUT as the in- and outdata. When I test the script I run it in another folder, from the command line : perl scriptname.pl. What I want to do is have the script know where it is, and use the appropriate in- and outdata streams accordingly: Regular files in the testing location, standard in/out in production. Running it from the command line in the prod folder it works fine, but when triggered from the billing process it won't. This is what it looks like:

use Cwd;
my $WorkingDirectory = getcwd();

my $in_file = $WorkingDirectory.'/INDATA.GMF';
my $out_file = $WorkingDirectory.'/UTDATA.GMF';
my $log_file = $WorkingDirectory.'/PreProcessBillTags.log';

open (LOGFILE, "+>$log_file") or die "can't open $log_file $!";


if ($WorkingDirectory =~ /customscripts$/i)
{
open (BILLFILE, "-") or die "Cannot read from standard input BILLFILE";
open (BILLOUTPUT, ">-") or die "Cannot write to standard output BILLOUTPUT";
}
else
{
open (BILLFILE, "$in_file") or die "Cannot read from input file $in_file";
open (BILLOUTPUT, "+>$out_file") or die "Cannot write to output file $out_file";
}
 
You didn't tell us the error it fails on. Is it a basic permissions issue?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Don't rely on the current working directory. It's messy and unreliable depending on the calling method. Instead make the working directory a parameter to the script. That way you can be certain that it's using the correct place.

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

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$workdir[/blue] = [blue]$ARGV[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red] || [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Usage: scriptname.pl <workingdir>[/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$log_file[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$workdir[/blue], [red]'[/red][purple]PreProcessBillTags.log[/purple][red]'[/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$in_file[/blue]  = [maroon]catfile[/maroon][red]([/red][blue]$workdir[/blue], [red]'[/red][purple]INDATA.GMF[/purple][red]'[/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$out_file[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$workdir[/blue], [red]'[/red][purple]UTDATA.GMF[/purple][red]'[/red][red])[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red][blue]$WorkingDirectory[/blue] =~ [red]/[/red][purple]customscripts$[/purple][red]/[/red][red]i[/red][red])[/red] [red]{[/red]
	[blue]$in_file[/blue] = [red]'[/red][purple]-[/purple][red]'[/red][red];[/red]
	[blue]$outfile[/blue] = [red]'[/red][purple]-[/purple][red]'[/red][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh_log[/blue], [red]'[/red][purple]+>[/purple][red]'[/red], [blue]$log_file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$log_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red][black][b]my[/b][/black] [blue]$fh_billin[/blue], [blue]$in_file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$in_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red][black][b]my[/b][/black] [blue]$fh_billout[/blue], [red]'[/red][purple]+>[/purple][red]'[/red], [blue]$out_file[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$out_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red]"[/red][purple];[/purple]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Hi & thanx for the tips!

However... now the script dies on me at

my $workdir = $ARGV[0] || die "Usage: scriptname.pl <workingdir>";

I ran the script with -W, got no warnings, it just died...

Rgd's
Cy


 
... as a matter of fact, if I run the script from the command line in the production folder ('customscripts'), the file names are correctly displayed as '-', as per the logfile. Still, the calling program exits with an error since it can't find INDATA.GMF... It's weird...
 
Are you sure the file is in the directory and the script has permissions to access it? Is the filename all caps?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, in production this script is using STDIN and STDOUT - it accepts a stream from another application which calls it. In test, it's more convenient to execute it from the command line using 'regular' files as in and out. The idea is to have the script itself realize that it's being executed in the production folder (/customscripts) and open '-' instead of the file names I only want in test. Since the calling program can't possibly read the script itself, the reference to INDATA.GMF must be coming from inside the script.
Yes, the filename is all caps, but the script does exactly what i want when run from the command line - inside the /customscripts directory the log shows it uses '-' as in and out, and in any other directory it uses INDATA.GMF and OUTDATA.GMF. Could it be that when it's being called by a process, the script ISN'T executed in the directory where it resides...?
 
OK - I solved it just by blabbing... :)

I simply turned the conditional around: If we're NOT in /tmp (my test directory), use STDIN/OUT, else use files. The log now shows this:

############################################################
Script source: /export/home/geneva/PreProcessBillTags ############################################################ Files in: StandardIN, Files out: StandardOUT
############################################################

So it turns out that the 'working directory' for a script that's called from within an executing process is the executing user's home directory... i.e. 'geneva' in this case.

Thanks for the suggestions y'all made - it made me try a couple of extra twists!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top