Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: problems passing variables between subroutine

by CountZero (Bishop)
on Sep 04, 2012 at 20:35 UTC ( #991687=note: print w/ replies, xml ) Need Help??


in reply to problems passing variables between subroutines

Read again what I explained about using prototypes.

The (\@$) prototype definition expects an array as its first argument and makes it into a reference (i.e. a scalar). So your

my (@array_of_lines, $entry_no_new) = @_;
is quite wrong.

It should be

my ($arrayref_of_lines, $entry_no_new) = @_;
and next you must dereference the reference:
my @array_of_lines = @$arrayref_of_lines;

Trust me, you are far better off forgetting all about prototypes and simply use references.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics


Comment on Re: problems passing variables between subroutine
Select or Download Code
Re^2: problems passing variables between subroutine
by Freezer (Sexton) on Sep 05, 2012 at 10:35 UTC
    Hi there, Unfortunately there is a problem as I don't seem to be passing anything in. Is the code you suggest ok, or have I made an error elsewhere in the code? What is the most convenient way for me to find out?
    sub create_output (\@$) { my ($arrayref_of_lines, $entry_no_new) = @_; my @array_of_lines = @$arrayref_of_lines; print "Hi there\n"; print "@array_of_lines"; # THIS IS NOT PRINTING ANYTHING exit;
    Update:

    I get an error message:

    Use of uninitialized value $_[1] in join or string at receive_annotati +ons_B.pl line 162.
    When the following is used:
    sub create_output (\@$) { my ($arrayref_of_lines, $entry_no_new) = @_; # THIS DOES NOT WO +RK my @array_of_lines = @$arrayref_of_lines; print "Hi there\n"; print "@_"; print "@array_of_lines"; exit;
      The error is not in the subroutine.

      In your script when you call create_output     (@array_of_lines, $entry_no_new); the array is empty since you never put anything into @array_of_lines.

      You are getting yourself confused because in the look_through_file subroutine you also declare an @array_of_lines array and fill it with data. BUT as this is a lexical array scoped to this subroutine only (and totally separate of the lexical @array_of_lines declared in the main body of your script), it will simply disappear at the end of the subroutine, taking all its data with it.

      There are two solutions:

      1. use the @array_of_lines declared in the main body of your script also in the subroutine. In other words, do not do a my @array_of_lines in your sub. THIS IS THE BAD SOLUTION!
      2. Take the return value from your look_through_file sub and put that in the @array_of_lines array declared in the main body of your script. You will have to re-adjust the return parameters of that sub and put the array at the back or the returned scalar values will get eaten by the array.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2014-09-30 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls