wanadlan has asked for the wisdom of the Perl Monks concerning the following question:

This code can unzip certain file in zip file. But it use '@ARGV' to run this program. How can it run without '@ARGV' or How can it run in CGI?
#!/usr/bin/perl -w # Extracts the named files into 'extractTest' subdir # usage: # perl extract.pl [-j] zipfile.zip filename [...] # if -j option given, discards paths. # # $Revision: 1.5 $ # use strict; my $dirName = 'extractTest'; use vars qw( $opt_j ); use Archive::Zip qw(:ERROR_CODES); use Getopt::Std; $opt_j = 0; getopts('j'); if (@ARGV < 2) { die <<EOF usage: perl extract.pl [-j] zipfile.zip filename [...] if -j option given, discards paths. EOF } my $zip = Archive::Zip->new(); my $zipName = shift(@ARGV); my $status = $zip->read( $zipName ); die "Read of $zipName failed\n" if $status != AZ_OK; foreach my $memberName (@ARGV) { print "Extracting $memberName\n"; $status = $opt_j ? $zip->extractMemberWithoutPaths($memberName) : $zip->extractMember($memberName); die "Extracting $memberName from $zipName failed\n" if $status != +AZ_OK; }

Replies are listed 'Best First'.
Re: How can I unzip certain file in zip file?
by Jaap (Curate) on Jan 29, 2003 at 20:05 UTC
    How can it run without '@ARGV' or How can it run in CGI?

    Well... if you set $zipName and $memberName[0] in the code, you could disble the parts that reads out @ARGV

    If you want to run it inCGI, take a look at the CGI module.
      Since under CGI you have named parameters instead positional parameters, you'll have to restruture your code to extract the parameters at the top of your script. Also in your for loop instead of looping on @ARGV, you would instead loop on the array in your querystring.
      # at the top to load your parameters # call the script as script.pl?zipName=blah&members=abc&members=xyz my $zipName = $cgi->param('zipName'); my @members = $cgi->param('members'); print 'usage message' unless $zipName and @members

      --
      integral, resident of freenode's #perl
      
Re: How can I unzip certain file in zip file?
by OM_Zen (Scribe) on Jan 29, 2003 at 20:34 UTC
    Hi,

    The module CGI has to be used for cgi like
    use strict; use CGI; # You have to get the zip filename from the form elements o +f an HTML page using the cgi->param method my $cgi = new CGI; my $zipName = $cgi->param('Zipfilenm'); # the Archive::Zip methods can be used here with the $zipNa +me as the parameter and I guess the global parameter ($opt_h) is for +the option of the command line processing of the file , hence can be +avoided and need not like use Getopts::Std and avoid error processin +g with ARGV , but choose to process errors in the form where you get +the zipName and stuff , then you should be all set
A reply falls below the community's threshold of quality. You may see it by logging in.