Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Simulate file

by DreamT (Pilgrim)
on Sep 20, 2011 at 08:19 UTC ( #926855=perlquestion: print w/replies, xml ) Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have a script that opens a file using a normal file-handle, no problems with that. But, I'd like to generate the file in the script rather than having a physical file (I know it may sound stupid, but there are reasons for it). I tried to use $INC and __FILE__ to simulate a physical file:
BEGIN { $INC{'../myfile.txt'} = __FILE__; My file content }


(of course the above gives me syntax error)
How can I do this?

Replies are listed 'Best First'.
Re: Simulate file
by moritz (Cardinal) on Sep 20, 2011 at 08:33 UTC
    There are several solutions:

    The easiest is to change the call to open to something more suitable, like opening a scalar reference ("in-memory file"), as described in perlopentut.

    If you absolutely don't want to modify the call to open(), you can replace open() with your own open() sub, which intercepts the opening process, and then returns an in-memory file handle instead. See perlsub for details (search for CORE::)

    Another option is to use something like ptrace (not very portable) to intercept system calls to fopen. But that's probably really overkill.

Re: Simulate file
by Khen1950fx (Canon) on Sep 20, 2011 at 09:42 UTC
    IO::String will let you do in-memory, in-core file operations, a "simulated file":
    #!/usr/bin/perl use strict; use warnings; use IO::String; my $str = <<EOT; Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. EOT my $io = IO::String->new($str); $io->open('STDOUT'); my @lines = <$io>; $io->getline; my $buf = ""; read($io, $buf, 100); select $io; print STDOUT "$str\n"; $io->close('STDOUT');
Re: Simulate file
by johngg (Canon) on Sep 20, 2011 at 10:03 UTC

    You can also open a file handle directly against a reference to a HEREDOC as well as a scalar reference.

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > open my $inFH, q{<}, \ <<EOD or die qq{open: <<HEREDOC: $!\n}; > some letters > this line also 99 has digits > this does not > EOD > > while ( <$inFH> ) > { > print if m{\d}; > } > > close $inFH or die qq{close: <<HEREDOC: $!\n};' this line also 99 has digits knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Simulate file
by Anonymous Monk on Sep 20, 2011 at 08:42 UTC

    %INC is for modules, not just any files

    Use 3 argument open and here-docs, example

    #!/usr/bin/perl -- use strict; use warnings; use autodie; # dies if open/close... fail Main(@ARGV); exit(0); sub Main { Demo(); } sub Demo { my ( $Input, $Input2, $WantedOutput ) = DemoData(); NotDemoMeaningfulName( $Input, $Input2, \my $Output ); ... } sub DemoData { my $One = <<'__One__'; Optometrists and Opticians (simple) Regulations, Cap. 500A, RG 2 __One__ my $Two = <<'__Two__'; OPTOMETRISTS AND OPTICIANS (SIMPLE) REGULATIONS, Cap. 500A, Regulation + 2 __Two__ my $Three = <<'__Two__'; {RG 2}{Regulation 2} __Two__ return \$One, \$Two, $Three; } ## end sub DemoData sub NotDemoMeaningfulName { my ( $inputOne, $inputTwo, $outputFile ) = @_; open my ($inOne), '<', $inputOne; open my ($inTwo), '<', $inputTwo; open my ($outFh), '>', $outputFile; ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-10-17 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (92 votes). Check out past polls.

    Notices?