Assignment of var without escape/interpolation

B

Bart Van der Donck

Hello,

I was surprised to find out that there seems to be only one assignment
notation that guarantees no interpolation/escape:

#!perl
my $str = <<'EndOfString';
'"\ \$@% \\A $ $r \n \\n \\\B
EndOfString
chomp $str;
print $str;

IMO this is no assignment of a var anymore. It feels more like a trick
box with here-doc and chomp.

But this notation is not very parctical to use in daily work (eg
binaries). Does anyone else have the same experience/suggestions ?
 
J

Jürgen Exner

Bart said:
I was surprised to find out that there seems to be only one assignment
notation that guarantees no interpolation/escape:

Assignments never do any interpolation.

my $foo = "Never want to see this text";
my $x = '$foo\n'; print $x;
my $y = $x; print $y;

Both print() statement print the literal text '$foo\n'. No interpolation
happening, neither when assigning directly from a string nor when assigning
from another variable.
What are you talking about?
my $str = <<'EndOfString';
'"\ \$@% \\A $ $r \n \\n \\\B
EndOfString
chomp $str;
print $str;

IMO this is no assignment of a var anymore. It feels more like a trick
box with here-doc and chomp.

To me it looks like a misguided attempt to solve a perceived problem.
But this notation is not very parctical to use in daily work (eg
binaries).

Certainly agree with that evaluation.
Does anyone else have the same experience/suggestions ?

No. And suggestion for what? You didn't tell us what you are really trying
to do.
Assignments without interpolating the assigned value cannot be the issue
because assignments don't do interpolation anyway.

jue
 
B

Bart Van der Donck

Jürgen Exner said:
[...]
Assignments never do any interpolation.

Am I using the wrong terminology ?

my $bar = "a";
my $foo = "bc$bar";

"bc$bar" is the assignment of declaration 'my $foo', right ?
As I understand it, there is an interpolation of the $bar-variable
inside the assignment of $foo, so $foo gets value "bca".
[...]
To me it looks like a misguided attempt to solve a perceived
problem.

The problem is that I want a variable to have the exact value as I
typed it in the code, regardless of which characters I use (assumed,
within the limitations of the OS and of Perl).
[...]
And suggestion for what? You didn't tell us what you are
really trying to do.

One could think of a number of application fields, for example, a
variable that holds the contents of a .gif file. Just any case where
one needs to be sure the typed characters will become the exact value
of the variable under any circumstances.
 
D

Denver

Bart said:
Am I using the wrong terminology ?
Yes.

my $bar = "a";
my $foo = "bc$bar";

"bc$bar" is the assignment of declaration 'my $foo', right ?

No. It is a string expression.
As I understand it, there is an interpolation of the $bar-variable
inside the assignment of $foo,

That's incorrect.

so $foo gets value "bca".

If you take 16/64 and cancel the sixes in the numerator and denominator, you get 1/4.
Therefore, 16/64 equals 1/4.

The problem is that I want a variable to have the exact value as I
typed it in the code, regardless of which characters I use (assumed,
within the limitations of the OS and of Perl).

Do you mean simply no interpolation?
One could think of a number of application fields, for example, a
variable that holds the contents of a .gif file.

