Re: slurp in file
On Sun, 09 Feb 2003 12:54:51 -0500, Wiggins D'Anconia wrote: ktb wrote: Is there any function that places perl code from a file into a program as if it was just part of the program file itself? perldoc -f use perldoc -f require perldoc -f eval Don't forget perldoc -f do Will get you started. As for slurping data: perldoc -f open perldoc perlopentut http://danconia.org Cheerio, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: slurp in file
On Sun, 9 Feb 2003 11:45:56 -0600, [EMAIL PROTECTED] (Ktb) wrote: Is there any function that places perl code from a file into a program as if it was just part of the program file itself? Something like: file ** Hello World. ** program $file = slurp('file'); print $file\n: Well the advice given by others is correct, but just to answer the slurp part of your question. #Return the file contents as a scalar value sub slurp { my $OP ; open FS, $_[0] or die Can't open $_[0]:$!; {local $/ = undef; #file as 1 big string instead of line by line $OP = FS; } close FS or die Can't close $_[0]:$!; return $OP ; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
slurp in file
Is there any function that places perl code from a file into a program as if it was just part of the program file itself? Something like: file ** Hello World. ** program $file = slurp('file'); print $file\n: I'm creating an address book as described on page 237 of Beginning Perl and adding a few twists. I would like to add functionality that would allow a person to be prompted for and add/edit/delete entries in the address book. I thought it would be best to place those entries into a separate file in the format of: From the book - $addressbook{Paddy Malone} = { address = 23, Blue Jay Way, phone = 404-6599 }; And then bring them back into the program to print out. Thanks, kent -- To know the truth is to distort the Universe. Alfred N. Whitehead (adaptation) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: slurp in file
ktb wrote: Is there any function that places perl code from a file into a program as if it was just part of the program file itself? perldoc -f use perldoc -f require perldoc -f eval Will get you started. As for slurping data: perldoc -f open perldoc perlopentut http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: slurp in file
ktb wrote: From the book - $addressbook{Paddy Malone} = { address = 23, Blue Jay Way, phone = 404-6599 }; And then bring them back into the program to print out. Thanks, kent Hi Kent, How about don't do it? Look, when your text shows an assignment statement such as this, it is simply an example of how to prime your hash--for testing purposes only--from within your code. The rule should be: NEVER hard-code your data. Data is one thing. Program code is another. To include coded functionality, use the use or require statements. To store and retrieve data: =Devise a storage format appropriate to your data. For instance: ==CSV: Paddy Malone, 23 Blue Jay Way, 404-6599 ==Tagged Sections [BEGIN ADDRESS] name=Paddy Malone address=23 Blue Jay Way phone=404-6599 [END ADDRESS] =Develop a standard set of functions to store in this format and retrieve from it. =When you need to add, delete or modify data, call the appropriate function to open and edit the data file. I'll give you an example for the second format. You just got a new entry for Paddy Malone, with the same address as above, but phone number 514-2233 [Paddy went cellular, say]. #assumed: Paddy Malone has been loaded into $CurrentMember{'name'} open IN, address_book.txt; open OUT, address_book.tmp; my $EnteredName = $CurrentMember{'name'}; my $Line; while (defined ($Line = IN) and !( $Line =~ $EnteredName) ) {print OUT $Line;} if (defined($Line) { #if existing record is found print OUT $Line; #this prints the name=Paddy Malone line, which, being the #key for the record, will not changed $Line = IN; while ( !($Line =~ /[END ADDRESS]/)) { chomp $Line; my ($attribute, $value) = split (/=/, $Line); if (defined ($CurrentMember{$attribute})) { $value = $CurrentMember{$attribute} } print OUT $attribute=$value\n; $Line = IN; } print OUT $Line\n; while (defined ($Line = IN)) {print OUT $Line;} } else { # Here goes code to append a new record if no existing record for # Paddy Malone is found } close IN; close OUT; rename address_book.tmp address_book.txt; At first galnce, this might seem like a lot of processing, but the thing is, you only have to do it once. Of course, the code above is only an example, and you would need to refine it for your purposes. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]