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

hi I am new to perl what is going on here.. Initial value of kitdir is D:\kitTemp but when i run the script it is taking different directory like $kitdir= D:\kitTemp\graphics\bin so i am getting error like unable to open D:\kitTemp\graphics\bin\log for writing please help me how can i initilize ..
$KitDir = cwd; $KitDir =~ s|/|\\|g; $ENV{'KITDIR'} = $KitDir after the 1000 lines of script they are using like this ,, $KitDir = $ENV{'KITDIR'}; if ($KitDir eq "") { $KitDir = $Config::Conn{KitDir}; }
i want to be always Kitdir value is D:\kitTemp then only i wont get any errors..

Replies are listed 'Best First'.
Re: variable initilization
by 7stud (Deacon) on Nov 24, 2009 at 05:44 UTC

    How about:

    ... ... $ENV{'KITDIR'} = 'D:\\kitTemp';
Re: variable initilization
by Ratazong (Monsignor) on Nov 24, 2009 at 07:34 UTC
    That's easy ... just assign $KitDir at the beginning with the correct value, and never assign a value to it again. Especially, don't do
    $KitDir = $ENV{'KITDIR'};
    at the end of your script.
    HTH, Rata
Re: variable initilization
by Marshall (Canon) on Nov 24, 2009 at 14:04 UTC
    I feel for you.
    You are tasked with modifying very bad code.

    Things go wrong from the very first program statement.
    cwd is a "buzz word" meaning: current working directory.

    Without getting into fancy stuff, I show below why the first statement is wrong ('cwd' is almost certainly not what is intended for $Kitdir). And then I say that there is no need to translate "forward slash" to "back slash". Perl will do that for you when it is necessary as a path name! I show you that this translation works, but don't do it!

    $KitDir = cwd; print "$KitDir\n"; # prints 'cwd' use Cwd; $Kdir = getcwd(); # $Kdir is: Where am I? print "$Kdir\n"; # prints 'C:/TEMP' $Kdir =~ s|/|\\|g; # not necessary! Don't! print "$Kdir\n"; # prints 'C:\TEMP'
    So that covers your first 2 program statements.
    $ENV{'KITDIR'} = $KitDir; #assigns $KitDir value to #KITDIR key in ENV hash
    In the below:
    I don't think that $ENV{'KITDIR'} will wind up with what you think it will.
    $KitDir = $ENV{'KITDIR'}; if ($KitDir eq "") { $KitDir = $Config::Conn{KitDir}; }
    I suppose we could have:
    $ENV{'KITDIR'} ||= $Config::Conn($KitDir);
    but it is hard for me to see how $ENV{'KITDIR'} could be false given the above statements.
      Thank you very much for your reply still iam not understanding this perl expression $Kdir =~ s|/|\\|g;    # not necessary! Don't! can you please reply Thank you
        This is a substitution expression that changes forward slash to a backward slashes. The windows command line needs to have backward slashes while Unix uses forward slashes. With Perl you can use forward slash on both types of systems provided that this is in a Perl function call not a shell cmd to the native OS. I always use forward slash in Perl code. The backward slash has a purpose as an escape character, hence the need for this backslash backslash to mean one backslash. That is not an issue for the forward slash and resulting regular expressions are easier to understand.
Re: variable initilization
by doug (Pilgrim) on Nov 24, 2009 at 15:24 UTC
    use Readonly; Readonly my $KitDir => 'D:\kitTemp';