Quoting "$vars" and open()

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Quoting "$vars" and open()

    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 ?
    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 $!;

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  4. #3

    Default Re: Quoting "$vars" and open()

    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

    --
    Andreas Kähäri
    Andreas Kahari Guest

  5. #4

    Default Re: Quoting "$vars" and open()

    Andreas Kahari 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", 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;

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  6. #5

    Default 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

  7. #6

    Default 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

  8. #7

    Default Re: Quoting "$vars" and open()

    In article <slrnbmgsr9.7o0.tadmc@magna.augustmail.com>, Tad McClellan wrote:
    > 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.
    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?
    >> 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?
    > 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

    --
    Andreas Kähäri
    Andreas Kahari Guest

  9. #8

    Default 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

  10. #9

    Default Re: Quoting "$vars" and open()

    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
    --
    $_=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

  11. #10

    Default Re: Quoting "$vars" and open()

    Gunnar Hjalmarsson wrote:
    > 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;
    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;

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  12. #11

    Default 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

  13. #12

    Default 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

  14. #13

    Default 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

  15. #14

    Default Re: Quoting "$vars" and open()

    On 17 Sep 2003 23:54:18 -0700, Chris Marshall <c_j_marshall@hotmail.com> wrote:
    > [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.
    Then you use quotes obviously, since it is a completely different case.

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

    [snip silly example]

    --
    Sam Holden

    Sam Holden Guest

  16. #15

    Default Re: Quoting "$vars" and open()

    Tassilo v. Parseval <tassilo.parseval@post.rwth-aachen.de> wrote in comp.lang.perl.misc:
    > 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):
    [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
    Anno Siegel Guest

  17. #16

    Default 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

  18. #17

    Default 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

  19. #18

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139