simple script

J

josef hader

Hello,
I would like to apply e.g. epstopdf to several files specified e.g by
*.eps
I have writen the following perl program, however it does not work
(resolve the joker)

#! /usr/bin/perl -w
foreach @argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
epstopdf $ARGV[$argnum];
}

thanks!
 
A

A. Sinan Unur

I would like to apply e.g. epstopdf to several files specified e.g by
*.eps
I have writen the following perl program, however it does not work
(resolve the joker)

No, we should not have to figure it out. You need to show what you
expected and what you got.
#! /usr/bin/perl -w

use strict;

missing.
foreach @argnum (0 .. $#ARGV) {

Why @argnum?
print "$ARGV[$argnum]\n";
epstopdf $ARGV[$argnum];

$argnum is undeclared.

There is no Perl function called epstopdf.

perldoc -f system

Please read the posting guidelines for this group, and follow them.

Sinan

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

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
D

David Squire

josef said:
Hello,
I would like to apply e.g. epstopdf to several files specified e.g by
*.eps
I have writen the following perl program, however it does not work
(resolve the joker)

#! /usr/bin/perl -w

You're missing a "use strict;" here...
foreach @argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n"
epstopdf $ARGV[$argnum];
}

.... which would have told you about the problem you have here with
@argnum and $argnum.

More fundamentally, what makes you think that epstopdf is a Perl
function? Perl scripts are not shell scripts. See perldoc -f system. You
might also want to look at the backtics operator.

DS
 
J

josef hader

#! /usr/bin/perl -w
use strict;
use warnings;
use diagnostics;

my $argnum=0;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
system("epstopdf ".$ARGV[$argnum]);
}


so, this is what I wanted to do...
 
D

David Squire

josef said:
#! /usr/bin/perl -w
use strict;
use warnings;
use diagnostics;

my $argnum=0;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
system("epstopdf ".$ARGV[$argnum]);
}

Much closer - it works, but is still more complicated than it need be,
and is using the system function incorrectly:

#!/usr/bin/perl

use strict;
use warnings;

foreach my $arg (@ARGV) {
print "$arg\n";
system('epstopdf', $arg);
}
 
D

David Squire

David said:
josef said:
#! /usr/bin/perl -w
use strict;
use warnings;
use diagnostics;

my $argnum=0;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
system("epstopdf ".$ARGV[$argnum]);
}

Much closer - it works, but is still more complicated than it need be,
and is using the system function incorrectly

Well, not really incorrectly, but not as I would :)
#!/usr/bin/perl

use strict;
use warnings;

foreach my $arg (@ARGV) {
print "$arg\n";
system('epstopdf', $arg);

# ... and we really should always check for success:
system('epstopdf', $arg) == 0 or warn "epstopdf $arg failed";
# see perldoc -f system for more detailed reporting using $! and $?

DS
 
D

David Squire

David said:
David said:
josef said:
#! /usr/bin/perl -w
use strict;
use warnings;
use diagnostics;

my $argnum=0;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
system("epstopdf ".$ARGV[$argnum]);
}

Much closer - it works, but is still more complicated than it need be,

If we're really aiming for compactness:

#!/usr/bin/perl
use strict;
use warnings;

map {system('epstopdf', $_) == 0 or warn "'epstopdf $_' failed\n"} @ARGV;
 
A

A. Sinan Unur

If we're really aiming for compactness:

I don't think there is any good reason to.
#!/usr/bin/perl
use strict;
use warnings;

map {system('epstopdf', $_) == 0 or warn "'epstopdf $_' failed\n"}
@ARGV;

UGH!

Your previous advice was on the money (esp. using the list argument to
system.

But this is just hard to read, IMHO.

Especially @ARGV hanging right at the end of the line, and map in void
context.

I would recommend using a loop when you mean to use a loop, and using
map if you are transforming a list.

Also, you should include $? to indicate why system failed in the warning
message.

If compactness were a criterion, there would be no reason to use Perl
for this. Just issue:

for %f in (*.eps) do epstopdf %f

from the command line.

The only reason to do anything other than that ought to be to provide a
decent utility.

Sinan

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

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
D

David Squire

A. Sinan Unur said:
OK. Sorry if I sounded too alarmist.

No worries. My underlying motivation was that the OP's code looked very
C'ish, and I was trying to think of something very Perlish :)

DS
 
G

Glenn Jackman

At 2006-05-16 08:49AM said:
map {system('epstopdf', $_) == 0 or warn "'epstopdf $_' failed\n"} @ARGV;

or:
do {system('epstopdf',$_) and warn "'epstopdf $_' failed: $!\n"} for @ARGV;
 
B

Ben Morrow

Quoth David Squire said:
If we're really aiming for compactness:

#!/usr/bin/perl
use strict;
use warnings;

map {system('epstopdf', $_) == 0 or warn "'epstopdf $_' failed\n"} @ARGV;

sub mysystem {
my $rv = system @_;
defined $rv or warn "can't run '$_[0]': $!";
local $" = ' ';
$rv and warn "'@_' failed with $?";
}

mysystem epstopdf => $_
for @ARGV;

or maybe

use Shell qw/epstopdf/;

for (@ARGV) {
epstopdf $_;
$? and warn "epstopdf failed with $?";
}

Ben
 
R

Randal L. Schwartz

Glenn> or:
Glenn> do {system('epstopdf',$_) and warn "'epstopdf $_' failed: $!\n"} for @ARGV;

Or simpler:

system(...) and warn "..." for @ARGV;

Don't invoke a do-block when not necessary. "EXPR and EXPR" is an "EXPR".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

*** ***
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top