From spikeh at mweb.co.za Fri Jan 9 00:58:34 2004 From: spikeh at mweb.co.za (Spike) Date: Mon Aug 2 21:40:02 2004 Subject: [Za-pm] Passing values References: Message-ID: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> 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\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 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/a42295f9/attachment.htm From scarte at pan.uzulu.ac.za Fri Jan 9 02:05:03 2004 From: scarte at pan.uzulu.ac.za (Sean Carte) Date: Mon Aug 2 21:40:02 2004 Subject: [Za-pm] Passing values In-Reply-To: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> References: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> Message-ID: <1073635491.23423.7.camel@seanc.uznet.uz> 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 University of Zululand ______________________________________ Inflex - installed on mailserver for domain @pan.uzulu.ac.za Queries to: postmaster@pan.uzulu.ac.za From spikeh at mweb.co.za Fri Jan 9 02:22:53 2004 From: spikeh at mweb.co.za (Spike) Date: Mon Aug 2 21:40:02 2004 Subject: [Za-pm] Passing values In-Reply-To: <1073635491.23423.7.camel@seanc.uznet.uz> References: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> Message-ID: <5.2.1.1.2.20040109101837.02964858@pop3.mweb.co.za> 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 >University of Zululand > > >______________________________________ >Inflex - installed on mailserver for domain @pan.uzulu.ac.za >Queries to: postmaster@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 From scarte at pan.uzulu.ac.za Fri Jan 9 02:59:18 2004 From: scarte at pan.uzulu.ac.za (Sean Carte) Date: Mon Aug 2 21:40:02 2004 Subject: [Za-pm] Passing values In-Reply-To: <5.2.1.1.2.20040109101837.02964858@pop3.mweb.co.za> References: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za><5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za><5.2.1.1.2.20040109101837.02964858@pop3.mweb.co.za> Message-ID: <1073638748.23423.35.camel@seanc.uznet.uz> On Fri, 2004-01-09 at 10:22, Spike wrote: > 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: > > First answer of the year! At least I never said mine was the first *correct* answer of the year! > > > > $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; > > } The above code only appears to work because it's updating $value in the main namespace (is that the correct terminology?). It fails as soon as you make $value local to the double subroutine (or change the variable to something else): my $value = 3; double(\$value); print "$value\n"; # 6 expected -- but not gotten sub double { my $ref = shift; my $value = $$ref; $value *= 2; } Surely there must be a way to do it via references ...? -- Sean Carte University of Zululand ______________________________________ Inflex - installed on mailserver for domain @pan.uzulu.ac.za Queries to: postmaster@pan.uzulu.ac.za From ncoetzee1 at fnb.co.za Fri Jan 9 06:25:23 2004 From: ncoetzee1 at fnb.co.za (Nico Coetzee) Date: Mon Aug 2 21:40:02 2004 Subject: [Za-pm] Passing values In-Reply-To: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> References: <5.2.1.1.2.20040109085115.00aa12c8@pop3.mweb.co.za> Message-ID: <200401091425.26056.ncoetzee1@fnb.co.za> -----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. ________________________________