J
Jorge
As an exercise in learing Object Oriented Perl I am creating a class
and a calling script which simply checks the command line arguments to
see if each file is present. So far it appears to be working as
expected but I have a question about the use of @ARGV in my calling
script.
The only way I could see to pass @ARGV as an object was to make it a
scalar ... thus the line ...
my $list_of_files = join ' ', @ARGV;
Is there a way I can use a reference such as \@ARGV and somehow
derference it in the package?
Both my script and package is listed below -- I appreciate any help on
this - thank you.
snip<------ package starts here ------>snip
#!/usr/local/bin/perl -w
package CmdLineArgs;
use strict;
sub new {
my $cla = {};
$cla->{QTY} = undef;
$cla->{LIST} = [];
bless($cla);
return $cla;
}
sub qty {
my $cla = shift;
if (@_) { $cla->{QTY} = shift }
return $cla->{QTY};
}
sub list {
my(@args, $args, @args_not_found);
my $cla = shift;
if (@_) { $cla->{LIST} = shift }
@args = split(' ', $cla->{LIST} );
foreach $args(@args){
if(!( -f $args)){
push(@args_not_found, $args);
}
}
if(@args_not_found){
return @args_not_found;
}else{
return "success";
}
}
1;
snip<------ calling script starts here ------>snip
#!/usr/local/bin/perl -w
use strict;
use lib '/home/blahblah/lib';
use CmdLineArgs;
my $args;
$args = CmdLineArgs->new();
$args->qty($#ARGV + 1);
my $list_of_files = join ' ', @ARGV;
print "\n".$args->qty." files were specified as command line arguments
\n";
print "(".$list_of_files.")\n";
if($args->list($list_of_files) eq "success"){
print $args->list($list_of_files)."\n";
}else{
my @not_found = $args->list($list_of_files);
foreach my $not_found(@not_found){
print $not_found." - was not found\n";
}
}
and a calling script which simply checks the command line arguments to
see if each file is present. So far it appears to be working as
expected but I have a question about the use of @ARGV in my calling
script.
The only way I could see to pass @ARGV as an object was to make it a
scalar ... thus the line ...
my $list_of_files = join ' ', @ARGV;
Is there a way I can use a reference such as \@ARGV and somehow
derference it in the package?
Both my script and package is listed below -- I appreciate any help on
this - thank you.
snip<------ package starts here ------>snip
#!/usr/local/bin/perl -w
package CmdLineArgs;
use strict;
sub new {
my $cla = {};
$cla->{QTY} = undef;
$cla->{LIST} = [];
bless($cla);
return $cla;
}
sub qty {
my $cla = shift;
if (@_) { $cla->{QTY} = shift }
return $cla->{QTY};
}
sub list {
my(@args, $args, @args_not_found);
my $cla = shift;
if (@_) { $cla->{LIST} = shift }
@args = split(' ', $cla->{LIST} );
foreach $args(@args){
if(!( -f $args)){
push(@args_not_found, $args);
}
}
if(@args_not_found){
return @args_not_found;
}else{
return "success";
}
}
1;
snip<------ calling script starts here ------>snip
#!/usr/local/bin/perl -w
use strict;
use lib '/home/blahblah/lib';
use CmdLineArgs;
my $args;
$args = CmdLineArgs->new();
$args->qty($#ARGV + 1);
my $list_of_files = join ' ', @ARGV;
print "\n".$args->qty." files were specified as command line arguments
\n";
print "(".$list_of_files.")\n";
if($args->list($list_of_files) eq "success"){
print $args->list($list_of_files)."\n";
}else{
my @not_found = $args->list($list_of_files);
foreach my $not_found(@not_found){
print $not_found." - was not found\n";
}
}