CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
cannot pass contents of a variable from a sub to the main program

 



dsaliba
Novice

May 17, 2013, 5:40 AM

Post #1 of 4 (430 views)
cannot pass contents of a variable from a sub to the main program Can't Post

Basically what I'm trying to do is retrieve the contents of a file and store them in a variable $name via a procedure list_proc. Then I want to be able to use the contents of the variable $name in the main program. Thanks in advance.


Code
list_proc(); 
print "$name"; #variable is now empty
print "Enter name to check if you're on the guest list\n";
$entry = <STDIN>;
chomp ($entry);

if ($entry ne $name) {
print "You're not on the list!";
} else {
print "Enjoy your evening $entry";
}

sub list_proc {
open (WORDSLIST, "file")|| die "can't open wordslist";

while ($name = <WORDSLIST>) {
chomp($name);
print "$name\n"; #contents of file are stored
}
close (WORDSLIST);
}


BillKSmith
Veteran

May 17, 2013, 6:21 AM

Post #2 of 4 (422 views)
Re: [dsaliba] cannot pass contents of a variable from a sub to the main program [In reply to] Can't Post

You have several errors. The most serious is that you cannot store a list of names in a scalar variable ($name).
As your subroutine reads each name, it overwrites the previous one and prints the current one. When it comes to the end of the file, it sets $name to undef and returns.

Your main program only tries to compare the entry with one name (it does not even have that), not all of them

The subroutine should store all the names as keys of a hash and return the hash. The main program could them test if the name exists in the hash.

UPDATE: Add more comments.

You should always use the pragmas 'use strict' and 'use warnings'. Fix all the errors that they report.

Use the three argument form of open (Refer: perldoc -f open). Also use a lexical filehandle. (my $WORDSLIST rather than the bareword WORDSLIST)

Add the special variable $! (refer: perldoc perlvar) to the error message on open.

It is a good idea to put all your file input in a subroutine. Why not do the same for terminal input? In this case consider using IO::Prompt::Tiny from CPAN.
Good Luck,
Bill

(This post was edited by BillKSmith on May 17, 2013, 7:12 AM)


dsaliba
Novice

May 17, 2013, 8:01 AM

Post #3 of 4 (417 views)
Re: [BillKSmith] cannot pass contents of a variable from a sub to the main program [In reply to] Can't Post


In Reply To
You have several errors. The most serious is that you cannot store a list of names in a scalar variable ($name).
As your subroutine reads each name, it overwrites the previous one and prints the current one. When it comes to the end of the file, it sets $name to undef and returns.

Your main program only tries to compare the entry with one name (it does not even have that), not all of them

The subroutine should store all the names as keys of a hash and return the hash. The main program could them test if the name exists in the hash.


In the subroutine (in the while loop) I inserted print "$name" to check if the entire list is stored and it is, not just one name.


FishMonger
Veteran / Moderator

May 17, 2013, 8:29 AM

Post #4 of 4 (413 views)
Re: [dsaliba] cannot pass contents of a variable from a sub to the main program [In reply to] Can't Post


Quote
I inserted print "$name" to check if the entire list is stored and it is, not just one name.


Wrong.

$name DOES NOT store a list. It only stores the last value it was assigned. You are overwriting that value at each iteration of the loop.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives