Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Returning and passing data to and from subroutines

by ssandv (Hermit)
on Jul 07, 2010 at 22:45 UTC ( [id://848570]=note: print w/replies, xml ) Need Help??


in reply to Returning and passing data to and from subroutines

It would have been helpful to point out that lines 212 and 213 are:

my @data = shift; my $loop_count = 0; while (@data) { $data[$loop_count] =~ m{s/^\s*//}xms;#line 212 $data[$loop_count] =~ m{s/\s*$//}xms;#line 213 $loop_count++; }

So first of all, shift gets _one scalar_ off the arguments. You can't pass arrays around like that in Perl, you pass lists. If you call a sub with an array as the argument, it comes in as a list. If you pass in *two* arrays, they get flattened into a single list of scalar arguments. Secondly, in Perl you can iterate over the elements of an array like so:

foreach $element (@array) { do_stuff_to($element); }
so you don't need a loop counter at all. If you *do* need a loop counter (i.e. to synchronize two array walks), you would be better off doing something like
for $index (0..$#array) { # $#array is the last index of @array do_stuff_to($array[$index]); do_stuff_to($other_array[$index]); };

Also your matches appear to be looking for some strange things. The s/// inside the m//, if it even works at all, is operating on $_, not on $data[$loop_count], I believe, which is unlikely to be what you want. m// should contain a regex, not code. s/// is code.

Replies are listed 'Best First'.
Re^2: Returning and passing data to and from subroutines
by vendion (Scribe) on Jul 17, 2010 at 03:30 UTC

    So if I change the shift to @_ that would make it work for arrays but what if I need to pass something like a scalier or a hash? Is there a way to check/handle this other than making more than one sub that preforms the same task just taking different arguments? Sorry I was confused on the use of shift and I was under the impression that shift did exactly that.

      If you call a sub with a single scalar, it's the same as calling it with a 1-element list. @_ will contain the one scalar, and you can get it with shift. If you want to call a sub with multiple arrays or hashes and not have them flattened, you need to use references, and you probably want to look at something like perlreftut. If you call a sub with a single array and you want to get all the elements out at once, you can use:

      my @args=@_; # or my %args=@_ if you know it's going to be a hash
      or
      my ($arg1, $arg2, $arg3) = @_; # faster than (shift,shift,shift)
      the second form there assumes you know how many arguments you're going to get, or don't care if some of them come back undef.

        Thanks, the reason I ask is because some how I have done something wrong. When at the programs menu if I were to input lets say "10" it some how returns as a 1 then it fails the test causing my display_menu_error sub to be executed with this error "ERROR: illegal option: 1 selected" some where along the line the 10 I give the program is changed:

        db_connect(); #Connect to the database if needed while ( display_menu(), ( my $option = <> ) ) { print '$option before processing input: ' . $option # returns 10 $option = process_input($option); print '$option after processing input: ' . $option # returns 1 if ( ( $option !~ m{/^\d+$/}xms ) or ( $option <= 0 ) or ( $option > +10 ) ) { display_menu_error("ERROR: illegal option: $option selected\n"); next; } print_form() if $option == 1; print_edit() if $option == 2; db_remove() if $option == 3; db_fetch() if $option == 4; print_all() if $option == 5; print_report() if $option == 6; db_check() if $option == 7; leave_comment() if $option == 8; leave_donation() if $option == 9; if ( $option == 10 ) { $dbh->disconnect; print "Quitter...\n"; exit 0; } } ... sub process_input { # takes in information and loops through testing the input for valid a +lphanumeric characters. my @data = @_; for (@data) { chomp; s/^\s*//; # Have tried commenting out no change s/\s*$//; # Still somehow returns incorrect values } return @data; }
        I just can't see why it would be doing that to me like that

Log In?
Username:
Password:

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

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

    No recent polls found