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

Hi guys. I'm using Perl on Win XP.

I am trying to use.. my $log = "\\$pcname\c$\windows\windowsupdate.log";

Every time I do it errors out saying invalid escape on w. Can someone help me get this line to work?

Replies are listed 'Best First'.
Re: Can't load this path into Perl
by kyle (Abbot) on Jan 22, 2009 at 21:27 UTC
    my $pcname = 'pee-cee-name'; my $log = "\\\\$pcname\\c\$\\windows\\windowsupdate.log"; print "$log\n"; __END__ \\pee-cee-name\c$\windows\windowsupdate.log

    Inside quotes, a backslash is an escape character. To get a literal backslash, you need two of them, and you need one before the dollar sign so it's not introducing a variable.

Re: Can't load this path into Perl
by mr_mischief (Monsignor) on Jan 22, 2009 at 23:34 UTC
    In addition to what good advice Limbic~Region kyle already gave, note that in most cases Perl code on Windows can use forward slashes in path names. Consider this, tested to work even with UNC paths:

    my $log = '//AIR/c$/windows/windowsupdate.log'; # "AIR" is the COMPUTE +RNAME of one of my systems

    Also note that variables are interpolated in double quotes (or in qq() expressions) and are not interpolated in single quotes (or in q() expressions).

    As an additional Windows-specific tip, remember that C:\Windows is just a default. What you really want for the Windows directory to be safer is the WINDIR environment variable for the system in question. A hard-coded path will be right most of the time, though, or if you have enough control over the target machines to guarantee it.

    Update: credited the wrong monk above. I'm not sure how I managed that. Sorry, kyle.

Re: Can't load this path into Perl
by poolpi (Hermit) on Jan 23, 2009 at 11:54 UTC

    If you need to deal with file names,
    see this core module in PathTools: File::Spec .
    (At least since Perl 5.00405)

    my $full_path = File::Spec->catpath( $volume, $directory, $file );


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb