in reply to Re: Re: DOS directory naming in PERL?
in thread DOS directory naming in PERL?

I would have to disagree. Single quotes work fine when you have a backslash followed by anything but a backslash, but if you try to use two backslashes together (as in '\\foo') they will escape into one backslash. Does anyone know how to avoid this? Strings like '\\\\192.168.1.1' to get \\192.168.1.1 are ugly.

The best I could come up with is:

print <<'' . " is the UNC name" \\192.168.1.1
But this leaves an extra newline at the end of the IP address. Am I missing something blatently obvious?

Replies are listed 'Best First'.
Re: Re: Re: Re: DOS directory naming in PERL?
by chipmunk (Parson) on Jan 21, 2001 at 12:08 UTC
    In any quoted string ("", '', qq{}, q{}, etc.), a double backslash must be interpolated as a single backslash, because a backslash is used to escape the quoting character(s). This makes it possible to get the one character string \ and the two character string \', with '\\\'' and '\\', respectively.

    Here-docs are the exception to this. One can always find a terminator, possibly several characters long, that doesn't appear within the string itself. Thus, backslashes are free to be regular characters in a here-doc.

    I don't think you're missing anything; this is just how strings work in Perl.

      In any quoted string ("", '', qq{}, q{}, etc.), a double backslash must be interpolated as a single backslash, because a backslash is used to escape the quoting character(s). This makes it possible to get the one character string \ and the two character string \', with '\\\'' and '\\', respectively.

      I don't see what's wrong with having '\' mean \ and having '\'."'" or q<\'> mean \'. It's a little more typing, but personally if I was the creator of Perl I would sacrifice the special meaning of backslashes in single-quoted strings for more normality...oh well, I'll have to be content with a source filter or using here-docs. But...

      Here-docs are the exception to this. One can always find a terminator, possibly several characters long, that doesn't appear within the string itself. <

      This is something I've been thinking about. What if I'm quoting arbitrary user-supplied data in an eval()? I wouldn't want to remove any occurances of the here-doc terminator I was using -- that might upset users whose data contains my terminator. Even if the terminator is a long and rare word, there is still a chance of data/terminator clashing -- disallowing the terminator in the data seems very cargo-cult and wrong to me.

      I guess the best here-doc terminator in this case would be a randomly-generated one...if only Perl had a qb// operator to quote an arbitrary number of bytes. (__DATA__ doesn't do what I want, since it is only one section. And if I want multiple sections, I need separators. The same exact problem all over again. Oh wait -- I could just store the number of bytes inside each section of the DATA. But then I have an evil arbitrary limit on how much data the user can supply. Unless I use BER encoded integers. Too much work for me.)

        As you suggest, this quoting semantic is really just a design decision.

        If you need to quote arbitrary data in a here-doc, you might try something like the following:

        my $data = get_some_arbitrary_data(); $data =~ s/^( *)$/ $1/gm; $eval = "<<'';\n$data\n\n"; $result = eval $eval; $result =~ s/^ //; print $result;
        If you specify an empty terminator, then the here-doc ends at the next empty line. Just make sure there are no empty lines in the arbitrary data; perhaps by prepending a space to every empty line. I added a space to every line consisting only of spaces, so I could convert the data back after the eval.