That's outside the limitations of Perl.
You can't put in an arbitrary binary literal in line in your code.
(I don't really know why you want to.)

But as Jue showed you, you can have string literals without interpolation.

Did you try his examples?
 
J

Jürgen Exner

Bart said:
Jürgen Exner said:
[...]
Assignments never do any interpolation.

Am I using the wrong terminology ?

my $bar = "a";
my $foo = "bc$bar";

"bc$bar" is the assignment of declaration 'my $foo', right ?
As I understand it, there is an interpolation of the $bar-variable
inside the assignment of $foo, so $foo gets value "bca".

Interpolation is the correct term, but it has nothing to do with assignment.
It happens when you define a string, i.e. when using those quotes. And any
text enclosed in double quotes is interpolated, regardless of assignment or
not.
The problem is that I want a variable to have the exact value as I
typed it in the code, regardless of which characters I use (assumed,
within the limitations of the OS and of Perl).

Well, but you are asking Perl _explicitely_ to interpolate that string. If
you don't want interpolation then don't use double quotes. Use single quotes
instead, just like I did in my sample program that you snipped so
conveniently.

jue
 
P

Peter J. Holzer

Bart said:
Am I using the wrong terminology ?

my $bar = "a";
my $foo = "bc$bar";

"bc$bar" is the assignment of declaration 'my $foo', right ?
As I understand it, there is an interpolation of the $bar-variable
inside the assignment of $foo, so $foo gets value "bca".

Yes, but the interpolation is done by the double quotes, not by the
assignment. What you want is a quote operator which doesn't do any
interpolation.
One could think of a number of application fields, for example, a
variable that holds the contents of a .gif file.


This is not possible.

Firstly, a GIF file of any non-trivial length will
almost certainly contain all possible byte values and therefore also the
end-quote character if it is only a single character. So you cannot use
a "begin quote/end quote" quoting mechanism. You would either need
counted strings or an arbitrary multi-byte delimiter (which perl already
has with here-documents.

Secondly, a GIF file will usually also contain the byte which
corresponds to the newline character on the current platform. This is
not portable between platforms, so the GIF file would be destroyed
simple by running the script on a different platform.

Even if it was possible, why would you want to do this? A script with
embedded binary content is extremely fragile: Edit is once with a
less-then-perfect editor and you have made it unusable. Furthermore,
nobody is able to type in syntactically correct GIF files. And if you
want to copy an existing GIF file into a perl script it is very simple
to just add the necessary escapes (perl is very well suited for writing
such conversion scripts :).
Just any case where one needs to be sure the typed characters will
become the exact value of the variable under any circumstances.

There is no case where you need to be sure of this. You just need to be
sure the typed characters become the characters you want.

hp
 
B

Bart Van der Donck

Denver said:
No. It is a string expression.

Assignment (also 'initialization' or 're-initialization') sets or
re-sets the value of a variable, I think we can agree on that
definition ?

So, I'ld say my terminology still stands.

I wouldn't use a statement "Assignments don't do interpolation" like
Jürgen. Though it's scientifically correct (the assignment doesn't
*do* it, it are the double quotes that do it), it sounds like it's
impossible to use interpolation inside assignments. Therefore I'ld
rather state "Interpolations occur inside assignments if double quotes
are used to perform the assignment".
That's incorrect.

I don't see what's wrong with my statement. I just happen to use double
quotes for my assignment here, so the interpolation took place inside
my assignment.
Do you mean simply no interpolation?

Yes, no interpolation, and also no escape (as I originally wrote).
That's outside the limitations of Perl.

No. Perl variables may hold the contents of a gif-file correctly:

#!/usr/bin/perl
print "Content-Type: image/gif\n\n";
$file = 'plan.gif';
open my $F, '<', $file || die "Cant open $file: $!";
$chars_of_gif.=$_ while(<$F>);
close $F || die "Cant close $file: $!";
print $chars_of_gif; # $chars_of_gif holds gif file here

Running it as CGI shows that the content is correct. So I'ld conclude
it's not a Perl issue, but a OS and/or editor issue.
You can't put in an arbitrary binary literal in line in your code.
(I don't really know why you want to.)

Yes, that seems impossible indeed. Maybe with the perfect editor under
the perfect OS ? :)
But as Jue showed you, you can have string literals without interpolation.
Did you try his examples?

print '$foo\\n';

does not print the literal text, but prints:

$foo\n
 
P

Peter J. Holzer

Jürgen Exner said:
Well, but you are asking Perl _explicitely_ to interpolate that
string. If you don't want interpolation then don't use double quotes.
Use single quotes instead,

Well, single quotes (unlike single-quoted here-documents) "interpolate"
(I personally wouldn't use that term for escape-sequences, but perldoc
perlop does) \\ to \ and \<end-of-quote> to <end-of-quote>. I'm not sure
why they do - it seems like a remnant of before the day q() was
invented, because it shouldn't ever be necessary to escape the
<end-of-quote> character these days, and then escaping the backslash
isn't necessary, either.

hp
 
D

Denver

Bart said:
Assignment (also 'initialization' or 're-initialization') sets or
re-sets the value of a variable, I think we can agree on that
definition ?
Yes. But you are confusing this with how it obtains the value to be assigned.

So, I'll say my terminology still stands.
I say it doesn't.

I wouldn't use a statement "Assignments don't do interpolation" like
Jürgen. Though it's scientifically correct
It is correct.

(the assignment doesn't *do* it, the double quotes do it),
Correct in that context.

it sounds like it's impossible to use interpolation inside assignments.
I have no problem with that.

rather state "Interpolations occur inside assignments if double quotes
are used to perform the assignment".
That has a bunch of bogus baggage.

Explain:
print "bc$bar";

Are you going to say: "Interpolations occur inside print statements if double quotes are used to perform the printing"?
Obviously that is nonsense and it only gets worse.

I don't see what's wrong with my statement.
Because interpolation happens completely independent of, outside of, and before any assignment.
the interpolation took place inside the assignment.
That is false.

Yes, no interpolation, and also no escape (as I originally wrote).
Then use quotes that do not do interpolation. Is this too difficult?

No. Perl variables may hold the contents of a gif-file correctly:
They certainly may.
So I'll conclude it's not a Perl issue
You are still wrong.

Yes, that seems impossible indeed. Maybe with the perfect editor under
the perfect OS ? :)

What syntax do you propose?
 
J

Jürgen Exner

Bart said:
Assignment (also 'initialization' or 're-initialization') sets or
re-sets the value of a variable, I think we can agree on that
definition ?

So far, so good.
So, I'ld say my terminology still stands.

I wouldn't use a statement "Assignments don't do interpolation" like
Jürgen. Though it's scientifically correct (the assignment doesn't
*do* it, it are the double quotes that do it), it sounds like it's
impossible to use interpolation inside assignments.Therefore I'ld
rather state "Interpolations occur inside assignments if double quotes
are used to perform the assignment".

