On Feb 16, 8:06 pm, jwkr...@shaw.ca ("John W. Krahn") wrote: > Herb wrote: > > Hi All, > > Hello, > > > I am a perl novice and am having some trouble with formatting a web > > file to put into a hash. I have the following code: > > > #!/usr/bin/perl -w > > > use LWP::Simple; > > #use strict; > > > sub sws { > > > my $file = shift; > > You should probably pass the filehandle instead of the file name because > you are not using the file name anyways. > > my $fh = shift; > > > while (!eof(FH)) { > > There are almost no instances when you would ever need to use the eof() > function. > > > $line = <FH>; > > push @temp, $line; > > } > > You would normally write that as: > > while ( my $line = <FH> ) { > push @temp, $line; > } > > Or simply: > > �...@temp = <FH>; > > > foreach $line(@temp) { > > > $line =~ s/^\s*(.*?)\s*$/$1/; > > #print "$line\n"; > > } > > Better to just remove whitespace from inside the while loop instead of > running two loops: > > my @temp; > while ( my $line = <FH> ) { > $line =~ s/^\s+//; > $line =~ s/\s+$//; > push @temp, $line; > } > > > } > > > my $url = 'http://domain.com/path/to/file'; > > > my $content = get $url; > > die "Couldn't get $url" unless defined $content; > > > open (FH, ">localfile") || die "Cannot open the first file... > > $!\n"; > > print FH $content; > > close (FH); > > > my $file = "localfile"; > > Why not just declare $file farther up and use it for both open()s? > > > open (FH, "<$file") || die "Cannot open the second file... $! > > \n"; > > sws(*$file); > > You are trying to use a typeglob with a lexical variable which won't > work, typeglobs only work with package variables (like FH). You are > passing the file name to the sub which doesn't use it, you should pass > the filehandle instead: > > sws( *FH ); > > Or even better, use a lexical filehandle: > > open my $FH, '<', $file or die "Cannot open the second file... $!\n"; > > sws( $FH ); > > > close(FH); > > > exit 0; > > > I do want to use a sub so I can use it for other web files. The above > > code does write the content of the file to the localfile but the sub > > does not write the stripping of whitespace. I use the print "$line > > \n"; to test and it does print the file content into the proper format > > of stripped whitespace to STDOUT. I'd like to use strict as well but > > I have a undeclaired problem as well. > > You have to declare @temp and $line before you can use them. > > > The last step will be for me to > > put the localfile content into a hash split on a blank space. > > > As you can see I am far from proficient in perl. Any suggestions to > > get me on the correct track would be greatly appreciated. > > John > -- > The programmer is fighting against the two most > destructive forces in the universe: entropy and > human stupidity. -- Damian Conway
Thanks for the suggestions John. Passing the file handle allowed me to move forward. I'm really just trying to read everything I can to learn. I've got quite a few perl scripts that I want to put together so I am perldoc'ing quite a bit. Thanks again!! -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/