Interpolations within system()

Discussion in 'Perl Misc' started by beartiger@gmail.com, Sep 26, 2005.

  1. Guest

    ....are not allowed.

    The way I get around it is:

    ==========================================================================

    use strict;
    use warnings;

    my @files=glob("*gif");

    foreach my $file (@files)
    {

    $file=~s/\.gif//g;

    my $line="system('\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    -open:$file.gif -save:png $file.png')";


    open(INFO, ">temp.pl");
    print INFO $line;
    close(INFO);


    system('perl temp.pl');

    }

    unlink "temp.pl";

    ==========================================================================

    What is the elegant way?


    John
     
    , Sep 26, 2005
    #1
    1. Advertising

  2. Joe Smith Guest

    wrote:
    > ...are not allowed.


    You're wrong. Variable interpolation is allowed in system(),
    both in the scalar string form and the list form.
    -Joe
     
    Joe Smith, Sep 26, 2005
    #2
    1. Advertising

  3. wrote:
    [Subject: Interpolations within system()]
    > ...are not allowed.


    Where did you get that idea?
    Could you please post a brief but complete script that illustrated the
    problem?
    (What you did post was an awkward workaround for a probably nonexisting
    issue).

    jue
     
    Jürgen Exner, Sep 26, 2005
    #3
  4. Brian Wakem Guest

    wrote:
    > Interpolations within system()...are not allowed.



    They are.


    > my $line="system('\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    > -open:$file.gif -save:png $file.png')";
    >
    >
    > open(INFO, ">temp.pl");
    > print INFO $line;
    > close(INFO);
    >
    >
    > system('perl temp.pl');



    What on Earth is going on here?

    1) You are single quoting the command, which is why your variables are
    not being interpolated. Use double quotes.

    2) $line will now contain (according to perldoc -f system, which I'm
    sure you have read) the exit status of the program as returned by the
    "wait" call.

    3) You then write this exit status value to a perl file and attempt to
    execute it? Has the world gone mad? What are you *actually* trying to do?


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Sep 26, 2005
    #4
  5. Babacio Guest

    Brian Wakem.

    > They are.
    >
    >
    >> my $line="system('\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    >> -open:$file.gif -save:png $file.png')";
    >> open(INFO, ">temp.pl");
    >> print INFO $line;
    >> close(INFO);
    >> system('perl temp.pl');

    >
    >
    > What on Earth is going on here?
    >
    > 1) You are single quoting the command, which is why your variables are
    > not being interpolated. Use double quotes.
    >
    > 2) $line will now contain (according to perldoc -f system, which I'm
    > sure you have read) the exit status of the program as returned by the
    > "wait" call.
    >
    > 3) You then write this exit status value to a perl file and attempt to
    > execute it? Has the world gone mad? What are you *actually* trying
    > to do?


    It's quite hard to figure out. Maybe something like

    system("\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    -open:$file.gif -save:png $file.png");

    But it's hard to believe. I am not acquainted with the syntax of
    system commands in windows.

    What I can tell anyway if that if $x is a scalar whose value is a
    string, system($x) does what is expected. So there is nothing like
    'not-allowed interpolations within system()' !

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 26, 2005
    #5
  6. Guest

    Jürgen Exner wrote:
    > wrote:
    > [Subject: Interpolations within system()]
    > > ...are not allowed.

    >
    > Where did you get that idea?
    > Could you please post a brief but complete script that illustrated the
    > problem?
    > (What you did post was an awkward workaround for a probably nonexisting
    > issue).


    Here's what I tried:

    # list.pl:

    use strict;
    use warnings;

    my $foo="bar.html";
    system('ls -l $foo');

    # The var is ignored, and the whold directory is listed:
    #
    # [cygwin]$ perl list.pl
    # -rw-r--r-- jeh 7 Sep 26 04:15 bar.html
    # -rw-r--r-- jeh 8 Sep 26 04:17 foo.html
    # -rw-r--r-- jeh 183 Sep 26 04:19 list.pl
    #
    # Whereas, the desired output is merely:
    #
    # -rw-r--r-- jeh 7 Sep 26 04:15 bar.html

    Okay, I see now that I could simply have done this:

    # list.pl:

    use strict;
    use warnings;

    my $bar="bar.html";
    my $foo="ls -l $bar";
    system($foo);

    Duh. But, I appear to be correct: you cannot do *interpolation* within
    system, no?


    J
     
    , Sep 26, 2005
    #6
  7. Guest

    [I read this after my reply to Juergen above:]
    Brian Wakem wrote: <snips>
    > You are single quoting the command, which is why your variables are
    > not being interpolated. Use double quotes.


    Oh. Thanks.


    J
     
    , Sep 26, 2005
    #7
  8. Guest

    I wrote:
    > But, I appear to be correct: you cannot do *interpolation* within
    > system, no?


    Never mind.


    J
     
    , Sep 26, 2005
    #8
  9. Brian Wakem <> wrote in news::

    > wrote:
    >> Interpolations within system()...are not allowed.

    >
    >
    > They are.
    >
    >
    >> my $line="system('\"c:/Program Files/HyperSnap-DX
    >> 5/HprSnap5.exe\"
    >> -open:$file.gif -save:png $file.png')";
    >>
    >>
    >> open(INFO, ">temp.pl");
    >> print INFO $line;
    >> close(INFO);
    >>
    >>
    >> system('perl temp.pl');

    >
    >
    > What on Earth is going on here?
    >
    > 1) You are single quoting the command, which is why your variables are
    > not being interpolated. Use double quotes.


    Better yet, use (untested -- see perldoc -f system for more info):

    my $ret = system(
    q{"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe"},
    qq{-open:"$file".gif},
    q{-save:png},
    qq{"$file.png"},
    );

    die $? if $ret;

    That should be able to deal with spaces in filenames, and avoid the
    shell.

    > 2) $line will now contain (according to perldoc -f system, which I'm
    > sure you have read) the exit status of the program as returned by the
    > "wait" call.


    I don't think so. The OP had:

    >> my $line="system('\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    >> -open:$file.gif -save:png $file.png')";


    That will *not* invoke system.

    > 3) You then write this exit status value to a perl file and attempt to
    > execute it? Has the world gone mad? What are you *actually* trying
    > to do?


    He is trying to learn by trying different combinations of things until
    he finds one that "works". That is an extremely inefficient learning
    strategy.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 26, 2005
    #9
  10. wrote in
    news::

    > Jürgen Exner wrote:
    >> wrote:
    >> [Subject: Interpolations within system()]
    >> > ...are not allowed.

    >>
    >> Where did you get that idea?
    >> Could you please post a brief but complete script that illustrated
    >> the problem?
    >> (What you did post was an awkward workaround for a probably
    >> nonexisting issue).

    >
    > Here's what I tried:
    >
    > # list.pl:
    >
    > use strict;
    > use warnings;
    >
    > my $foo="bar.html";
    > system('ls -l $foo');


    That is a single quoted string you are passing to system. The effect is
    the same as typing

    ls -l $foo

    on the cygwin bash prompt. Since you most likely do not have an
    environment variable called $foo set, bash interprets that as

    ls -l

    On the other hand, if you had invoked system with a list of arguments
    (thereby bypassing the shell), you would have gotten the result you
    wanted:

    system('ls', '-l', $foo);

    or even just a double-quoted string:

    system("ls -l $foo");

    > # The var is ignored, and the whold directory is listed:


    See above.

    > my $bar="bar.html";
    > my $foo="ls -l $bar";
    > system($foo);
    >
    > Duh. But, I appear to be correct: you cannot do *interpolation*
    > within system, no?


    system("ls -l $bar");

    Note double quotes, as opposed to the single-quotes you used in the
    first example.

    Note also that passing a list to system is better than passing a single
    string in most cases.

    Do read perldoc -f system.

    Do read the posting guidelines for this group.

    By now, I have a sneaking suspicion that you are just trolling.

    Sinan


    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 26, 2005
    #10
  11. Brian Wakem Guest

    A. Sinan Unur wrote:

    > I don't think so. The OP had:
    >
    >>> my $line="system('\"c:/Program Files/HyperSnap-DX 5/HprSnap5.exe\"
    >>> -open:$file.gif -save:png $file.png')";

    >
    > That will *not* invoke system.



    Ah yes. It is so bizarre I could not comprehend it.


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Sep 26, 2005
    #11
  12. Dr.Ruud Guest

    beartiger:

    > my $foo="bar.html";
    > system('ls -l $foo');



    This looks more dangerous:

    my $HOME = '~/test';
    system('rm -fr $HOME');

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 26, 2005
    #12
  13. Guest

    Brian Wakem wrote:
    <snip>
    > Ah yes. It is so bizarre I could not comprehend it.


    Just curious, but why can't you guys simply help rather than being
    assholes and rubbing someone's nose in their stupid mistake?
    Obviously, from my first post I was aware that this was *not* the way
    to go about things.


    J
     
    , Sep 26, 2005
    #13
  14. wrote in news:1127736717.031508.152820
    @g47g2000cwa.googlegroups.com:

    > Brian Wakem wrote:
    > <snip>
    >> Ah yes. It is so bizarre I could not comprehend it.

    >
    > Just curious, but why can't you guys simply help rather than being
    > assholes and rubbing someone's nose in their stupid mistake?
    > Obviously, from my first post I was aware that this was *not* the way
    > to go about things.


    No. In your first post, you made an outrageous claim:

    wrote in news:1127711367.389315.271310
    @f14g2000cwb.googlegroups.com:

    > Subject: Interpolations within system()
    > From:
    > Organization: http://groups.google.com
    > Newsgroups: comp.lang.perl.misc
    >
    > ...are not allowed.


    And you stated a "solution" to an imagined problem.

    You did not ask a neutral question such as "how do I interpolate a
    variable in a system call".

    That said, no one has been an "a**h**e to you.

    Anyway, thanks for identifying yourself.

    Sinan.

    PS: Before I forget, *PLONK*



    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 26, 2005
    #14
  15. wrote:
    [...]
    > my $foo="bar.html";
    > system('ls -l $foo');
    >
    > # The var is ignored, and the whold directory is listed:


    As I thought, your problem has nothing to do with system().
    You are using single quotes and thereby explicitely requesting that Perl
    shall use the string literally without interpolating any variable.

    Have you tried double quotes?

    system("ls -l $foo");

    Further details see "perldoc perlop", section "Quote and Quote-like
    Operators"

    jue
     
    Jürgen Exner, Sep 26, 2005
    #15
  16. wrote:
    > Brian Wakem wrote:
    >> Ah yes. It is so bizarre I could not comprehend it.

    >
    > Just curious, but why can't you guys simply help rather than being
    > assholes and rubbing someone's nose in their stupid mistake?


    Your posts showed a few misconceptions. I for one noticed efforts to
    really explain what was going on, and I have severe difficulties to
    understand how that would not be helpful, and make those who tried to
    help "a..h...s".

    Is your idea of "help" limited to a code snippet that 'works'? In that
    case you have come to the wrong place.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 26, 2005
    #16
  17. Guest

    Gunnar Hjalmarsson wrote:
    > wrote:
    > > Brian Wakem wrote:
    > >> Ah yes. It is so bizarre I could not comprehend it.

    > >
    > > Just curious, but why can't you guys simply help rather than being
    > > assholes and rubbing someone's nose in their stupid mistake?

    >
    > Your posts showed a few misconceptions. I for one noticed efforts to
    > really explain what was going on, and I have severe difficulties to
    > understand how that would not be helpful, and make those who tried to
    > help "a..h...s".
    >
    > Is your idea of "help" limited to a code snippet that 'works'? In that
    > case you have come to the wrong place.


    Those who helped really did help, especially Juergen, and I thanked
    (and thank) them.

    I stand by what I said about Brian and Unur, who seemed to delight in
    rubbing my nose in my mistake.

    Now perhaps we can drop this issue, which really doesn't deserve
    further discussion.


    J
     
    , Sep 26, 2005
    #17
  18. wrote:
    > I stand by what I said about Brian and Unur, who seemed to delight in
    > rubbing my nose in my mistake.


    If that's how you perceive efforts to explain how or why incorrect
    posted code is incorrect, you'd better stay out of Usenet.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 26, 2005
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KatB
    Replies:
    0
    Views:
    468
  2. mark4asp
    Replies:
    2
    Views:
    2,276
    Harry Haller
    Nov 7, 2006
  3. Elmo Watson
    Replies:
    1
    Views:
    622
    Eliyahu Goldin
    May 8, 2007
  4. Ultrus
    Replies:
    3
    Views:
    417
    Stefan Behnel
    Jul 9, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    483
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page