Quoting "$vars" and open()

C

Chris Marshall

I know its wrong to quote "$vars" - but is there an easy way around it
when using open() and your filename is in a $var ?

Thanks
Chris
 
G

Gunnar Hjalmarsson

Chris said:
I know its wrong to quote "$vars" - but is there an easy way around
it when using open() and your filename is in a $var ?

Who says it's wrong to quote a variable? I suppose you are referring
to statements claiming that it's bad style to quote variables *when
there is no reason for doing so*. But, for instance, if you have a
directory path in the variable $dir and a file name in the variable
$file, there is absolutely nothing wrong with doing:

open FH, "$dir/$file" or die $!;
 
A

Andreas Kahari

I know its wrong to quote "$vars" - but is there an easy way around it
when using open() and your filename is in a $var ?

I wouldn't say it's "wrong", but it's easy to avoid:

open(DATA, $var) or die; # Reading

open(DATA, ">" . $var) or die; # Writing
 
G

Gunnar Hjalmarsson

Andreas said:
I wouldn't say it's "wrong", but it's easy to avoid:

open(DATA, $var) or die; # Reading

open(DATA, ">" . $var) or die; # Writing

If you are writing portable code (for Perl < 5.6.0), you can't avoid
it when including MODE, so the latter needs to be written:

open(DATA, "> $var") or die;
 
T

Tad McClellan

Andreas Kahari said:
I wouldn't say it's "wrong",


If you are going to contradict a Perl FAQ, a little bit of your
rationale would be appreciated.

It may aid in improving the answer given in the FAQ.

How can stringifying a reference be "not wrong"?

open(DATA, ">" . $var) or die; # Writing


This is not related to the current discussion.

We are not saying that

">$var"

is wrong, we are saying that

"$var"

is wrong.
 
A

Andreas Kahari

If you are going to contradict a Perl FAQ, a little bit of your
rationale would be appreciated.

Good evening to you too.
It may aid in improving the answer given in the FAQ.

How can stringifying a reference be "not wrong"?

A reference? I tought we were talking about a scalar containing
a filename?
This is not related to the current discussion.

Hmm... I must have misread the question. How do you interpret
the question?
We are not saying that
">$var"
is wrong, we are saying that
"$var"
is wrong.

Which is correct in a general way, but we're discussing it in
the context of the open() command.


Cheers,
Andreas
 
T

Tad McClellan

Andreas Kahari said:
A reference?


We are discussing this Perl FAQ:

perldoc -q vars

What's wrong with always quoting "$vars"?


"What's wrong" according the FAQ answer is the danger of
stringifying a reference.

I tought we were talking about a scalar containing
a filename?


I thought we were talking about useless uses of double quotes,
without regard to what the variable's value is, nor where
the variable is used.

You _never_ need

"$vars"

because

$vars

will always work in its place.

Hmm... I must have misread the question. How do you interpret
the question?


I thought he was asking about the Perl FAQ above.
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
I thought we were talking about useless uses of double quotes,
without regard to what the variable's value is, nor where
the variable is used.

You _never_ need

"$vars"

because

$vars

will always work in its place.

It should, and actually will for all Perl-builtins. Yet there is a
difference on the inside between these two (which makes your points even
more valid):

ethan@ethan:~$ perl -MDevel::peek
$a = 1;
Dump($a);
Dump("$a");
__END__
SV = IV(0x812b084) at 0x8128ab0
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 1
SV = PV(0x811e638) at 0x8128ac8
REFCNT = 1
FLAGS = (PADTMP,POK,pPOK)
PV = 0x8123078 "1"\0
CUR = 1
LEN = 2

The stringification will upgrade the scalar value (well, not really
upgrading - the IV part is lost; there are also cases possible where
both PV and IV exist). So

$a = 1;
$b = "$a";

is the equivalent to the other direction as in

$a = "1";
$b = $a + 0;

To make people stop using these casts in cases where they are pointless,
it might be worth to remark that a stringification can make a scalar use
more memory and thus reduce the performance slightly:

ethan@ethan:~$ perl -MDevel::Size=size
print size(1), "\n";
print size("1"), "\n";
__END__
16
26

When doing these conversions in a very unclever way, as in

$a = $b = $c = 1;
@a = ("$a", "$b", "$c");
my $sum;
$sum += $_ for @a;

which means: IV -> PV -> IV, this has runtime-costs since these casts
naturally come at some computational costs that could easily be avoided.

Tassilo
 
G

Gunnar Hjalmarsson

Gunnar said:
If you are writing portable code (for Perl < 5.6.0), you can't
avoid it when including MODE, so the latter needs to be written:

open(DATA, "> $var") or die;

Hmm.. I'd better correct myself before somebody else does. ;-)

Both those open() statements specify just two arguments, so they work
in Perl versions before 5.6.0. What require Perl 5.6.0 (or later)
are open() statements with three or more arguments, such as:

open(DATA, ">", $var) or die;
 
C

Chris Marshall

Andreas> open(DATA, ">" . $var) or die; # Writing

Tad> This is not related to the current discussion.
Andreas> Hmm... I must have misread the question. How do you
interpret
Andreas> the question?

Tad> I thought he was asking about the Perl FAQ above.


I guess that Andreas was "right" in interpreting my question - I was
hoping to come away with what he gave me:

open(DATA, $var) or die; # Reading

open(DATA, ">" . $var) or die; # Writing
 
C

Chris Marshall

Andreas Kahari said:
I wouldn't say it's "wrong", but it's easy to avoid:

open(DATA, $var) or die; # Reading

open(DATA, ">" . $var) or die; # Writing


Just what I was looking for - thanks.
 
C

Chris Marshall

Yes:

Delete the double quote characters!


How about when using open() to write to a file. e.g.

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

my $var = "file.txt";

open(DATA, > $var) or die "$!"; # Writing
print DATA "hello world\n";


../test.pl
syntax error at test.pl line 7, near ", >"
Execution of test.pl aborted due to compilation errors.
 
S

Sam Holden

How about when using open() to write to a file. e.g.

Then you use quotes obviously, since it is a completely different case.

">$vars" is completely different than "$vars".

[snip silly example]
 
A

Anno Siegel

Tassilo v. Parseval said:
Also sprach Tad McClellan:


It should, and actually will for all Perl-builtins. Yet there is a
difference on the inside between these two (which makes your points even
more valid):

[internal workings snipped]

There is another case where "$vars" and $vars are different, quite on
the surface. It happens when $vars is an object whose stringification
is overloaded. Obviously, $var is the object, but "$var" can be anything.

In the presence of overloading a few basic rules in Perl don't apply
anymore. Variable stringification is one example, and "\ @$x", "\ %$x"
and "\ $$x" aren't necessarily no-ops for overloaded $x.

Anno
 
C

Chris Marshall

(e-mail address removed) (Sam Holden) wrote in message
How about when using open() to write to a file. e.g.

Then you use quotes obviously, since it is a completely different case.

">$vars" is completely different than "$vars".

[snip silly example]


Well calling it silly is a bit harsh.

The whole basis for my question was:
1) the faq says not to enclose a $var in quotes
2) when you open a file for writing you are tempted to do just that.

Anyway - the 3 parameter form of open() is just what I was looking for
and if ">$vars" is perfectly safe then even better.

Cheers,
Chris
 
T

Tad McClellan

Chris Marshall said:
(e-mail address removed) (Sam Holden) wrote in message
How about when using open() to write to a file. e.g.

Then you use quotes obviously, since it is a completely different case.

">$vars" is completely different than "$vars".

[snip silly example]


Well calling it silly is a bit harsh.

The whole basis for my question was:
1) the faq says not to enclose a $var in quotes


It was "silly" because it did not illustrate the situation that
the FAQ is talking about.

If your question was about a Perl FAQ, it would have been helpful
to point that out said FAQ, it would have avoided some of the
resulting misunderstandings...

2) when you open a file for writing you are tempted to do just that.


No you're not.

You are tempted to do ">$var", which is *not* what the FAQ is talking about.

You have misunderstood the FAQ.

What is "bad" is quoting when the *only thing* quoted is a
scalar variable.

The FAQ says that

"$var"

is wrong. The FAQ does not say anything about

">$var"

Anyway - the 3 parameter form of open() is just what I was looking for


"$var" being "wrong" has nothing to do with open().

print "$var";

is just as wrong.
 
C

Chris Marshall

(e-mail address removed) (Tad McClellan) wrote in message
What is "bad" is quoting when the *only thing* quoted is a
scalar variable.

The FAQ says that

"$var"

is wrong. The FAQ does not say anything about

">$var"


ah - ok I get it now.

Thanks for the advice and apologies if the question was badly worded.
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top