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

Closing a system process after file creation (was: One for the Wisest Monks)

by Anonymous Monk
on Jun 02, 2002 at 11:02 UTC ( #171007=perlquestion: print w/replies, xml ) Need Help??

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

I am using a one up counter from a file to make ID keys for a db file. I have found a way to dynamically create a .dat file using this one up counter so I can have a log seperate from the database that coincides. I do this with the following command:

system "cat > $num.bat"; #(creates 19.bat if the one up counter is at 19)

now the question....how can I add a command in this string that will not only create the new file but close it automatically so the process dies?

update by boo_radley : title change
  • Comment on Closing a system process after file creation (was: One for the Wisest Monks)

Replies are listed 'Best First'.
Re: One for the Wisest Monks
by Zaxo (Archbishop) on Jun 02, 2002 at 11:14 UTC

    Your system call is waiting for input from stdin. You could call 'touch' instead, but how about a pure perl solution?

    { my $fh; open $fh, '>', "/path/to/${num}.bat" or die $!; }
    You are likely to want a more economical representation of your counter, sooner or later. You should think about flock when you design it.

    After Compline,
    Zaxo

      I've tried this..or a reasonable facimile...I'll try again when I go back to work...perhaps I was making a permissions oversight.

      This command will create a file called $(num).bat? in the directory listed in the path?

      Sure hope it works!! I'll let ya know tomorrow!

      Thanks!
Re: One for the Wisest Monks
by Matts (Deacon) on Jun 02, 2002 at 12:03 UTC
    If you really want to use system commands and not straight perl, try:
    system "touch $num.bat";
    Has the advantage of not doing anything to the file (except changing it's timestamp) if it already exists.
      system "touch $num.bat";
      But that unnecessarily starts a separate process, and is subject to security issues regarding the contents of $num.

      Much safer and faster:

      open DUMMY, ">>$num.bat"; close DUMMY;

      -- Randal L. Schwartz, Perl hacker

        system touch => "$num.bat";
        doesn't suffer from security issues regarding the content of $num. As for "much faster", I'm not quite convinced. You're doing disk I/O here, and that's likely to be the bottleneck anyway.

        Abigail

      I like it!
        Have it implemented and it's working GREAT! Thanks again!

        TexasTess
        "Great Spirits Often Encounter Violent Opposition From Mediocre Minds" --Albert Einstein
Re: One for the Wisest Monks
by TexasTess (Beadle) on Jun 02, 2002 at 11:25 UTC
    BTW didn't mean for this to be anonymous....forgot to log in
      Just a hint: change the site scheme in your User settings - then it is immediately visible when you're logged in and when not. :-)
      ____________

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171007]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-12-09 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (37 votes). Check out past polls.

    Notices?