Re: How do I treat a string like a filehandle?⭐
by davido (Cardinal) on Oct 02, 2003 at 03:55 UTC
|
open(MEMORY, '>', \$var)
or die "Can't open memory file: $!\n";
print MEMORY "foo!\n";
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I treat a string like a filehandle?⭐
by runrig (Abbot) on Sep 16, 2000 at 00:27 UTC
|
| [reply] [Watch: Dir/Any] |
|
I mean the IO::Scalar module from the IO-Stringy distribution on CPAN :)
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: How do I treat a string like a filehandle?
by hsinclai (Deacon) on Mar 09, 2004 at 04:14 UTC
|
Just did this in a real app - nearly fell out of the chair when it worked.
require 5.8.0; ## in order to write NSANS format to a scalar
use strict;
foreach $ns_answer (@ns_answer) {
open(NSANS,">", \$ns_holder) or die("bla..\n");
write NSANS;
print OFILE $ns_holder;
close(NSANS);
}
format OFILE= ## for the disk file report
@<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+<<<<<<<<<<<<<
$adr, $ptr_answer
.
format NSANS= ## written to handle, then to scalar, then printed t
+o OFILE
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+<<<<<<<<<<<<<
$ns_answer
.
To clarify, the format NSANS was defined, but when used, the scalar $ns_holder is opened to write to. Once the write is done, the scalar $ns_holder contains the NSANS-formatted structure.
Then the scalar $ns_holder can be written ("print"ed) to another handle, in this case the format OFILE (the final output file).. so a format(ed) thing containing different scalars can get written to a previously defined and already open'ed file handle (two identical output formats, containing different scalars, written to a single output file handle).
That wasn't too clear - sorry - but I hope it's evident that you can write to a handle to a scalar.
| [reply] [Watch: Dir/Any] [d/l] |
|
And if you are using a pre-5.80 level, or just want
to be backward compatible, you can use IO::Scalar
#!/usr/bin/perl
#You could use IO::Scalar which seems to
#be part of IO::Stringy.
use IO::Scalar;
$data = "My message:\n";
### Open a handle on a string, and append to it:
$SH = new IO::Scalar \$data;
print $SH "Hello";
print $SH ", world!\nBye now!\n";
print "The string is now: ", $data, "\n";
### Open a handle on a string, read it line-by-line, then close it:
$SH = new IO::Scalar \$data;
while (<$SH>) {
print "Got line: $_";
}
close $SH;
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I treat a string like a filehandle?
by indigo (Scribe) on Sep 16, 2000 at 03:59 UTC
|
#!/usr/bin/perl -w
use FileHandle;
use strict;
my $string =<<EOD;
One fish
Two fish
Red fish
Blue fish
EOD
my $fh = new FileHandle("echo \'$string\' |") or die;
print $_ while <$fh>;
| [reply] [Watch: Dir/Any] [d/l] |
|
No point in invoking a shell there, when this will do:
my $fh = FileHandle->new or die "Cannot create filehandle: $!";
defined(my $pid = open($fh, "-|")) or die "Cannot fork: $!";
unless ($pid) {
print <<'EOD';
One fish
Two fish
Red fish
Blue fish
EOD
exit 0;
}
print $_ while <$fh>; # in parent
But again, this forks (as does yours), and it's much better to use IO::Stringy to do it all within one process.
-- Randal L. Schwartz, Perl hacker | [reply] [Watch: Dir/Any] [d/l] |
Re: How do I treat a string like a filehandle?
by sproaticus (Initiate) on Sep 16, 2000 at 00:29 UTC
|
Sorry, newbieitis. Now I'm logged in, and have a more readable version of the code from my question:
my $stringfile = FileHandle::newFromString( "One fish\nTwo fish\nThree
+ fish\nFour fish" );
while( <$stringfile> ) {
dosomething( $_ );
}
Cheers,
- jsproat
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] [d/l] |