Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Re: read/write subroutine

by epoptai (Curate)
on Jun 29, 2001 at 14:09 UTC ( #92577=note: print w/replies, xml ) Need Help??

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

No apology needed grinder. I appreciate the attention to detail and sound advice you provide. Your previous comments on reputer have caused a measurable improvement in my coding habits.

My only excuse for not using ref or wantarray is not being familiar enough with them to have known to use them. The mode parameter did become more useful when extending the sub. I posted a simpler version of the one I use, which includes this write line. But $mode is now gone and this just checks for a 4th param:

print IO Data::Dumper->new([$data],[$name])->Indent(2)->Quotekeys(0)-> +Dump if $name;
By the way your wantarray line wouldn't work until I added returns:
wantarray ? return @file : return join '', @file;
Thanks again, I've updated the snippet with your improvements.

Replies are listed 'Best First'.
Re: Re: Re: read/write subroutine
by damian1301 (Curate) on Jun 29, 2001 at 23:47 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://92577]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-04-24 23:27 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (447 votes). Check out past polls.