Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Re: read/write subroutine

by damian1301 (Curate)
on Jun 29, 2001 at 23:47 UTC ( #92786=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: read/write subroutine
in thread read/write subroutine

You can simplify that wantarray call by adding a return in front and then letting the ternary operators do the rest like so:

return wantarray ? @file : join '', @file;


And here is my slightly changed version of your code.
#!/usr/bin/perl sub io { # usage: # @array = io('read',$file) # $string = io('read',$file) # io('write',$file,\$string) # io('write',$file,\@array) my($bit,$file,$data) = @_; if($bit eq 'read'){ open IO,"< $file" or die "Cannot open $file for input: $!\n"; my @file = <IO>; close IO; return wantarray ? @file : join '', @file; } if($bit eq 'write'){ open IO,"> $file" or die "Cannot open $file for output: $!\n"; print IO ref $data eq 'ARRAY' ? @$data : ref $data eq "SCALAR"? $ +$data : ''; close IO; } } $string = "YO!"; io('write','file.txt',\$string);
This way everything is passed as references and it deletes an extra line of code. :)

$_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.


Comment on Re: Re: Re: read/write subroutine
Select or Download Code

Log In?
Username:
Password:

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

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

    The best computer themed movie is:











    Results (107 votes), past polls