How to access filehandle through globref?

J

J Krugman

If $x holds a reference to a typeglob, I can access, say, the scalar
slot in the typeglob with ${*$x}. But how do I access the filehandle
slot *$x ?

Thanks!

jill
 
B

Ben Morrow

J Krugman said:
If $x holds a reference to a typeglob, I can access, say, the scalar
slot in the typeglob with ${*$x}. But how do I access the filehandle
slot *$x ?

*{*$x}{IO}

Ben
 
J

J Krugman

*{*$x}{IO}

Thanks! I can retrieve the contents of the filehandle slot with
*{*$x}{IO}, but it looks like Perl won't let me assign to it. It
complains with "Can't modify glob elem in scalar assignment". Is
there any way to set the filehandle slot through a typeglob ref?

jill

P.S. Is this documented anywhere?
 
B

Ben Morrow

J Krugman said:
Thanks! I can retrieve the contents of the filehandle slot with
*{*$x}{IO}, but it looks like Perl won't let me assign to it. It
complains with "Can't modify glob elem in scalar assignment". Is
there any way to set the filehandle slot through a typeglob ref?

s/through a typeglob ref//;

No. You definitely have an X-Y problem here: what are you trying to achieve?
P.S. Is this documented anywhere?

The *foo{THING} syntax is documented in perlref.

Ben
 
J

J Krugman

s/through a typeglob ref//;

OK, how do you do it through if you have the typeglob, not just a
reference to it?
You definitely have an X-Y problem here: what are you trying to achieve?

I'm trying to find a way to temporarily reassign DB::OUT in perldb.
Everything I've tried has failed...

The reason I want to reassign DB::OUT is that I want to capture
the output of certain perldb commands (e.g. x or V) to a file.

jill

P.S. "X-Y problem"???
 
B

Ben Morrow

J Krugman said:
OK, how do you do it through if you have the typeglob, not just a
reference to it?

Sorry, I seem to have confused you... the intention was that you cannot
assign to the filehandle slot at all, whether through a typeglob ref or
not.
I'm trying to find a way to temporarily reassign DB::OUT in perldb.
Everything I've tried has failed...

The reason I want to reassign DB::OUT is that I want to capture
the output of certain perldb commands (e.g. x or V) to a file.

open my $SAVE_DBOUT, '>&=', DB::OUT;
open DB::OUT, '>', 'file';
....
open DB::OUT, '>&=', $SAVE_DBOUT;

If you're not using 5.8 you'll need to change those to

open my $SAVE_DBOUT, '>&=' . fileno DB::OUT;

..
P.S. "X-Y problem"???

This a common expression here... it means 'you asked how to do X, but
you're actually trying to do Y and X is not the way to go about it'. The
implication is that we'll be better able to help you if you tell us what
Y is.

Ben
 
J

J Krugman

open my $SAVE_DBOUT, '>&=', DB::OUT;
open DB::OUT, '>', 'file';
...
open DB::OUT, '>&=', $SAVE_DBOUT;
If you're not using 5.8 you'll need to change those to
open my $SAVE_DBOUT, '>&=' . fileno DB::OUT;

Many thanks! This is great to know.
This a common expression here... it means 'you asked how to do X, but
you're actually trying to do Y and X is not the way to go about it'. The
implication is that we'll be better able to help you if you tell us what
Y is.

Ironically, I originally posted precisely Y (see the post "How to
reassign DB::OUT?"), but I got zero replies... Come to think of
it, X-Y problems would be the natural outcome from such situations
(i.e. you get no replies when you ask how to do Y, so you try your
desperate best guess, which requires doing X; it doesn't work, so
you end up asking about X).

jill
 
M

Michele Dondi

open my $SAVE_DBOUT, '>&=', DB::OUT;
open DB::OUT, '>', 'file';
...
open DB::OUT, '>&=', $SAVE_DBOUT;

If you're not using 5.8 you'll need to change those to

open my $SAVE_DBOUT, '>&=' . fileno DB::OUT;

Hey, I hope you won't think I'm starting some sort of personal
competition, but... may this be an occasion you were sloppy enough to
give me a chance of saying: aren't we checking if open()s succeeded
any more?!?
;-)


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top