Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Creating temporary files without File::Temp

by c (Hermit)
on Feb 28, 2002 at 14:51 UTC ( [id://148225]=perlquestion: print w/replies, xml ) Need Help??

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

I just read through this post on Tilly's advice on using File::Temp and it looks like just the recommendation I need. Unfortunately, my company doesnt stock File::Temp as a module, and the systems group is somewhat reluctant to field module requests from one of the network guys.
I read through IO::File's description on CPAN and found the new_tmpfile however it left me somewhat confused. My goal is to write to a completely random file and then attach it to an outbound email using Mail::Sender. For example...

## create message with attachment $sender->MailFile({ to => "$username\@mycompany.inc", subject => "$subject", msg => "Attached is the info you need!", file => "$tmpfile", });

Try as I might using IO::File, attaching the tmp file is just not happening. Maybe I am trying to use the temporary file for purposes that it's really not meant for?

Perhaps its easier to state the reason I need the tmpfile. My script will parse several configuration files and return the information to user as a CSV file attached to an email. I'd like to write to a completely randomly named file so that a devious individual won't try any of the tricks of symlinking a known output file's name to some other file/area on the machine in the hopes of causing trouble. Make sense?

Any help is appreciated. Humbly -c

ps. thanks again ybiC for that original post. i find most of my answers simply by perusing your writeups...

Replies are listed 'Best First'.
Re: Creating temporary files without File::Temp
by Sinister (Friar) on Feb 28, 2002 at 15:03 UTC
    I usualy use POSIX::tmpnam for this. It returns a still available file name for the /tmp dir. (eg: /tmp/fileErjARC). example code:
    use POSIX qw(tmpnam); use strict; # || loose XP. use Sender; # ? my $tmpfile = tmpnam(); open(OUT, ">$tmpfile") || die("Can't write tmp-file"); print OUT "data ... data ... data\n"; close(OUT); my $sender = Sender new; ## create message with attachment $sender->MailFile({ to => "$username\@mycompany.inc", subject => "$subject", msg => "Attached is the info you need!", file => "$tmpfile", }); unlink $tmpfile;

    I guess this is what you meant?

    Sinister greetings.
    "With tying hashes you can do everything God and Larry have forbidden" -- Johan Vromans - YAPC::Europe 2001
    perldoc -q $_
Re: Creating temporary files without File::Temp
by hatter (Pilgrim) on Feb 28, 2002 at 15:42 UTC
    If the normal module options aren't available (or when you're using a language that doesn't have such things easily available) it's fairly common to use $CTIME.$PID as a unique filename. It's unique as long as you're not going through 65,000 processes a second, and fairly difficult to guess what the name will be on a normal multi-user system.

    the hatter
Re: Creating temporary files without File::Temp
by rob_au (Abbot) on Mar 01, 2002 at 02:42 UTC
    Although I believe perrin has answered your immediate problem already in this thread, I think that it may be worthwhile to direct your attention to this node on using temporary files in Perl written by myself previously. This discussion highlights a number of issues with regards to using temporary files in general and how to implement secure usage of temporary files using the POSIX::tmpnam or IO::File::new_tmpfile functions.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Creating temporary files without File::Temp
by perrin (Chancellor) on Feb 28, 2002 at 15:27 UTC
    What exactly is not working when you try to do this? Maybe you just have a mismatch in your subroutine args or something.
      well, when using IO::File, i tend to get temp files created with a value of IO::File=GLOB(0x80cb9bc)

      perhaps it is incorrect code? -c

        It's giving you a reference to an IO::File object. If you want the file name or a file handle, you can get it from that object.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-18 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found