tom arnall am Sonntag, 30. April 2006 22.57: [...] > OK, the above stuff is all good. and now i have another question. the > following code: > > #!/usr/bin/perl -w > use strict; > use Tie::Scalar; > use DB_File; > > my ($f,@f,%f); > > tie %f, "DB_File", "file.txt" ; > $f{'a'}='aaaa'; > untie %f; > %f = (); > tie %f, "DB_File", "file.txt" ; > @f = %f; > print " > @f"; > > tie $f,"Tie::StdScalar","scalar.txt","\$f"; > $f = 'a'; > untie $f; > $f = ''; > tie $f,"Tie::StdScalar","scalar.txt","\$f"; > print " > $f > "; > > > produces: > > a aaaa > scalar.txt
Hello tom Since I'm not enough experienced with tie'ing, I can only provide a very basic and hopefully correct answer: > shouldn't the second line be 'a', i.e., shouldn't 'StdScalar' work along > the lines of the code handling the tied hash variable? When you look at the source of the Tie::StdScalar package, you can see that the TIESCALAR method just blesses a reference to the string 'scalar.txt' to the Tie::StdScalar class, FETCH gets the string, and STORE sets it. (further arguments, as "\$f" - the same as '$f' btw - above, are ignored). The class does not know that you want to treat 'scalar.txt' as a filename, and therefore no persistent storage is involved, and thus the change done by $f = 'a' is lost after untie'ing. > if this is not true, > is there a module that does work similarly to 'DB_File', i.e., one that > enables tie-ing a text file to a scalar? I don't know. Maybe you want to look at Tie::File to access the lines of a disk file via an array, search on search.cpan.org, or write your own subclass to get the desired behaviour (I can't see much sense in using Tie::StdScalar directly). Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>