I wrote a wrapper sub for creating a new directory using make_path. When I pass this sub the octal mode value in different manners I am unable to get make_path happy. I do more than make_path but that is where my problem is so I am simplifying. I am unable to copy and paste so hand typed code follows (please pardon my typos):

#!/usr/bin/perl # # Many comments # use warnings; use strict; use lib'./'; use File::Path qw(make_path remove_tree); use Getopt::Std; use Data::Dumper; use POSIX; use constant ( FALSE => 0, TRUE => 1); sub usage { print STDERR "SetPermissions -d <directory path name> -m <mode i.e. +0777>\n"; } # Get any command line parameters. my %options = (); getopts("d:m:", \%options); # Get the directory path name. my $dirPathName = ""; if (defined($options{d})) { $dirPathName = $options{d}; } else { usage; die "ERROR: the directory path name must be supplied."; } # Get the directory mode. my $dirMode = ""; if (defined($options{m})) { $dirMode = $options{m}; } else { usage; die "ERROR: the directory mode must be supplied."; } ############################################# # Create a directory if it is not already there. # Parameters: # 1) String : The directory path name. # 2) String : The directory mode. # sub smoothMakeDir { my ($dirPathName, $mode) = @_; if ( ! -e $dirPathName ) { my $err; my $saveUmask = umask(); print STDERR "MODE: " . $mode . "\n"; my $usedMode = oct($Mode); umask(0); print STDERR "usedMode: " . $usedMode . "\n"; make_path( $dirPathName, { mode => $usedMode, error => \$err ) ); print STDERR Dumper($err); if ( @$err ) { print STDERR "$!\n"; print STDERR "ERROR: Failed to create the directory: " . $dirPat +hName . "\n"; umask($saveUmask); die "Failed to create a directory"; } umask($saveUmask); } return 0; } print STDERR "\n"; smoothMakeDir( $dirPathName . "1", $dirMode ); print STDERR "\n"; smoothMakeDir( $dirPathName . "2", 0777 ); print STDERR "\n"; smoothMakeDir( $dirPathName . "3", "0777" ); RESULTS: MODE: 0777 usedMode: 511 $VAR1 = []; MODE: 511 usedMode: 329 $VAR1 = []; MODE: 0777 usedMode: 511 $VAR1 = []; ls -al dr----x--t 2 myuserid mygroup 0 Oct 22 11:22 david1 drwxrwxrwx 2 myuserid mygroup 0 Oct 22 11:22 david2 dr----x--t 2 myuserid mygroup 0 Oct 22 11:22 david3

No matter what I try, I am unable to adjust the mode within the sub so that make_path works correctly. I want to pass a variable to the sub with the user's input but it "always" fails. Only the hard coded value works.

I just tried using chmod --> chmod($mode, $dirpathName); <-- while not setting a mode in the make_path call. I get the same results both ways.

It MUST be something in the pass by value to the sub because the print in the sub will print different values depending on what it was passed.

UPDATE:

Using --> $val = oct($val) if $val =~ /^0/; <-- from the oct() Perldoc page flipped the result! At least I can get a value passed by a variable to work correctly now. I still think that there should be a way to handle all value types passed in this "typeless" language!!!!


In reply to Automatic type conversion in a sub parameter causing a problem by davebock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.