[Za-pm] Passing values

Spike spikeh at mweb.co.za
Fri Jan 9 02:22:53 CST 2004


This seems to work too:-

#!/usr/bin/perl

use strict;

my $num = 3;

print "$num\n";

double($num);

print "$num\n";  # 6 expected

#------------------------#

sub double
{
     $_[0] *= 2;
}



At 2004/01/09 10:04 AM, Sean Carte wrote:
>On Fri, 2004-01-09 at 08:58, Spike wrote:
> > Hi All
> >
> > First query of the year!
>
>First answer 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\n";  #   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\n";  #   6 expected
> >
>
>Use references:
>
>$value = 3;
>double(\$value); # pass a reference to $value
>print "$value\n";  #   6 expected
>
>sub double {
>   my $ref = shift;
>   $value = $$ref; # dereference $ref
>   $value *= 2;
>}
>
>--
>Sean Carte <scarte at pan.uzulu.ac.za>
>University of Zululand
>
>
>______________________________________
>Inflex - installed on mailserver for domain @pan.uzulu.ac.za
>Queries to: postmaster at pan.uzulu.ac.za


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  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/za-pm/attachments/20040109/e67bf4d4/attachment.htm


More information about the Za-pm mailing list