in reply to Re: Write to and read from scalar
in thread Write to and read from scalar

Hi, I'm not really sure I get the point. This can be done with a simple assignment $variable = 'xyz'. Then you just print this varible with print $variable. If this is not wat you are looking for please try to describe the problem other way.

Replies are listed 'Best First'.
Re^3: Write to and read from scalar
by tobyink (Canon) on Mar 23, 2012 at 09:49 UTC

    The point is that certain functions expect to be passed filehandles for various purposes.

    Sometimes the data you want to pass to these functions is actually held in a variable. The naive way of coping with that is to write out your data to a temporary file, open the file and pass the filehandle to the function. Often a better solution though is to construct a filehandle which points straight to the scalar variable, and not to a real file on disk.

    Grandfather provides one technique, supported by Perl 5.8+. If (for some perverse reason) you want to support earlier releases of Perl, then IO::String also provides a solution using tied filehandles.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'