Home > Archive > PERL Miscellaneous > September 2005 > Interpolations within system()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Interpolations within system()
|
|
| beartiger@gmail.com 2005-09-26, 3:56 am |
| ....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
| |
| Joe Smith 2005-09-26, 3:56 am |
| beartiger@gmail.com wrote:
> ...are not allowed.
You're wrong. Variable interpolation is allowed in system(),
both in the scalar string form and the list form.
-Joe
| |
| Jürgen Exner 2005-09-26, 3:56 am |
| beartiger@gmail.com 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
| |
| Brian Wakem 2005-09-26, 8:01 am |
| beartiger@gmail.com 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
| |
| Babacio 2005-09-26, 8:01 am |
| 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é erre hue ixe eu elle, Bruxelles.
| |
| beartiger@gmail.com 2005-09-26, 8:01 am |
| J=FCrgen Exner wrote:
> beartiger@gmail.com wrote:
> [Subject: Interpolations within system()]
>
> 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=3D"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=3D"bar.html";
my $foo=3D"ls -l $bar";
system($foo);
Duh. But, I appear to be correct: you cannot do *interpolation* within
system, no? =20
J
| |
| beartiger@gmail.com 2005-09-26, 8:01 am |
| [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
| |
| beartiger@gmail.com 2005-09-26, 8:01 am |
| I wrote:
> But, I appear to be correct: you cannot do *interpolation* within
> system, no?
Never mind.
J
| |
| A. Sinan Unur 2005-09-26, 8:01 am |
| Brian Wakem <no@email.com> wrote in news:3pq3giFbe3a9U1@individual.net:
> beartiger@gmail.com wrote:
>
>
> 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.
[color=darkred]
> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| A. Sinan Unur 2005-09-26, 8:01 am |
| beartiger@gmail.com wrote in
news:1127734634.817001.139960@o13g2000cwo.googlegroups.com:
> Jürgen Exner wrote:
>
> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Brian Wakem 2005-09-26, 8:01 am |
| A. Sinan Unur wrote:
> I don't think so. The OP had:
>
>
> 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
| |
| beartiger@gmail.com 2005-09-26, 8:01 am |
| 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
| |
| Dr.Ruud 2005-09-26, 8:01 am |
| 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."
| |
| A. Sinan Unur 2005-09-26, 8:01 am |
| beartiger@gmail.com wrote in news:1127736717.031508.152820
@g47g2000cwa.googlegroups.com:
> Brian Wakem wrote:
> <snip>
>
> 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:
beartiger@gmail.com wrote in news:1127711367.389315.271310
@f14g2000cwb.googlegroups.com:
> Subject: Interpolations within system()
> From: beartiger@gmail.com
> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Jürgen Exner 2005-09-26, 8:01 am |
| beartiger@gmail.com 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
| |
| Bernard El-Hagin 2005-09-26, 8:01 am |
| "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> That said, no one has been an "a**h**e to you.
Ummm, yes *you* have (not just to him, but to countless others on
*many* occasions). You suspected him of trollish behaviour, even though
he hasn't demonstrated any such thing.
> PS: Before I forget, *PLONK*
That's the 6th "plonk" you announced publically in the past couple of
days. You really are ridiculous. 90% of what you post is pointless
diatribe or (equally pointless) information on your latest "plonk"
victim. Well, I've had enough of reading your arrogant nonsense, so
into my aptly named shitlist you go.
--
Bernard
| |
| Gunnar Hjalmarsson 2005-09-26, 6:58 pm |
| beartiger@gmail.com wrote:
> 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?
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
| |
| beartiger@gmail.com 2005-09-26, 6:58 pm |
| Gunnar Hjalmarsson wrote:
> beartiger@gmail.com wrote:
>
> 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
| |
| Gunnar Hjalmarsson 2005-09-26, 6:58 pm |
| beartiger@gmail.com 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
|
|
|
|
|