Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Cheap idioms

by particle (Vicar)
on Oct 20, 2002 at 22:46 UTC ( #206726=note: print w/replies, xml ) Need Help??

in reply to Cheap idioms

i've been localizing handles with anonymous subroutines for a while now.

i think i'd write your snippet as an anonymous sub, giving it a little more muscle compared to a do block. something like:

#!/usr/bin/perl require 5.006_001; use strict; use warnings; $|++; my %cool_funcs = ( ## slurp a file to a scalar ## pass filename (as scalar) ## returns contents of file (scalar context) slurp_to_scalar => sub{ local( *ARGV, $/ ); @ARGV = @_; <> }, ## slurp one or more files to an array ## pass filename(s) (as scalar) ## returns contents of file(s) (list context) ## returns number of lines (scalar context) slurp_to_array => sub{ local *ARGV; @ARGV = @_; <> }, ); ## to use it: ## create a list of test files my @files = @ARGV; ## get the contents to an array, and the number of lines to a scalar my $no_of_lines = ( my @contents ) = $cool_funcs{slurp_to_array}->( @files ); ## these values match... print $no_of_lines, $/, scalar @contents, $/; ## here's the good stuff... print @contents; ## get file to scalar -- note extra args are ignored my $data = $cool_funcs{slurp_to_scalar}->( @files ); ## here's the file's contents... print $data;

~Particle *accelerates*

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://206726]
[Discipulus]: marto also Re^2: Catalyst:: Dispatcher - usage of expand_actions has signature polemic, and yesterday was quoted in CB
[marto]: Discipulus concerned partys should report things. as tye mentioned in his post (linked in the other thread from yesterday) there is an approach and strategy for such things

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-06-23 08:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (538 votes). Check out past polls.