Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re (tilly) 3 (comments): Supersplit

by jeroenes (Priest)
on Jan 02, 2001 at 04:26 UTC ( #49237=note: print w/ replies, xml ) Need Help??


in reply to Re (tilly) 3 (comments): Supersplit
in thread Supersplit

I have to ponder this for a while, and my time for perl is limited at the moment. But for the time being I will like to go into more detail about the filehandle stuff. I read CGI, and I understand the problem, because returns a variable containing both name and filehandle. I would guess that a check for reference should avoid this problem.

My assumption about the workings of $fh='INPUT'; join '',<$fh>; are based on perldoc:perlop, saying 'If the string inside the angle brackets is a reference to a scalar variable (e.g., <$foo>), then that variable contains the name of the filehandle to input from, or a reference to the same.'.

I understand the argument about the pipe and stuff, and I think a open.. "$fh" || open .. "<$fh" should do the trick here. I don't have IO::Scalar here, I'll ponder that later.

You worry about a trying open and silently failing. Well, I tried it, and the code simply returns the name if the open failed (and there is nothing to split on) as the first element. That leaves room for a check.

Furthermore, the code returns a prepared string as well, if reading as a filehandle fails. I quickly glanced at IO::Scalar, and I think the ref check should allow that as well. I think the following code should work.

sub _text{ my $fh = shift; unless (defined($fh)) { $fh = \*STDIN; } if ( (! ref $fh) && ((open INPUT, "$fh") || open INPUT, "<$fh" )) +{ $fh = join '', <INPUT>; close INPUT; } no strict 'refs'; (join '', <$fh>) || $fh; }

Bye,

Jeroen
I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)


Comment on Re: Re (tilly) 3 (comments): Supersplit
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-07-14 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls