@@ -307,6 +307,7 @@ my %FILES_CREATED_BY_OBJECT;
307307# use of the O_TEMPORARY flag to sysopen.
308308# Usually irrelevant on unix
309309# "use_exlock" => Indicates that O_EXLOCK should be used. Default is false.
310+ # "file_permissions" => file permissions for sysopen(). Default is 0600.
310311
311312# Optionally a reference to a scalar can be passed into the function
312313# On error this will be used to store the reason for the error
@@ -339,12 +340,13 @@ sub _gettemp {
339340
340341 # Default options
341342 my %options = (
342- " open" => 0,
343- " mkdir" => 0,
344- " suffixlen" => 0,
345- " unlink_on_close" => 0,
346- " use_exlock" => 0,
347- " ErrStr" => \$tempErrStr ,
343+ " open" => 0,
344+ " mkdir" => 0,
345+ " suffixlen" => 0,
346+ " unlink_on_close" => 0,
347+ " use_exlock" => 0,
348+ " ErrStr" => \$tempErrStr ,
349+ " file_permissions" => undef ,
348350 );
349351
350352 # Read the template
@@ -480,6 +482,9 @@ sub _gettemp {
480482 }
481483 }
482484
485+ my $perms = $options {file_permissions };
486+ my $has_perms = defined $perms ;
487+ $perms = 0600 unless $has_perms ;
483488
484489 # Now try MAX_TRIES time to open the file
485490 for (my $i = 0; $i < MAX_TRIES; $i ++) {
@@ -502,19 +507,19 @@ sub _gettemp {
502507 my $open_success = undef ;
503508 if ( $^O eq ' VMS' and $options {" unlink_on_close" } && !$KEEP_ALL ) {
504509 # make it auto delete on close by setting FAB$V_DLT bit
505- $fh = VMS::Stdio::vmssysopen($path , $OPENFLAGS , 0600 , ' fop=dlt' );
510+ $fh = VMS::Stdio::vmssysopen($path , $OPENFLAGS , $perms , ' fop=dlt' );
506511 $open_success = $fh ;
507512 } else {
508513 my $flags = ( ($options {" unlink_on_close" } && !$KEEP_ALL ) ?
509514 $OPENTEMPFLAGS :
510515 $OPENFLAGS );
511516 $flags |= $LOCKFLAG if (defined $LOCKFLAG && $options {use_exlock });
512- $open_success = sysopen ($fh , $path , $flags , 0600 );
517+ $open_success = sysopen ($fh , $path , $flags , $perms );
513518 }
514519 if ( $open_success ) {
515520
516521 # in case of odd umask force rw
517- chmod (0600 , $path );
522+ chmod ($perms , $path ) unless $has_perms ;
518523
519524 # Opened successfully - return file handle and name
520525 return ($fh , $path );
@@ -1048,7 +1053,8 @@ that the temporary file is removed by the object destructor
10481053if UNLINK is set to true (the default).
10491054
10501055Supported arguments are the same as for C<tempfile > : UNLINK
1051- (defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename
1056+ (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
1057+ Additionally, the filename
10521058template is specified using the TEMPLATE option. The OPEN option
10531059is not supported (the file is always opened).
10541060
@@ -1359,6 +1365,11 @@ versions, explicitly set C<< EXLOCK=>0 >>.
13591365
13601366 ($fh, $filename) = tempfile($template, EXLOCK => 1);
13611367
1368+ By default, the temp file is created with 0600 file permissions.
1369+ Use C<PERMS > to change this:
1370+
1371+ ($fh, $filename) = tempfile($template, PERMS => 0666);
1372+
13621373Options can be combined as required.
13631374
13641375Will croak() if there is an error.
@@ -1371,6 +1382,8 @@ TMPDIR flag available since 0.19.
13711382
13721383EXLOCK flag available since 0.19.
13731384
1385+ PERMS flag available since 0.24.
1386+
13741387=cut
13751388
13761389sub tempfile {
@@ -1386,8 +1399,9 @@ sub tempfile {
13861399 " SUFFIX" => ' ' , # Template suffix
13871400 " UNLINK" => 0, # Do not unlink file on exit
13881401 " OPEN" => 1, # Open file
1389- " TMPDIR" => 0, # Place tempfile in tempdir if template specified
1390- " EXLOCK" => 0, # Open file with O_EXLOCK
1402+ " TMPDIR" => 0, # Place tempfile in tempdir if template specified
1403+ " EXLOCK" => 0, # Open file with O_EXLOCK
1404+ " PERMS" => undef , # File permissions
13911405 );
13921406
13931407 # Check to see whether we have an odd or even number of arguments
@@ -1464,12 +1478,13 @@ sub tempfile {
14641478 my ($fh , $path , $errstr );
14651479 croak " Error in tempfile() using template $template : $errstr "
14661480 unless (($fh , $path ) = _gettemp($template ,
1467- " open" => $options {' OPEN' },
1468- " mkdir" => 0 ,
1469- " unlink_on_close" => $unlink_on_close ,
1470- " suffixlen" => length ($options {' SUFFIX' }),
1471- " ErrStr" => \$errstr ,
1472- " use_exlock" => $options {EXLOCK },
1481+ " open" => $options {OPEN },
1482+ " mkdir" => 0,
1483+ " unlink_on_close" => $unlink_on_close ,
1484+ " suffixlen" => length ($options {SUFFIX }),
1485+ " ErrStr" => \$errstr ,
1486+ " use_exlock" => $options {EXLOCK },
1487+ " file_permissions" => $options {PERMS },
14731488 ) );
14741489
14751490 # Set up an exit handler that can do whatever is right for the
0 commit comments