tmatsumoto am Samstag, 13. August 2005 11.13: > Hi Beginners, Hello,
> I a new Perl programmer dealing with issues of scope. I'm trying to > write a simple library using strict. The script that runs the library > is also strict. I can get the two to work without using strict, but I > understand that that is not good programming practice. How can I write > the both scripts so that the data being moved from the script to the > library and back using strict and thereby within the scope of the > script? > > ------------------------------------------------------------------------ > --------------------------- > > Library: The usual way to implement libraries is to put the stuff into modules, containing a package definition. See perldoc perlmod This means putting the following library code into e.g. libcgi3.pm (note the suffix, abbrev. for "perl module") > #!/usr/bin/perl -w Instead of the above line, you define a package (a namespace): package libcgi3; and don't forget: use strict; use warnings; # instead of the -w switch > sub parse_input { > $whichmethod = $ENV{"REQUEST_METHOD"}; > if ($whichmethod eq "GET") { > $forminfo = $ENV{"QUERY_STRING"}; > } else { > $forminfo = <STDIN>; > } > @key_value = split(/&/,$forminfo); > %form_data; > foreach $pair (@key_value) { > ($key, $value) = split(/=/,$pair); > $value =~ s/\+/ /g; > $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg; > $form_data{$key} = $value; > } > } The better way to decode requests would be using a module designed for that, see e.g.: perldoc CGI # CGI environment perldoc Apache::Request # mod_perl environment > sub print_header { > print "Content-type: text/html\n\n"; > } The mentioned modules also provide methods for that. > return 1; > > ------------------------------------------------------------------------ > --------------------------- > > Script: > > #!/usr/bin/perl -w > > require "libcgi3.pl"; To use the module, you write instead: use libcgi3; > parse_input(); > print_header(); Without the Exporter module (see man Exporter), you have to qualify the subs of an external module with their package namespace: libcgi3::parse_input(); libcgi3::print_header(); (the script is in package/namespace "main") > > open (TOFILE,">infofile.txt"); open (TOFILE,">infofile.txt") or die "couldn't open file: $!"; > print TOFILE "$form_data{'favoriteurl'} \n"; > > print TOFILE "\n\n\n"; > > print TOFILE "$form_data{'reason'} \n"; > > close(TOFILE); close(TOFILE) or die "couldn't close file: $!"; > print "File infofile.txt successfully saved"; > > ------------------------------------------------------------------------ > --------------------------- > > Any info about using strict and scope would help a lot. > > Thanks > > Todd joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>