Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ensembl
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ensembl-gh-mirror
ensembl
Commits
c2ca6444
Commit
c2ca6444
authored
13 years ago
by
Andy Yates
Browse files
Options
Downloads
Patches
Plain Diff
Easy line iterator
parent
b3c7051b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/Bio/EnsEMBL/Utils/IO.pm
+82
-8
82 additions, 8 deletions
modules/Bio/EnsEMBL/Utils/IO.pm
modules/t/utilsIo.t
+9
-0
9 additions, 0 deletions
modules/t/utilsIo.t
with
91 additions
and
8 deletions
modules/Bio/EnsEMBL/Utils/IO.pm
+
82
−
8
View file @
c2ca6444
...
...
@@ -70,6 +70,24 @@ Bio::EnsEMBL::Utils::IO
print $fh $$file_contents_ref;
return;
});
#Working with a set of lines manually
work_with_file('/my/file', 'r', sub {
my ($fh) = @_;
iterate_lines($fh, sub {
my ($line) = @_;
print $line; #Send the line in the file back out
return;
});
return;
});
#Doing the same in one go
iterate_file('/my/file', sub {
my ($line) = @_;
print $line; #Send the line in the file back out
return;
});
=head1 DESCRIPTION
...
...
@@ -95,12 +113,13 @@ use warnings;
use
base
qw(Exporter)
;
our
$GZIP_OK
=
0
;
our
@EXPORT_OK
=
qw/slurp slurp_to_array fh_to_array process_to_array work_with_file gz_slurp gz_slurp_to_array gz_work_with_file/
;
our
@EXPORT_OK
=
qw/slurp slurp_to_array fh_to_array process_to_array work_with_file gz_slurp gz_slurp_to_array gz_work_with_file
iterate_file iterate_lines
/
;
our
%EXPORT_TAGS
=
(
all
=>
[
@EXPORT_OK
],
slurp
=>
[
qw/slurp slurp_to_array gz_slurp gz_slurp_to_array/
],
array
=>
[
qw/fh_to_array process_to_array slurp_to_array gz_slurp_to_array/
],
gz
=>
[
qw/gz_slurp gz_slurp_to_array gz_work_with_file/
]
all
=>
[
@EXPORT_OK
],
slurp
=>
[
qw/slurp slurp_to_array gz_slurp gz_slurp_to_array/
],
array
=>
[
qw/fh_to_array process_to_array slurp_to_array gz_slurp_to_array/
],
gz
=>
[
qw/gz_slurp gz_slurp_to_array gz_work_with_file/
],
iterate
=>
[
qw/iterate_file iterate_lines/
],
);
use
Bio::EnsEMBL::Utils::
Exception
qw(throw)
;
use
Bio::EnsEMBL::Utils::
Scalar
qw(:assert)
;
...
...
@@ -254,7 +273,7 @@ sub fh_to_array {
return
\
@contents
;
}
=head process_to_array
=head
2
process_to_array
Arg [1] : Glob/IO::Handle $fh
Arg [2] : CodeRef $callback
...
...
@@ -272,12 +291,67 @@ sub process_to_array {
assert_file_handle
(
$fh
,
'
FileHandle
');
assert_ref
(
$callback
,
'
CODE
',
'
callback
');
my
@contents
;
while
(
my
$line
=
<
$fh
>
)
{
iterate_lines
(
$fh
,
sub
{
my
(
$line
)
=
@_
;
push
(
@contents
,
$callback
->
(
$line
));
}
return
;
});
return
\
@contents
;
}
=head2 iterate_lines
Arg [1] : Glob/IO::Handle $fh
Arg [2] : CodeRef $callback
Description : Iterates through each line from the given file handle and
hands them to the callback one by one
Returntype : None
Example : iterate_lines($fh, sub { print "INPUT: $_"; });
Exceptions : If the fh did not exist or if a callback was not given.
Status : Stable
=cut
sub
iterate_lines
{
my
(
$fh
,
$callback
)
=
@_
;
assert_file_handle
(
$fh
,
'
FileHandle
');
assert_ref
(
$callback
,
'
CODE
',
'
callback
');
while
(
my
$line
=
<
$fh
>
)
{
$callback
->
(
$line
);
}
return
;
}
=head2 iterate_file
Arg [1] : string $file
Arg [2] : string; $mode
Supports all modes specified by the C<open()> function as well as those
supported by IO::File
Arg [3] : CodeRef the callback which is used to iterate the lines in
the file
Description : Iterates through each line from the given file and
hands them to the callback one by one
Returntype : None
Example : iterate_file('/my/file', sub { print "INPUT: $_"; });
Exceptions : If the file did not exist or if a callback was not given.
Status : Stable
=cut
sub
iterate_file
{
my
(
$file
,
$callback
)
=
@_
;
work_with_file
(
$file
,
'
r
',
sub
{
my
(
$fh
)
=
@_
;
iterate_lines
(
$fh
,
$callback
);
return
;
});
return
;
}
=head2 work_with_file()
Arg [1] : string $file
...
...
This diff is collapsed.
Click to expand it.
modules/t/utilsIo.t
+
9
−
0
View file @
c2ca6444
...
...
@@ -47,10 +47,19 @@ my $expected_array = [qw/>X AAAAGGGTTCCC TTGGCCAAAAAA ATTC/];
is_deeply
(
slurp_to_array
(
$file
,
$chomp
),
$expected_array
,
'
Checking slurp to array with chomp
');
$chomp
=
0
;
is_deeply
(
slurp_to_array
(
$file
,
$chomp
),
[
map
{
$_
.
"
\n
"
}
@
{
$expected_array
}],
'
Checking slurp to array with chomp
');
my
$iterator_counter
=
0
;
iterate_file
(
$file
,
sub
{
my
(
$line
)
=
@_
;
chomp
(
$line
);
is
(
$line
,
$expected_array
->
[
$iterator_counter
++
],
sprintf
('
Checking line %d is ok
',
$iterator_counter
+
1
));
return
;
});
unlink
$file
;
dies_ok
{
slurp
(
$file
)
}
'
File no longer exists so die
';
}
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment