Hello,

Long-time perl wanna-be-hacker, first time poster here.

This script is running on a Sunblade 100 running Solaris 9 and the current
sun version of perl(based on recommended patch clusters) - 5.6.1.

I've got a script where I'm trying to read a list of files and switches,
sometimes from <STDIN> and sometimes from @ARGV. Here's the part of the
script where I check to see if STDIN is a file and read info from @ARGV:

--------- Begin -------
if (-f){
        print "STDIN is a file...\n";
        while (<STDIN>) {
                chomp;
                $use_stdin = "y";
                push @files, $_;
        }
        $numArgs = $#files + 1;
}
if (!defined @ARGV && !-f) {
        die "$usage";
}
if (!defined $numArgs) {
        $numArgs = $#ARGV + 1;
}

ARG_LOOP: foreach $argnum (0 .. $#ARGV) { # Do stuff with the command line
arguments

        my $num_min_args = 1;
        if ($ARGV[$argnum] eq "\-h") {
                die "$usage\n-w ==> Enable web directory storage.  This option will
place output files in the web\/ directory under PMs.\n[Input files] ==> A
list of .fig files which are wished to be converted to pdf files. Each
xxx.fig will be converted to xxx.pdf.\n\n";
        }
        elsif ($ARGV[$argnum] eq "\-w") {
                $webdir = "y";
        }
        elsif ($numArgs < $num_min_args && $use_stdin ne "y") {
                print "\nYou need more arguments - you only gave us $numArgs and we 
need
$num_min_args.\n";
                die "$usage";
        }
        elsif ($use_stdin ne "y") {
                push @files, $ARGV[$argnum];
        }
        else {
                last ARG_LOOP;
        }
}
print "Got $numArgs files to process...\n";

------ End -------

So - the jist of it is I wish to send a list of files to be converted
either by <STDIN> or @ARGV, and have the script figure it out based on
which I pass it.  The main reasoning (which may be flawed) I have is that
I want to be able to pass it an argument of files (ie fig2pdf.pl *.fig) or
pass it a pipeline (ie find . -name "*.pdf" | fig2pdf.pl) but either way I
need to also be able to send it a -w switch.

I've googled this pretty extensively and searched the archives equally
vigorously and came up with the 'if (-f)', but neither that nor 'if (-t)
seem to ever return a 1 so that the loop is executed.

Here's the entirety of my code below if you're interested.  Apologies in
advance - I wrote this in about an hour total and haven't spent much time
debugging it.

#!/usr/bin/perl -w
#
# Written Mon, Sep 20, 2004 by Schuyler Bishop
#
# SCCS ID %W% %G%
#
use strict;
my ($use_stdin, $argnum, $pdffile);
my $numArgs;
my $fig2ps = "/imc/data1/bin/grbatch -device 1 ";
my $webdir = "";
my @files=();
my $pwd = `pwd`;
chomp $pwd;
my @pwd_split = split /\//, $pwd;
print "\n";
my @scr_split = split /\//, $0;
my $script = $scr_split[-1];
my $usage = "\nUsage: $script [-w] [-h] [Files to convert]\n\n";

if (-f){
        print "STDIN is a file...\n";
        while (<STDIN>) {
                chomp;
                $use_stdin = "y";
                push @files, $_;
        }
        print "\$numArgs = $#files + 1\n";
        $numArgs = $#files + 1;
}
if (!defined @ARGV && !-f) {
        die "$usage";
}
if (!defined $numArgs) {
        $numArgs = $#ARGV + 1;
}

ARG_LOOP: foreach $argnum (0 .. $#ARGV) { # Do stuff with the command line
arguments

        my $num_min_args = 1;
        if ($ARGV[$argnum] eq "\-h") {
                die "$usage\n-w ==> Enable web directory storage.  This option will
place output files in the web\/ directory under PMs.\n[Input files] ==> A
list of .fig files which are wished to be converted to pdf files. Each
xxx.fig will be converted to xxx.pdf.\n\n";
        }
        elsif ($ARGV[$argnum] eq "\-w") {
                $webdir = "y";
        }
        elsif ($numArgs < $num_min_args && $use_stdin ne "y") {
                print "\nYou need more arguments - you only gave us $numArgs and we 
need
$num_min_args.\n";
                die "$usage";
        }
        elsif ($use_stdin ne "y") {
                push @files, $ARGV[$argnum];
        }
        else {
                last ARG_LOOP;
        }
}
print "Got $numArgs files to process...\n";

foreach $pdffile (@files) {
        print "\nWorking on $pdffile ...\n";
        my $relative;
        my @path;
        my $dir;
        if ($pdffile =~ "\/") {
                @path = split /\//, $pdffile;
        }
        else {
                $path[0] = $pdffile;
        }
        my $basedir;
        if ($path[0] eq "." || substr($path[0], 0, 1) ne "\/") {
                $relative = "y";
        }
        if ($webdir eq "y") {
                foreach $dir (@path) {
                        if ( $dir =~ /PMs/ && $relative ne "y") {
                                $basedir = "$pwd/$basedir/web";
                        }
                        elsif ($#path eq 0){
                                my $pwd_dir;
                                foreach $pwd_dir (@pwd_split){
                                        if ($pwd_dir =~ /PMs/){
                                                $basedir = "$basedir/web";
                                        }
                                        else {
                                                $basedir = "$basedir/$pwd_dir";
                                        }
                                }
                                last;
                        }
                        elsif ($dir eq $path[-1]) {
                                last;
                        }
                        elsif ($dir =~ /PMs/ && $relative eq "y") {
                                $basedir = ".$basedir/web";
                                last;
                        }
                        else {
                                $basedir = "$basedir/$dir";
                        }
                }
        }
        else {
                foreach $dir (@path) {
                        if ($#path eq 0 ){
                                $basedir = "$pwd/";
                                last;
                        }
                        elsif ( $dir eq $path[-1] && $relative ne "y") {
                                last;
                        }
                        elsif ($dir eq $path[-1] && $relative eq "y") {
                                $basedir = ".$basedir";
                                last;
                        }
                        else {
                                $basedir = "$basedir/$dir";
                        }
                }
        }
        if (!-e $basedir) {
                system ("mkdir -p $basedir");
        }
        my $in_fig = $pdffile;
        my @out_tmp = split /.fig/, $path[-1];
        my $tmp_ps = "$out_tmp[0].ps";
        my $out_pdf = "$out_tmp[0].pdf";

        unlink '$basedir/$out_pdf';
        system ("$fig2ps $in_fig -printfile $tmp_ps");
        print "Writing to $basedir/$out_pdf ...\n";
        system ("ps2pdf $tmp_ps $basedir/$out_pdf");
        unlink $tmp_ps;
}
print "\n";


_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to