Sorry, you are missing the point. Quotes and assignment are totally
independant concepts. The one has nothing to do with the other.
There are quotes that are not part of an assignment: if ($foo eq
'$MyFavouriteValue') {..}
And there are assignment that don't use quotes: $a = $foo X 3;
And then there are cases where they happen to be used together: $a = "My
Text";

Your statement is rather like saying "addition occurs inside of an
assignment if the expression on the right hand side contains a plus sign".
Yeah, sure, that's correct. But it's totally missing the point by explaining
one special case instead of the generalized concept.

jue
 
A

Alan_C

Jürgen Exner said:
Bart said:
[ . . ] [ . . ]
[ . . ]
[ . . ] I'ld
rather state "Interpolations occur inside assignments if double quotes
are used to perform the assignment".

I get the same end or printed output result in the next using either single
or double quotes. And, assignment is involved.

#!/usr/bin/perl
use warnings;
use strict;

my $bar = "a";
# my $foo = "bc$bar";

# either of next 2 prints: bca
# my $foo = "bc" . $bar;
my $foo = 'bc' . $bar;
print $foo, "\n";

I agree with the logic/example about the + sign ie since the + does
it, it's naturally follows that when assigned if + is used that
addition occurs.

Double quotes interpolates but (I guess) an operator can as well under
certain circumstance -- like my code above, the concatenation operator.

The word context is coming up for me. I'm guessing here that context has
to do with it (I could be wrong). But it seems that in the context of
the operation (concatenation operator) that it knows to combine the bc
string with the *content* of the $bar scalar variable.
 
D

Dr.Ruud

