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: Re: [dsaliba] cannot pass contents of a variable from a sub to the main program: Edit Log



BillKSmith
Veteran

May 17, 2013, 6:21 AM


Views: 419
Re: [dsaliba] cannot pass contents of a variable from a sub to the main program

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)


Edit Log:
Post edited by BillKSmith (Veteran) on May 17, 2013, 7:12 AM


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

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