Ask a Question related to PERL Miscellaneous, Design and Development.
-
Chris Marshall #1
Quoting "$vars" and open()
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
Chris Marshall Guest
-
How to add "..." button to a property to open up "File Open"?
I am creating a control and thinking about adding a property to the control. I will like a "..." button shown next to the value of this property... -
Force GetUrl to open dialog box "open or save"
I'm triyng to link several buttons to several external 3D .step and .igs files. I needed Flash to open dialog box "Open or save", but what hapens... -
animation "open" onPress( animation "close" ) go to Frame "myFrame"
I have searched hi and low for the answer and nothing has helped me as of yet. My Main timeline is sectioned off by keyframes that are 10 frames... -
Is there a setting to allow $var = "" when POST and GET vars are strict?
I have the following problem: $_POST and $_GET variables can only be used when in the correct format (not $val), but when I define a variable... -
Unable To Open File "..." Because It Is Already Open With Write Permission
I am trying to open a page for offline browsing and whenever I try to open a particular link I get this "Unable To Open File '...' Because It Is... -
Gunnar Hjalmarsson #2
Re: Quoting "$vars" and open()
Chris Marshall wrote:
Who says it's wrong to quote a variable? I suppose you are referring> 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 ?
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 $!;
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Andreas Kahari #3
Re: Quoting "$vars" and open()
In article <cb9c7b76.0309170556.5bac0a80@posting.google.com >,
Chris Marshall wrote:I wouldn't say it's "wrong", but it's easy to avoid:> 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 ?
open(DATA, $var) or die; # Reading
open(DATA, ">" . $var) or die; # Writing
--
Andreas Kähäri
Andreas Kahari Guest
-
Gunnar Hjalmarsson #4
Re: Quoting "$vars" and open()
Andreas Kahari wrote:
If you are writing portable code (for Perl < 5.6.0), you can't avoid> In article <cb9c7b76.0309170556.5bac0a80@posting.google.com >,
> Chris Marshall wrote:>>>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
it when including MODE, so the latter needs to be written:
open(DATA, "> $var") or die;
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Tad McClellan #5
Re: Quoting "$vars" and open()
Chris Marshall <c_j_marshall@hotmail.com> wrote:
> I know its wrong to quote "$vars" - but is there an easy way around it
Yes:
Delete the double quote characters!
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Tad McClellan #6
Re: Quoting "$vars" and open()
Andreas Kahari <ak+usenet@freeshell.org> wrote:
> In article <cb9c7b76.0309170556.5bac0a80@posting.google.com >,
> Chris Marshall wrote:>>> 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",
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.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Andreas Kahari #7
Re: Quoting "$vars" and open()
In article <slrnbmgsr9.7o0.tadmc@magna.augustmail.com>, Tad McClellan wrote:
Good evening to you too.> Andreas Kahari <ak+usenet@freeshell.org> wrote:>>> In article <cb9c7b76.0309170556.5bac0a80@posting.google.com >,
>> Chris Marshall wrote:>>>>> 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",
> If you are going to contradict a Perl FAQ, a little bit of your
> rationale would be appreciated.
A reference? I tought we were talking about a scalar containing> It may aid in improving the answer given in the FAQ.
>
> How can stringifying a reference be "not wrong"?
a filename?
Hmm... I must have misread the question. How do you interpret>>> open(DATA, ">" . $var) or die; # Writing
> This is not related to the current discussion.
the question?
Which is correct in a general way, but we're discussing it in> We are not saying that
> ">$var"
> is wrong, we are saying that
> "$var"
> is wrong.
the context of the open() command.
Cheers,
Andreas
--
Andreas Kähäri
Andreas Kahari Guest
-
Tad McClellan #8
Re: Quoting "$vars" and open()
Andreas Kahari <ak+usenet@freeshell.org> wrote:
> In article <slrnbmgsr9.7o0.tadmc@magna.augustmail.com>, Tad McClellan wrote:>> If you are going to contradict a Perl FAQ, a little bit of your
>> rationale would be appreciated.>>> How can stringifying a reference be "not wrong"?
> 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.
>>>>>> open(DATA, ">" . $var) or die; # Writing
>> This is not related to the current discussion.
> Hmm... I must have misread the question. How do you interpret
> the question?
I thought he was asking about the Perl FAQ above.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Tassilo v. Parseval #9
Re: Quoting "$vars" and open()
Also sprach Tad McClellan:
It should, and actually will for all Perl-builtins. Yet there is a> 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.
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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
Tassilo v. Parseval Guest
-
Gunnar Hjalmarsson #10
Re: Quoting "$vars" and open()
Gunnar Hjalmarsson wrote:
Hmm.. I'd better correct myself before somebody else does. ;-)> Andreas Kahari wrote:
>>>> 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;
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;
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Chris Marshall #11
Re: Quoting "$vars" and open()
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
Chris Marshall Guest
-
Chris Marshall #12
Re: Quoting "$vars" and open()
Andreas Kahari <ak+usenet@freeshell.org> wrote in message news:<slrnbmgqce.cru.ak+usenet@vinland.freeshell.o rg>...
> In article <cb9c7b76.0309170556.5bac0a80@posting.google.com >,
> Chris Marshall wrote:>> > 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
Just what I was looking for - thanks.
Chris Marshall Guest
-
Chris Marshall #13
Re: Quoting "$vars" and open()
[email]tadmc@augustmail.com[/email] (Tad McClellan) wrote in message news:<slrnbmgsjg.7o0.tadmc@magna.augustmail.com>.. .
> Chris Marshall <c_j_marshall@hotmail.com> wrote:
>>> > I know its wrong to quote "$vars" - but is there an easy way around it
>
> 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.
Chris Marshall Guest
-
Sam Holden #14
Re: Quoting "$vars" and open()
On 17 Sep 2003 23:54:18 -0700, Chris Marshall <c_j_marshall@hotmail.com> wrote:
Then you use quotes obviously, since it is a completely different case.> [email]tadmc@augustmail.com[/email] (Tad McClellan) wrote in message news:<slrnbmgsjg.7o0.tadmc@magna.augustmail.com>.. .>>> Chris Marshall <c_j_marshall@hotmail.com> wrote:
>>>>>> > I know its wrong to quote "$vars" - but is there an easy way around it
>>
>> Yes:
>>
>> Delete the double quote characters!
>
> How about when using open() to write to a file. e.g.
">$vars" is completely different than "$vars".
[snip silly example]
--
Sam Holden
Sam Holden Guest
-
Anno Siegel #15
Re: Quoting "$vars" and open()
Tassilo v. Parseval <tassilo.parseval@post.rwth-aachen.de> wrote in comp.lang.perl.misc:
[internal workings snipped]> 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):
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
Anno Siegel Guest
-
Chris Marshall #16
Re: Quoting "$vars" and open()
[email]sholden@flexal.cs.usyd.edu.au[/email] (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
Chris Marshall Guest
-
Tad McClellan #17
Re: Quoting "$vars" and open()
Chris Marshall <c_j_marshall@hotmail.com> wrote:
> [email]sholden@flexal.cs.usyd.edu.au[/email] (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.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Chris Marshall #18
Re: Quoting "$vars" and open()
[email]tadmc@augustmail.com[/email] (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.
Chris Marshall Guest



Reply With Quote

