#!/usr/bin/perl -w use strict; my $name; my %contacts; my @children; &my_function(\$name, \%contacts, \@children); print "Name = $name\n"; foreach my $key ( keys(%contacts) ) { print "$key = $contacts{$key}\n"; } foreach my $child (@children) { print "Child = $child\n"; } sub my_function() { my $ref_name = $_[0]; my $ref_contacts = $_[1]; my $ref_children = $_[2]; $$ref_name = "Harry"; $$ref_contacts{'home'} = '1234567890'; $ref_contacts->{'work'} = '9876543210'; push(@$ref_children, 'James'); push(@$ref_children, 'Mary'); print "Name = $$ref_name\n"; foreach my $key ( keys(%$ref_contacts) ) { print "$key = $$ref_contacts{$key}\n"; } foreach my $child (@$ref_children) { print "Child = $child\n"; } print "\n"; }
Monday, June 7, 2010
Arguments By Reference
Modify scalar, hash and list through a subroutine.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment