Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Reading a variable as a file

by Wiggins (Hermit)
on Nov 23, 2007 at 19:44 UTC ( [id://652645]=perlquestion: print w/replies, xml ) Need Help??

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

Background:
I have read an entire file into a variable ($email); undef $/; open INPT, "afile"; $email=<INPT>;...
Done massive substitutions all over the place with REGEXs and .csv file contents;
Now I want to read (and remove) only line #1 from $email into a new variable ($subject); and then remove all "white-space only" lines until the first 'used'(non-space characters) line.

Desired Solution:
I want to "open" the variable as a file, and manipulate it with File IO semantics in a record fashion (without having to write it out to disk and read it back in again).
Or is this just a foolish desire?

Replies are listed 'Best First'.
Re: Reading a variable as a file
by ikegami (Patriarch) on Nov 23, 2007 at 19:58 UTC
    use 5.008000; # open \$var open(my $fh, '<', \$var); while (<$fh>) { ... }

    or

    while ($var =~ /(.*\n|.+)/g) { ... }
      Cool. I didn't know it also works with refs. An alternative is IO::Scalar.
      use IO::Scalar; ### Open a handle on a string, read it line-by-line, then close it: $SH = new IO::Scalar \$var; while (<$SH>) { # ... } close $SH;

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
Re: Reading a variable as a file
by fernandes (Monk) on Nov 23, 2007 at 21:56 UTC
    Plese, let me know when you find the better solution. This subject interest to me as well.
      ikegami's post gives you the best solution you're going to get.
      open my $STR, "<", \$scalar;

      is the way to go

Re: Reading a variable as a file
by thezip (Vicar) on Nov 23, 2007 at 20:04 UTC
    You may want to investigate Tie::File for this.

    Your wish is my commandline.
      Unless I misunderstand the question, Tie::File is the wrong thing here. It makes a file look like an array. But I think what OP wants is to make a scalar look like a file.

        Yes, you're absolutely right.

        But to me, this problem seems to fall somewhere in the middle regarding how it should be handled... On the one hand, a scalar containing the entire file would make it easier to perform the numerous regex substitutions, while on the other hand, an array would make it easier to to shift off the required items.

        I think the point I was trying to make was that an "array" (instead of reading the entire file into a scalar) might be another approach to this problem.

        Update: After re-reading the OP's question, I now realize that I pretty much ignored the basic spec for the problem.

        Your wish is my commandline.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found