On Wed, Aug 4, 2010 at 12:09, Sooraj S <[email protected]> wrote:
> Hi,
>
> My script has 3 options which recieve integers. If any of those
> options are having a value which is less than zero i want to make them
> to 100.
>
> eg: if opt2 = 32 and opt3 = 24 i want to make them to 100.
>
> code
> =====
> our $opt1,$opt2,$opt3;
> our @opt = (\$opt1,\$opt2,\$opt3);
>
> GetOptions('opt1=i' => \$opt1,
>           'opt2=i' => \$opt2,
>           'opt3=i' => \$opt3)
> check();
>
> sub check
> {
>  foreach (@opt)
>  {
>    # error here : The change does not get reflected on the actual
> options.
>    if ($$_ < 100)
>    {
>      $$_ = 100;
>    }
>  }
> }

Let's go over what this should look like:

    #!/usr/bin/perl

    use strict;
    use warnings;

The standard header all of your scripts should start with.  The
[strict pragma][0] will turn off three features of Perl 5 that have
been found to cause coding mistakes when used without proper caution.
The [warnings pragma][1] turns on the printing of optional warnings
that will help you immensely in finding problems with your code.

    use Getopt::Long;

This includes the [Getopt::Long module][2] which will read commandline
options from @ARGV for you.


    GetOptions(
        'opt1=i' => \my $opt1,
        'opt2=i' => \my $opt2,
        'opt3=i' => \my $opt3,
    );

This function call will read the options opt1, opt2, and opt3 from the
commandline and store their values (if any) in the lexical variables
$opt1, $opt2, and $opt3.

    check($opt1, $opt2, $opt3);

This calls the function check with the three arguments we parsed from
the commandline.

    print
        "opt1: $opt1\n",
        "opt2: $opt2\n",
        "opt3: $opt3\n";

This prints out the three option's values after the call to check.

    sub check {

This starts the definition of the check function.

        for my $num (@_) {
        }

This [for loop][3] will iterate over the arguments passed to check and
alias $num to them.  Because $num is aliased to the the elements of @_
and the elements of @_ are [aliased to the arguments passed to
check][4], any changes you make to $num will change the corresponding
variable passed to check.  Note that I am using the keyword for
instead of foreach.  Perl 5 does not care which keyword you use, and
for is four characters shorter.  Perl 5 can easily determine if your
for loop is an iterating loop (like this one) or a C-style
(initialize; test; mutate) loop based on the contents of the
parenthesis.

            unless (defined $num and $num > 99) {
                $num = 100
            }

This will set $num to 100 [unless]5] $num is [defined][6] and greater
than 99.  If the user does not pass an argument, the corresponding
variable will not be initialized.  Trying to use a variable that has
not initialized is [considered to be a mistake][7] by the warnings
pragma, so we check to make sure that is defined before we attempt to
compare it with 99.  The short-circuit nature of [and][8] makes sure
that we do not do the comparison if $num is undefined.

Putting it all together we get:

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;

GetOptions(
        'opt1=i' => \my $opt1,
        'opt2=i' => \my $opt2,
        'opt3=i' => \my $opt3,
);
check($opt1, $opt2, $opt3);

print
        "opt1: $opt1\n",
        "opt2: $opt2\n",
        "opt3: $opt3\n";

sub check {
        for my $num (@_) {
                unless (defined $num and $num > 99) {
                        $num = 100
                }
        }
}

 [0]: http://perldoc.perl.org/strict.html
 [1]: http://perldoc.perl.org/warnings.html
 [2]: http://perldoc.perl.org/Getopt/Long.html
 [3]: http://perldoc.perl.org/perlsyn.html#Foreach-Loops
 [4]: http://perldoc.perl.org/perlsub.html#DESCRIPTION
 [5]: http://perldoc.perl.org/perlsyn.html#Compound-Statements
 [6]: http://perldoc.perl.org/functions/defined.html
 [7]: http://perldoc.perl.org/perldiag.html#Use-of-uninitialized-value%25s
 [8]: http://perldoc.perl.org/perlop.html#Logical-And

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to