ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ok, so i'm toying with perl a bit and i'm writing a little isbn verifier app (something i write in just about every language i learn). basically, it takes a command line argument of an isbn string (like "123-3245-789X", or whatever), and, through a series of computations, determines if the number is valid...
anyway, here's my code at this moment:
Code:
#!/usr/bin/perl -w
use strict;
# perl ISBN verifier - my first perl attempt
my $sum1 = 0;
my $sum2 = 0;
sub isValid {
my $sum = shift;
return ($sum % 11 == 0);
}
sub getSums {
my $isbn = shift;
$isbn =~ s/[^\dXx]//;
foreach(split $isbn) {
if ($_ == "X" || $_ == "x") {
$sum1 += 10;
} else {
$sum1 += $_;
$sum2 += $sum1;
}
}
}
my $num = $ARGV[1] or die "command line args are wrong";
getSums($num);
if (isValid($sum2)) {
print "Valid\n";
} else {
print "Invalid\n";
}
and, when i run it, i get this:
Code:
<|inkedmn@skank|~/code/perl/code>% perl practice.pl 12345678X
command line args are wrong at practice.pl line 27.
my major trouble is grabbing the command-line arguments correctly (and i'm not sure if there are type issues), could somebody please tell me what i'm doing wrong?
For a comparative example, here is my "version" of the isbn number validator. (=
Code:
#!/usr/bin/perl
use strict;
use warnings;
# subroutine prototyping allows us to define subs and what
# they are expected to receive (and perl will give an error,
# usually, if it is called incorrectly)
#
sub isValid($);
# I've started doing this for the main loop of my scripts lately, to
# keep it inside of a block, for variable scoping, so we don't have
# globals.
#
#MAIN
{
@ARGV or die "Usage:\n\n\t$0 <isbn_number> <...>\n";
# test as many numbers as they wish to pass in
#
for my $isbnNumber(@ARGV) {
print isValid($isbnNumber) ? "Valid\n" : "Invalid\n";
}
exit;
}
# I combined the sum and valid parts
#
sub isValid($)
{
# double error checking to make sure that the sub gets called
# correctly
#
my($isbn) = shift || die "isValid: expected isbn number, received nothing\n";
my($sum) = 0;
my($digit);
$isbn =~ s/[^0-9xX]//g;
length($isbn) == 10 or return 0;
# instead of split'ing, pull out each char individually w/ substr
#
for my $loc(1..10) {
$digit = substr($isbn,$loc-1,1);
$sum += ($digit =~ /[Xx]/ ? 10 : $digit) * $loc;
}
return ($sum % 11 == 0);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.