rianne809 has asked for the wisdom of the Perl Monks concerning the following question:

Hi good day to all. I have created a module which will load a list of addresses and help users to search for the name they wanted to search. I just cant load the list so the search returns nothing. here is my module, please help, thanks a many.

package Load; #use strict; use warnings; use vars qw ($VERSION @ARRAY_LIST @SEARCH_STRING); our $VERSION=0.10; our @ARRAY_LIST=(); our @SEARCH_STRING=(); sub new { my ($class) = @_; my $self = { _listNames => undef, _search => undef }; bless $self,$class; return $self; } sub listNames { my ($self,$listNames) = @_; $self->{_listNames}= $listNames if defined ($listNames); open(LIST,"$listNames") || die "Error::Cannot load names!!!\n"; #my $buff="./data/buffed.dat"; #open(BUFF,">$buff") || die "Error::Cannot load buffer file!!!\n"; while(<LIST>) { chomp($_); push (@ARRAY_LIST,$_); } } sub search { my ($self,$search) = @_; $self->{_search}=$search if defined ($search); our @SEARCH_STRING=grep(/$search/,@ARRAY_LIST); } sub display { my ($self) = @_; print "\n\nSearching directory ...\n\n"; sleep 2; print "Search Results:\n"; print "=======================================\n\n"; foreach $files (@SEARCH_STRING){ print "$files\n"; } } 1;

And here is my perl script:

#! /usr/perl/bin/perl use lib qw(./modules); use warnings; use Load; $list="./data/list.txt"; $dir=Load->new(); $dir=Load->listNames($list); $counter=0; print "=============================================\n"; print "== D I R E C T O R Y ==\n"; print "=============================================\n\n"; sleep 1; ### Search Loop until($counter==1){ print "Please enter name to be searched:\n\n"; chomp($string=<STDIN>); sleep 2; $dir=Load->search($string); $dir=Load->display(); sleep 2; print "Would you like to search another?[y/n]\n\n"; chomp($ans=<STDIN>); if ($ans=~/^[yY]/){ print "\n"; next; } elsif ($ans=~/^[Nn]/){ print "\nClosing directory ...\n\n"; sleep 2; $counter++; } else { $counter++; } }

Edit: g0n - moved text out of code tags

Replies are listed 'Best First'.
Re: how can i load the files and search for match?
by Zaxo (Archbishop) on Oct 12, 2005 at 12:50 UTC

    You don't say whether you get any error messages. I'll take the free guess and assert that the data file isn't being found. You have a hard-coded relative path, meaning the program can only be run from one particular directory.

    If you have a single data file in a known location, use an absolute path. If you want to be able to select a data file, accept the name from the command line.

    A design note: you should consider the effect of storing the namelist in a global array. Should new take the file name in the constructor, keeping the name list as instance data?

    Added: Also, the commented-out open to write suggests that you may have clobbered your name file.

    After Compline,
    Zaxo

Re: how can i load the files and search for match?
by davidrw (Prior) on Oct 12, 2005 at 13:11 UTC
    First thing i see is this:
    package Load; #use strict;
    Which probably means there was some warning and it "went away" with strict commented out... First, remove that, and we get:
    Global symbol "$files" requires explicit package name at Load.pm line +44. Global symbol "$files" requires explicit package name at Load.pm line +45.
    To fix that, make it read foreach my $files (@SEARCH_STRING){ in the display() method.

    Next error is this, which is the root cause of your problem:
    Can't use string ("Load") as a HASH ref while "strict refs" in use at +Load.pm line 22.
    Line 22 is in listNames() and is:
    $self->{_listNames}= $listNames if defined ($listNames);
    The calling code is:
    $dir=Load->new(); $dir=Load->listNames($list);
    That needs to be something along the lines of:
    my $load = Load->new(); my $dir = $load->listNames($list);
    Since listNames() accesses values of the object (blessed hashref), you can only invoke it on an object, and not directly on the class name. You should similarily modifiy the ->search() and ->display calls as well.

    Also, be _sure_ to add use strict; in your program file as well...
      Hi I already changed the script to this :

      #! /usr/perl/bin/perl use lib qw(./modules); use warnings; use strict; use Load; my $list="./data/list.txt"; my $dir=Load->new(); $dir->listNames($list); my $counter=0; print "=============================================\n"; print "== D I R E C T O R Y ==\n"; print "=============================================\n\n"; sleep 1; ### Search Loop until($counter==1){ print "Please enter name to be searched:\n\n"; chomp(my $string=<STDIN>); sleep 2; $dir->search($string); $dir->display(); sleep 2; print "Would you like to search another?[y/n]\n\n"; chomp(my $ans=<STDIN>); if ($ans=~/^[yY]/){ print "\n"; next; } elsif ($ans=~/^[Nn]/){ print "\nClosing directory ...\n\n"; sleep 2; $counter++; } else { $counter++; } }

      but still no returned search result. Please help, thanks.

      Edit: g0n - moved node text out of code tags