Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing a FH to a module then writing to it

1 view
Skip to first unread message

Peter Billam

unread,
Nov 27, 2009, 10:56:08 PM11/27/09
to
So I've got my midi file from its URL into a scalar variable :-)
now I have to try to feed it into MIDI::Opus->new, which
won't accept data in a variable, only as a filehandle.
(The {'from_handle' => P{IO}} construction comes from
perldoc MIDI::Opus.)

use LWP::Simple();
my $url = "http://www.pjb.com.au/muscript/samples/prelude.mid";
my $midi = LWP::Simple::get($url);

use Symbol('gensym');
my $P = gensym();

use MIDI;
my $opus_ref = MIDI::Opus->new({'from_handle' => *P{IO}});
if (!print $P $midi) { warn "can't print: $!\n"; }
$opus_ref->dump();

I've tried $P{IO}, *P{IO}, $P, *{$P}{IO} and several others, and
I've tried putting the print $P $midi line before and
after the MIDI::Opus->new line, but I just get the warning:
can't print: Bad file descriptor
I guess the problem is that before MIDI::Opus->new, no file
is opened on P, and that after, MIDI::Opus->new has opened
the handle and read it and there was no data there.
Is there a way I can get my data into MIDI::Opus->new ?

Regards, Peter

--
Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html

Alan Curry

unread,
Nov 28, 2009, 12:11:21 AM11/28/09
to
In article <slrnhh17qo...@box8.pjb.com.au>,

Peter Billam <contac...@www.pjb.com.au> wrote:
>So I've got my midi file from its URL into a scalar variable :-)
>now I have to try to feed it into MIDI::Opus->new, which
>won't accept data in a variable, only as a filehandle.
>(The {'from_handle' => P{IO}} construction comes from
>perldoc MIDI::Opus.)
>
> use LWP::Simple();
> my $url = "http://www.pjb.com.au/muscript/samples/prelude.mid";
> my $midi = LWP::Simple::get($url);
>
> use Symbol('gensym');
> my $P = gensym();

Don't mess with gensym. Just do this:

open P, '<', \$midi or die;

>
> use MIDI;
> my $opus_ref = MIDI::Opus->new({'from_handle' => *P{IO}});

> if (!print $P $midi) { warn "can't print: $!\n"; }

and get rid of that print.

> $opus_ref->dump();
>

--
Alan Curry

Peter Billam

unread,
Nov 28, 2009, 1:01:03 AM11/28/09
to
> Peter Billam <contac...@www.pjb.com.au> wrote:
> >So I've got my midi file from its URL into a scalar variable :-)
> >now I have to try to feed it into MIDI::Opus->new, which
> >won't accept data in a variable, only as a filehandle.
> > use LWP::Simple();
> > my $url = "http://www.pjb.com.au/muscript/samples/prelude.mid";
> > my $midi = LWP::Simple::get($url);
> > use Symbol('gensym'); my $P = gensym();

On 2009-11-28, Alan Curry <pac...@kosh.dhis.org> replied:


> Don't mess with gensym. Just do this:
> open P, '<', \$midi or die;
> > use MIDI;
> > my $opus_ref = MIDI::Opus->new({'from_handle' => *P{IO}});
> > if (!print $P $midi) { warn "can't print: $!\n"; }
> and get rid of that print.

Many thanks ! I'd never used this before (perldoc -f open):
Since v5.8.0, perl has built using PerlIO by default. Unless
you’ve changed this (i.e. Configure -Uuseperlio), you can open
file handles to "in memory" files held in Perl scalars via:
open($fh, '>', \$variable) || ..

It all works now :-)

0 new messages