Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

act on a file list

by perl197 (Novice)
on Jun 30, 2014 at 14:58 UTC ( [id://1091733]=perlquestion: print w/replies, xml ) Need Help??

perl197 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to coble the code example below to perform work on the file name in the list. I have code that will pgp a single passed file name but I would like to loop through a file list and perform the same process against each filename in the list. How can i incorporate the $cmd component with the loop example such that each file_name in the list can be acted up on? I.e., i've tried substituting the print request with the cmd command but there's something fundamental i'm missing with this attempt

#code snippet that works in another script: $cmd = "gpg --recipient $gpg_key -q --yes -e $directory/$file_name"; $rc = system($cmd); #code loop example i'm having trouble combining with #!/usr/bin/perl -w use strict; my $file = "junk"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @file_name; while (<FH>) { push (@file_name, $_); } close FH or die "Cannot close $file: $!"; print @file_name; # see if it worked

Replies are listed 'Best First'.
Re: act on a file list
by Athanasius (Archbishop) on Jun 30, 2014 at 15:30 UTC

    Hello perl197, and welcome to the Monastery!

    Just a side note. The code:

    my @file_name; while (<FH>) { push (@file_name, $_); }

    can be replaced with the simpler and more idiomatic:

    my @file_name = <FH>;

    Here is the explanation, from readline:

    In scalar context, each call reads and returns the next line until end-of-file is reached, whereupon the subsequent call returns undef. In list context, reads until end-of-file is reached and returns a list of lines.

    In your code, while (<FH>) puts <FH> into scalar context1, so the while loop is needed to call <FH> repeatedly until the file is exhausted. But in the simpler version, assignment to an array variable (in this case, @file_name) puts the call to <FH> into list context, so <FH> is called only once and returns a list of all the lines in the file.

    Hope this is of interest,

    Update: 1Because of the implicit assignment to $_.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: act on a file list
by toolic (Bishop) on Jun 30, 2014 at 15:05 UTC
    Untested:
    for (@file_name) { chomp; my $cmd = "gpg --recipient $gpg_key -q --yes -e $directory/$_"; my $rc = system($cmd); }

      Thank-you much!...learning to crawl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1091733]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-03-28 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found