Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

open to a scalar variable

by girarde (Hermit)
on Jun 04, 2007 at 21:42 UTC ( [id://619237]=perlquestion: print w/replies, xml ) Need Help??

girarde has asked for the wisdom of the Perl Monks concerning the following question:

Per the open manpage, I am attempting to open a filehandle to a variable in memory and I am unable to read the variable. My code looks like this:

open(MESSAGE, "+>>", "\$message") or die "no message $!"; open(WHOM, ">>", "&=MESSAGE") or die "no whom $!"; open (WHAT, ">>", "&=MESSAGE") or die "no what $!";

after which much is written to the handles using various formats, and an attempt to get the data is made so:

while (my $line = <MESSAGE>) { print $line; $text .= $line; } print $text;

The result is:

Use of uninitialized value in print at check_remove.pl line 85.

where line 85 is the print $text line. Opening MESSAGE in different modes does not yield better results. For instance, using the syntax in the open manpage results in a Filehandle MESSAGE opened only for output error. It works OK if I omit the handles and point the formats at STDOUT, but I am on Win32 and want to mail the output, so piping the output to SENDMAIL won't help. I was going to use Mail::Sendmail, which is why I want the output in a scalar. What am I missing?

Replies are listed 'Best First'.
Re: open to a scalar variable
by kyle (Abbot) on Jun 04, 2007 at 21:53 UTC

    Don't put quotes around the variable (see open).

    open(MESSAGE, '+>>', \$message) or die "no message $!";

    (What you're doing is opening a file literally named "$message".)

      Unquoting \$message yielded an "uninitialized value" error at the print statement.

        Putting together some of the other suggestions in this thread, I come up with this:

        my $message = ''; open(MESSAGE, '+>>', \$message) or die "no message $!"; open(WHOM, '>>&=MESSAGE') or die "no whom $!"; open(WHAT, '>>&=MESSAGE') or die "no what $!"; print WHOM "I say 'whom'\n"; print WHAT "I say 'what'\n"; my $text; while (my $line = <MESSAGE>) { print $line; $text .= $line; } print $text; __END__ I say 'whom' I say 'what' I say 'whom' I say 'what'
Re: open to a scalar variable
by almut (Canon) on Jun 04, 2007 at 22:35 UTC
    and an attempt to get the data is made so: ...

    Why do you need to get at the data in $message by reading from the associated filehandle? (seems like a rather cumbersome way to copy a string from one variable to another... :)

    But if you really insist on doing that way, I suppose you have to reset the filepointer to the beginning before reading, e.g.

    use strict; use warnings; open my $msg, "+>", \my $message or die "no message $!"; print $msg " foo\n"; print $msg " bar\n"; print "\$message:\n$message"; seek $msg, 0, 0; # reset filepointer my $text; while (my $line = <$msg>) { $text .= $line; } print "\$text:\n$text";

    Output:

    $message: foo bar $text: foo bar

    In other words, once you have the mail in $message, why not pass it directly to Mail::Sendmail...

      It turns out that once the filehandles are working properly I don't need to read from the filehandle, I can just read $message normally. Thank you.
Re: open to a scalar variable
by kwaping (Priest) on Jun 04, 2007 at 21:55 UTC
    I'm not very familiar with this technique, but try this instead:
    open(MESSAGE, "+>>", \$message) or die "no message $!"; open(WHOM, ">>&=", MESSAGE) or die "no whom $!"; open (WHAT, ">>&=", MESSAGE) or die "no what $!";
    If $message is already a reference, try dropping the backslash too.

    ---
    It's all fine and dandy until someone has to look at the code.
      $message is declared but not defined until the reference assignment is made.

      Anyway, unquoting $message and putting the &= into the mode argument worked. Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://619237]
Approved by kwaping
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found