Re: how to deal with incorrect command line argument
by Kenosis (Priest) on Oct 30, 2013 at 21:58 UTC
|
use strict;
use warnings;
my @array;
for (@ARGV) {
if (-e) {
push @array, $_;
next;
}
warn qq{"$_" doesn't exist!};
}
@ARGV = @array;
print "@ARGV";
This pushes the 'good' params onto @array and warns on the others, then reinitializes @ARGV with the 'good' ones. | [reply] [d/l] [select] |
|
Slightly simplified, I think:
use strict;
use warnings;
for (@ARGV) {
if (-e) {
&doTheWork($_);
} else {
warn qq{"$_" doesn't exist!};
}
}
| [reply] [d/l] |
|
use strict;
use warnings;
(-e) ? doTheWork($_) : warn qq{"$_" doesn't exist!} for @ARGV;
| [reply] [d/l] |
|
|
|
|
|
Thanks my man, does the trick. May I ask a few questions on your solution though... I am going to comment it on your code and maybe you can correct me where I am wrong.
my @array; # this an array to store the good arguments
for (@ARGV) { # for the argumets array
if (-e) { # if the file exists
push @array, $_;# push the file that exists onto the @array
# why use $_ here?
next; # what purpose has this
}
warn qq{"$_" doesn't exist!};
}
@ARGV = @array;
I Appreciate the time you took to answer my question to man. | [reply] [d/l] |
|
You're most welcome! Am glad it helped.
You're commenting is correct. You asked, "why use $_ here?" Because Perls default scalar ($_) is implicitly used in the for loop that's iterating through the elements of @ARGV. Note, also, that Perl's default scalar is also implicitly used in the expression if (-e) { as that expression is equivalent to if (-e $_) {.
The purpose of next is to get the next element of @ARGV, otherwise a warn would occur.
| [reply] [d/l] [select] |
Re: how to deal with incorrect command line argument
by AnomalousMonk (Archbishop) on Oct 31, 2013 at 08:10 UTC
|
... I want the program to report an error "warn" but continue and work with the correct argument.
In general, I find I have enough trouble when I try to guess what my programs should be doing; I certainly don't want my programs making those guesses!
However, in the spirit of giving good advice and then immediately undercutting it, you might look at Text::Levenshtein and related fuzzy string comparison modules; see Levenshtein distance. The strategy might be something like "look for all file names in a given directory with an L-D (or other match metric) less than a given threshold, then use the file name with the least distance if that name is unique".
>perl -wMstrict -le
"use Text::LevenshteinXS;
;;
my $tyop = 'twst.html';
for my $try (qw(test.html testb.html)) {
my $d = distance($tyop, $try);
print qq{'$tyop' < $d > '$try'};
}
"
'twst.html' < 1 > 'test.html'
'twst.html' < 2 > 'testb.html'
| [reply] [d/l] |
|
<speechless>
</speechless>
Um. Wow. I have some self-educational opportunities here, it seems.
| [reply] [d/l] |
|
| [reply] |
|
|
The day that commands in the rm family start exposing that kind of behaviour, where not only they offer suggestions a la Google ("twst.htm doesn't exist - did you mean to rm test.htm instead?") but also assume that that's what I meant ("twst.htm doesn't exist, so I went out of my way for you and rm'ed test.html instead, which, hey, you only worked on for 8 hours straight and haven't backed up yet. You're welcome, bro.") I swear I'm never touching a computer again.
| [reply] [d/l] |
|
| [reply] |
Re: how to deal with incorrect command line argument
by Anonymous Monk on Oct 30, 2013 at 23:08 UTC
|
#!/usr/bin/perl --
use strict; use warnings;
use Path::Tiny qw/ path /;
Main( @ARGV );
exit( 1 );
sub Main {
while( @_ ){
my( $oldname, $newname ) = splice @_, 0, 2;
eval {
path( $oldname )->move( $newname );
1;
} or warn $@;
}
}
Re: Multiple File Rename
| [reply] [d/l] |