Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Passing multiple data types to a subroutine

by rbi (Monk)
on Nov 28, 2003 at 19:49 UTC ( [id://310778]=note: print w/replies, xml ) Need Help??


in reply to Passing multiple data types to a subroutine

You can get back the scalars like this:
my $scalar1 = 1; my $scalar2 = 2; ($scalar1,$scalar2) = my_subroutine(@array1,$scalar1,$scalar2); print "$scalar1 $scalar2 \n"; sub my_subroutine() { @array1 = @{shift()}; $scalar1 = shift(); $scalar2 = shift(); $scalar1 =+ 22; $scalar2 =+ 33; return ($scalar1,$scalar2); }
Or different types like:
my @array1 = (1,2); $arrayref = \array1; ($arrayref,$scalar1,$scalar2,$hashref) = my_sub(@array1,$scalar1,$scal +ar2); @array1 = @{$arrayref}; my %hash = %{$hashref}; print "$array[0]\n"; print "$hash{name}\n"; sub my_sub() { @array = @{shift()}; $s1 = shift(); $s2 = shift(); my %hash = {'name'=>'a', 'city'=>'b'}; $array[0] =3; $hash{name} = 'aaa'; return (\@array1,$scalar1,$scalar2,\%hash); }
Hope this helps.

Replies are listed 'Best First'.
Re: Re: Passing multiple data types to a subroutine
by Chmrr (Vicar) on Nov 28, 2003 at 20:20 UTC

    Err -- whoa there. There are a few problems with this code. First off, let me clean it up a little (such as actually passing it an array with some elements in it) and feed it though with -w -Mstrict

    my @array = (42,43,44,45); my $scalar1 = 1; my $scalar2 = 2; ($scalar1,$scalar2) = my_subroutine(@array,$scalar1, $scalar2); print "$scalar1 $scalar2\n"; sub my_subroutine() { my @a = @{shift()}; my $s1 = shift(); my $s2 = shift(); print "array: @a\ns1: $s1\ns2: $s2\n"; $s1 += 22; $s2 += 33; return ($s1, $s2); }

    ..to which perl says:

    main::my_subroutine() called too early to check prototype at - line 5. Can't use string ("42") as an ARRAY ref while "strict refs" in use at +- line 9.

    ..which shows the two most glaring bugs in the code. First off, you've given your subroutine a prototype, which only works if your calls to the subroutine are after its declaration. If you move the subroutine to above the call, however, we discover that you're giving the wrong prototype, anyways! (Too many arguments for main::my_subroutine at - line 15, near "$scalar2)")

    You're also passing in an array, and trying to treat it as an array reference in the code. That's what the second error message is telling you.

    These are all vaguely fixable by changing your code to:

    sub my_subroutine(\@$$) { my @a = @{shift()}; my $s1 = shift(); my $s2 = shift(); print "array: @a\ns1: $s1\ns2: $s2\n"; $s1 += 22; $s2 += 33; return ($s1, $s2); } my @array = (42,43,44,45); my $scalar1 = 1; my $scalar2 = 2; ($scalar1,$scalar2) = my_subroutine(@array,$scalar1, $scalar2); print "$scalar1 $scalar2\n";

    ..but don't do that, as prototypes are mostly broken and confusing. This public service announcement has been brought yo you by the letter P and the number 42.

    Networking -- only one letter away from not working

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://310778]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found