[Za-pm] Passing values

Nico Coetzee ncoetzee1 at fnb.co.za
Fri Jan 9 06:25:23 CST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

 Hi,

It all depends on how you like to manage you global variables vs local
variables. Perl is very forgiving if you don't use the 'strict' option.
Personally I always include 'use strict' to force myself to pay atentian on
variable scope - something that can stuff you around without the propper
decleration etc.

The other comment I want to make is that it is good practise to use the return
value of you sub's better. Consider the following:

sub double {
   my $num = shift;
   if( $num =~ /\d+/ ) {
      return $num*2;
   }
   return "err";
}

As you can see, we now have error checking build in. By default the sub
returns an error message, unless we have passed a number ( integer ). You
could then call the sub like this:

my $value = 55;
my $returnval = double( $value );
if( $returnval =~ /^err/i ) { print "Error!\n"; }
else { print "Double $value is equal to $returnval\n"; }

On subs that work on global values, I use the return value to indicate if the
value was succesfully updated or not, for example:

sub addone {
   my $num = shift;
   if( ( $num =~ /\d+/ ) && ( $num < 10 ) ) {
      $globalvalue++;
      return 1;
   }
   return 0;
}

Now, consider the following:

if ( addone( $somevalue ) ) {
   # global value was incremented - do want you want here...
} else {
   # global value was NOT incremented - do what you want here...
}

The above example will only increment the global value if the sub recieves a
number smaller then 10.

Also, check out the following little snippet which builds on the previous
example:

#!/usr/bin/perl
my $string = "I am 30 odd years old. Sometimes I whish I was 7 again.";
print "$string\n";
$string =~ s/(\d+)/addone( $1 )/ieg;
print "$string\n";
sub addone {
   my $num = shift;
   if( ( $num =~ /\d+/ ) && ( $num < 10 ) ) {
      $num++;
      return $num;
   }
   return $num;
}

When you run it, you should see something like:

I am 30 odd years old. Sometimes I whish I was 7 again.
I am 30 odd years old. Sometimes I whish I was 8 again.

I think you get the idea.

Cheers

Nico

On Friday 09 January 2004 08:58, Spike wrote:
> Hi All
>
> First query of the year!
>
> If I have a function (sub routine) like :
>
> sub double {
> my $num = shift;
> return $num * 2;
> }
>
> I could say:
>
> $value = 3;
> $value = double($value);
> print "$value
> ";  #   6 expected
>
> My question is how could I write that function so that it works directly on
> the variable that is passed to it in the same way that functions like chomp
> do?
>
> $value = 3;
> double($value);
> print "$value
> ";  #   6 expected
>
>
>
>
>
> Spike Hodge
>
> UNIX Programmer
> M-Web Technology
> 021 596 8496
> 083 294 9593
> Fax 0866721733
>
>
> Click here and make M-Web your homepage
> http://homepage.mweb.co.za

- -- 


BOFH excuse :

_Rosin_ core solder? But...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE//p2zlXERkPl0klQRAuGnAJ4yKSr0CWrDpEFcU55XNOMI+x2uSwCfa6wX
7mVfmbwS6s+fIGBlWXESnRQ=
=NyBs
-----END PGP SIGNATURE-----

___________________________________________________________________________________________________


The views expressed in this email are, unless otherwise stated, those of the author and not those
of the FirstRand Banking Group or its management.  The information in this e-mail is confidential
and is intended solely for the addressee. Access to this e-mail by anyone else is unauthorised. 
If you are not the intended recipient, any disclosure, copying, distribution or any action taken or 
omitted in reliance on this, is prohibited and may be unlawful.
Whilst all reasonable steps are taken to ensure the accuracy and integrity of information and data 
transmitted electronically and to preserve the confidentiality thereof, no liability or 
responsibility whatsoever is accepted if information or data is, for whatever reason, corrupted 
or does not reach its intended destination.

                               ________________________________



More information about the Za-pm mailing list