Jürgen Exner schreef:
If you don't want interpolation then don't use double quotes.

There is more to it. Bart originally wrote: "there seems to be only one
assignment notation that guarantees no interpolation/escape".
That was about notation and survival, more than about assignment (that
you so conveniently etc.).

#!/usr/bin/perl
use strict;
use warnings;

my ($s1, $s2, $s3);

$s1 = q{'"\ \$@% \\A $ $r \n \\n \\\B /)(><][;};

$s2 = << 'EndOfString';
'"\ \$@% \\A $ $r \n \\n \\\B /)(><][}{;
EndOfString

$s3 = <DATA>;

chomp( $s1, $s2, $s3 );
print "1<$s1>\n";
print "2<$s2>\n";
print "3<$s3>\n";

__DATA__
'"\ \$@% \\A $ $r \n \\n \\\B /)(><][}{;
 
B

Bart Van der Donck

Peter said:
Well, single quotes (unlike single-quoted here-documents) "interpolate"
(I personally wouldn't use that term for escape-sequences, but perldoc
perlop does) \\ to \ and \<end-of-quote> to <end-of-quote>. I'm not sure
why they do - it seems like a remnant of before the day q() was
invented, because it shouldn't ever be necessary to escape the
<end-of-quote> character these days, and then escaping the backslash
isn't necessary, either.

I can't think of any other reason than backwards compatibility.
 
B

Bart Van der Donck

Jürgen Exner said:
[...]
Sorry, you are missing the point. Quotes and assignment are totally
independant concepts. The one has nothing to do with the other.
There are quotes that are not part of an assignment: if ($foo eq
'$MyFavouriteValue') {..}
And there are assignment that don't use quotes: $a = $foo X 3;
And then there are cases where they happen to be used together: $a = "My
Text";

Your statement is rather like saying "addition occurs inside of an
assignment if the expression on the right hand side contains a plus sign".
Yeah, sure, that's correct. But it's totally missing the point by explaining
one special case instead of the generalized concept.

That is not missing the point. It is making a (non-general, indeed)
statement.

It's like saying:

"Leather seats are included if you buy a BMW 750."

Sure, leather seats and a BMW have nothing do with each other.

Am I missing the point here ? No, because I 'm just making a statement
for a particular case. I'm not making a general definition about BMW's
or leather seats here.
 
T

Tad McClellan

Alan_C said:
my $foo = 'bc' . $bar;

Double quotes interpolates but (I guess) an operator can as well under
certain circumstance -- like my code above, the concatenation operator.


There is no interpolation in your code above.

The word context is coming up for me. I'm guessing here that context has
to do with it (I could be wrong).


You are.

But it seems that in the context of
the operation (concatenation operator) that it knows to combine the bc
string with the *content* of the $bar scalar variable.


"evaluating an expression" (as in your code above) and "interpolation"
are not the same thing.

Interpolation has only to do with strings, evaluating expressions
happens all over the place.
 
T

Tad McClellan

Peter J. Holzer said:
Well, single quotes (unlike single-quoted here-documents) "interpolate"
(I personally wouldn't use that term for escape-sequences, but perldoc
perlop does) \\ to \ and \<end-of-quote> to <end-of-quote>. I'm not sure
why they do - it seems like a remnant of before the day q() was
invented, because it shouldn't ever be necessary to escape the
<end-of-quote> character these days,


If the string contains every character, then there are no
characters available for backslashless literals.
 
P

Peter J. Holzer

Tad said:
If the string contains every character, then there are no
characters available for backslashless literals.

That's true of course, but there are easy ways to work around it.
E.g.,

$allprintableasciichars = '!"#$%&' . q(') . '()*+...';

or

$allprintableasciichars = "!\x{22}#\x{24}%&'()*+...";

both of which are turned into a simple string literal by the perl
compiler or maybe even

$allprintableasciichars = join('', map {chr} (33 .. 126));

which constructs the string at runtime.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top