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

Using following functions in scriptA to create temp config file:
my $tmpParamFileName; my $tmpParamFileHandle; sub getTempParamFile(){ if(!defined $tmpParamFileHandle){ ($tmpParamFileHandle, $tmpParamFileName)=tempfile(UNLINK=>1); } return $tmpParamFileHandle; } sub getTempParamFileName(){ if(!defined $tmpParamFileName){ ($tmpParamFileHandle, $tmpParamFileName)=tempfile(UNLINK=>1); } return $tmpParamFileName; }
When I pass getTempParamFileName() as a parameter to ScriptB, it says <temp file> file not found.
<Temp file> is only available for ScriptA (from where it was created)?

2006-08-01 Retitled by GrandFather, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Using File::Temp Names
by Sidhekin (Priest) on Aug 01, 2006 at 03:22 UTC

    How do you call ScriptB? Or should I say, when do you call ScriptB?

    Files created with File::Temp may get removed as soon as the File::Temp object (file handle) goes out of scope (at least as long as you use, as you do, UNLINK=>1). So, are you sure the file handle is still live in scriptA when ScriptB tries to open the file by name?

    Update: Rereading your code, I note that the functions are closures, so unless you have code to explicitly undef or assign to $tmpParamFileHandle, they're likely to keep the file handle alive. Until global cleanup, that is. So, I guess the question is: Are you sure ScriptB is trying to open this file before scriptA exits?

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      Yes, file handle is still alive in ScriptA. To check I browsed the file using scripts shown below, before and after passing it to ScriptB.
      print "\n\n\n"; print "\n".getTempParamFileName()."\n"; open (AAA, "<".getTempParamFileName()); while(my $xxx=<AAA>){ print $xxx; } close AAA; ######### my @pmcmd=("$binDir/runWorkflow.pl", "-c", "$cluster", "-n", "$node", "-f", getWorkFlowFolder($feeder), "-p", getTempParamFileName(), + "-w", getWorkFlow($feeder)); $rc = PB::FileUtils::execute(\@pmcmd, $log); ######### print "\n\n\n"; print "\n".getTempParamFileName()."\n"; open (AAA, "<".getTempParamFileName()); while(my $xxx=<AAA>){ print $xxx; } close AAA;

        What's PB::FileUtils? Even google does not seem to know ...

        Does it execute runWorkflow.pl (ScriptB?) immediately and synchronously, on the same machine, under the same chroot, and with the same privileges (user/group)?

        Or, perhaps more easily answered, if you just run it with system {@pmcmd[0]} @pmcmd, does it see the file?

        print "Just another Perl ${\(trickster and hacker)},"
        The Sidhekin proves Sidhe did it!

Re: Using File::Temp Names
by bobf (Monsignor) on Aug 01, 2006 at 03:08 UTC

    What do you mean by "pass getTempParamFileName() as a parameter to ScriptB"? If you're exporting the function, the most likely reason tempfile is not found is because you need to use File::Temp.

      I mean, I pass the file name I get it from getTempParamFileName() to ScriptB. Debugging does says filename being passed is /tmp/efeRsfSd ... but ScriptB can not see file /tmp/efeRsfSd and gives error message file /tmp/efeRsfSd not found.
Re: Using File::Temp Names
by shmem (Chancellor) on Aug 01, 2006 at 09:46 UTC
    Ok, so this is in ScriptA. What is in ScriptB?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}