Server IP : 85.214.239.14 / Your IP : 3.141.31.100 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/doc/libio-stringy-perl/examples/ |
Upload File : |
#!/usr/bin/perl =head1 NAME IO_Scalar_synopsis - test out IO::Scalar =head1 SYNOPSIS ### From our distribution's top level directory: perl -I./lib examples/IO_Scalar_synopsis =cut use 5.005; use IO::Scalar; use strict; my $line = ('-' x 60)."\n"; my $somestring = "My message:\n"; ### ### Perform I/O on strings, using the basic OO interface... ### ### Open a handle on a string, and append to it: print $line; my $SH = new IO::Scalar \$somestring; $SH->print("Hello"); $SH->print(", world!\nBye now!\n"); print "The string is now: ", $somestring, "\n"; ### Open a handle on a string, read it line-by-line, then close it: print $line; $SH = new IO::Scalar \$somestring; while (defined($_ = $SH->getline)) { print "Got line: $_"; } $SH->close; ### Open a handle on a string, and slurp in all the lines: print $line; $SH = new IO::Scalar \$somestring; print "All lines:\n", $SH->getlines; ### Get the current position (either of two ways): my $pos = $SH->getpos; my $offset = $SH->tell; ### Set the current position (either of two ways): $SH->setpos($pos); $SH->seek($offset, 0); ### Open an anonymous temporary scalar: print $line; $SH = new IO::Scalar; $SH->print("Hi there!"); print "I printed: ", ${$SH->sref}, "\n"; ### get at value ### Don't like OO for your I/O? No problem. ### Thanks to the magic of an invisible tie(), the following now ### works out of the box, just as it does with IO::Handle: ### Open a handle on a string, and append to it: print $line; $SH = new IO::Scalar \$somestring; print $SH "Hello"; print $SH ", world!\nBye now!\n"; print "The string is now: ", $somestring, "\n"; ### Open a handle on a string, read it line-by-line, then close it: print $line; $SH = new IO::Scalar \$somestring; while (<$SH>) { print "Got line: $_"; } close $SH; ### Open a handle on a string, and slurp in all the lines: print $line; $SH = new IO::Scalar \$somestring; print "All lines:\n", <$SH>; ### Get the current position (WARNING: requires 5.6): $offset = tell $SH; ### Set the current position (WARNING: requires 5.6): seek $SH, $offset, 0; ### Open an anonymous temporary scalar: print $line; $SH = new IO::Scalar; print $SH "Hi there!"; print "I printed: ", ${$SH->sref}, "\n"; ### get at value ### Stringification: print $line; my $str = ""; $SH = new IO::Scalar \$str; print $SH "Hello, "; print $SH "world!"; print "I printed: $SH\n"; ### Done! 1;