Interpolations within system()

B

beartiger

....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
 
J

Jürgen Exner

...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
 
B

Brian Wakem

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?
 
B

Babacio

Brian Wakem.
They are.




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

beartiger

Jürgen Exner said:
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
 
B

beartiger

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

Oh. Thanks.


J
 
A

A. Sinan Unur

Brian Wakem said:
They are.




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:

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

A. Sinan Unur

(e-mail address removed) wrote in
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
 
B

beartiger

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?
Obviously, from my first post I was aware that this was *not* the way
to go about things.


J
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g47g2000cwa.googlegroups.com:
Brian Wakem wrote:


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:

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
Subject: Interpolations within system()
From: (e-mail address removed)
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*
 
J

Jürgen Exner

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
 
G

Gunnar Hjalmarsson

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.
 
B

beartiger

Gunnar said:
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
 
G

Gunnar Hjalmarsson

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.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top