Why ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix); doesn't work...

  • Thread starter Luis Angel Fdez. Fdez.
  • Start date
L

Luis Angel Fdez. Fdez.

Hi!

Why ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix); doesn't work
as I think it has?

I have:

use File::Temp qw/ :mktemp /;

and then

my $template = "baysTempFileXXXX";
my ($fh_bays, $bays_temp) = mkstemps($template, ".info") || die("Could
not create temporary file.\n");

The program doesn't dies so I guess mkstemps creates the temp file,
but...

print $bays_temp."\n";

gives me:

Use of uninitialized value in concatenation (.) or string at ./
upload.pl line 87, <IF> line 6.

And line 87 is the line above (the one with the print).

That code is basically a copy'n'paste from
http://search.cpan.org/~jhi/perl-5.8.0/lib/File/Temp.pm so I think I'm
doing something wrong but I don't know what.

Thanks in advance.

Bye!
 
A

anno4000

Luis Angel Fdez. Fdez. said:
Hi!

Why ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix); doesn't work
as I think it has?

I have:

use File::Temp qw/ :mktemp /;

and then

my $template = "baysTempFileXXXX";
my ($fh_bays, $bays_temp) = mkstemps($template, ".info") || die("Could

You have a compound problem of context and precedence here. "||" has
higher precedence than "=", so this is interpreted as

my ($fh_bays, $bays_temp) =
( mkstemps($template, ".info") || die(...));

That puts mkstemp() into scalar context, which means it returns only
the file handle and not the name. So $bays_temp remains undefined,
as you have noted.

Use the lower-precedence "or" instead of "||" and all will be well.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top