filehandle to variable problem

M

mathias

Greg said:
: like *main::FH or main::STDOUT or GLOB(0x80f60ac)

It's a filehandle. Why do you care what it looks like when you print
its internal representation? What did you expect to happen?

Greg
well i was waiting for bla instead of any of the strings i had.
can you give me a way then to get "bla" from this filehandle ?

thank you
Mathias
 
J

Jay Tilton

: Greg Bacon wrote:
:
: > : like *main::FH or main::STDOUT or GLOB(0x80f60ac)
: >
: > It's a filehandle. Why do you care what it looks like when you print
: > its internal representation? What did you expect to happen?
: >
: > Greg

: well i was waiting for bla instead of any of the strings i had.
: can you give me a way then to get "bla" from this filehandle ?

Your code is short enough that including it in the article is
preferable to making readers go out to the web to see it.

[grabbed from http://www.tiasarea.org/perl.html]
#!/usr/bin/perl -w
use strict;

sub runme {
my $fh=shift;
print $fh "bla\n"; }

my $var;
open FH, '>', \$var;
runme(\*FH);
$var = *FH;
close FH;

print "var: \n";
print ">".$var."<\n";

the output from this script is:
var:
*main::FH<

In Perl 5.8.0, "open FH, '>', \$var" will cause stuff printed to FH to
go into $var. Cut out the "$var = *FH;" line and look in $var for
your 'bla'.

In an older Perl, that form of open() does not have the same behavior.
It just ends up stringifying the scalar reference, creating a file
named like "SCALAR(0x1a653b4)".
 
M

mathias

Jay said:
: Greg Bacon wrote:
:
: > : like *main::FH or main::STDOUT or GLOB(0x80f60ac)
: >
: > It's a filehandle. Why do you care what it looks like when you print
: > its internal representation? What did you expect to happen?
: >
: > Greg

: well i was waiting for bla instead of any of the strings i had.
: can you give me a way then to get "bla" from this filehandle ?

Your code is short enough that including it in the article is
preferable to making readers go out to the web to see it.

[grabbed from http://www.tiasarea.org/perl.html]
#!/usr/bin/perl -w
use strict;

sub runme {
my $fh=shift;
print $fh "bla\n"; }

my $var;
open FH, '>', \$var;
runme(\*FH);
$var = *FH;
close FH;

print "var: \n";
print ">".$var."<\n";

the output from this script is:
var:
*main::FH<

In Perl 5.8.0, "open FH, '>', \$var" will cause stuff printed to FH to
go into $var. Cut out the "$var = *FH;" line and look in $var for
your 'bla'.

In an older Perl, that form of open() does not have the same behavior.
It just ends up stringifying the scalar reference, creating a file
named like "SCALAR(0x1a653b4)".
thank you for the help
i finally solved it out

Mathias
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top