5 IO_Scalar_synopsis - test out IO::Scalar
9 ### From our distribution's top level directory:
10 perl -I./lib examples/IO_Scalar_synopsis
18 my $line = ('-' x 60)."\n";
19 my $somestring = "My message:\n";
22 ### Perform I/O on strings, using the basic OO interface...
25 ### Open a handle on a string, and append to it:
27 my $SH = new IO::Scalar \$somestring;
29 $SH->print(", world!\nBye now!\n");
30 print "The string is now: ", $somestring, "\n";
32 ### Open a handle on a string, read it line-by-line, then close it:
34 $SH = new IO::Scalar \$somestring;
35 while (defined($_ = $SH->getline)) {
40 ### Open a handle on a string, and slurp in all the lines:
42 $SH = new IO::Scalar \$somestring;
43 print "All lines:\n", $SH->getlines;
45 ### Get the current position (either of two ways):
46 my $pos = $SH->getpos;
47 my $offset = $SH->tell;
49 ### Set the current position (either of two ways):
51 $SH->seek($offset, 0);
53 ### Open an anonymous temporary scalar:
56 $SH->print("Hi there!");
57 print "I printed: ", ${$SH->sref}, "\n"; ### get at value
62 ### Don't like OO for your I/O? No problem.
63 ### Thanks to the magic of an invisible tie(), the following now
64 ### works out of the box, just as it does with IO::Handle:
66 ### Open a handle on a string, and append to it:
68 $SH = new IO::Scalar \$somestring;
70 print $SH ", world!\nBye now!\n";
71 print "The string is now: ", $somestring, "\n";
73 ### Open a handle on a string, read it line-by-line, then close it:
75 $SH = new IO::Scalar \$somestring;
81 ### Open a handle on a string, and slurp in all the lines:
83 $SH = new IO::Scalar \$somestring;
84 print "All lines:\n", <$SH>;
86 ### Get the current position (WARNING: requires 5.6):
89 ### Set the current position (WARNING: requires 5.6):
92 ### Open an anonymous temporary scalar:
95 print $SH "Hi there!";
96 print "I printed: ", ${$SH->sref}, "\n"; ### get at value
105 $SH = new IO::Scalar \$str;
108 print "I printed: $SH\n";