0

Guys im really confused now. Im new to learning Perl. The book ive read sometimes do Perl codes and sometimes do Linux commands.

Is there any connection between them? (Perl codes and linux commands)

I want to open multiple files using Perl code, i know how to open a single file in Perl using:

open (MYFILE,'somefileshere');

and i know how to view multiple files in Linux using ls command.

So how to do this? can i use ls in perl? And i want to open certain files only (perl files) which dont have file extension visible (I cant use *.txt or etc. i guess)

A little help guys

2 Answers 2

2

Use system function to execute linux command, glob - for get list of files.

http://perldoc.perl.org/functions/system.html

http://perldoc.perl.org/functions/glob.html

Like:

my @files = glob("*.h *.m"); # matches all files with a .h or .m extension
system("touch a.txt"); # linux command "touch a.txt"
Sign up to request clarification or add additional context in comments.

3 Comments

can you edit your answer and use an example using perl codes. Please im new. i cant comprehend very well
but i cant see any file extension in my files. how to do that * pattern thingy?
you dont understand. my files are just plain "thisisjustanexamplefilename" not "thisisjustanexamplefilename.txt or .m or whatever dots there is" and they are perl files
0

Directory handles are also quite nice, particularly for iterating over all the files in a directory. Example:

opendir(my $directory_handle, "/path/to/directory/") or die "Unable to open directory: $!";

while (my $file_name = <$directory_handle>) {
  next if $file_name =~ /some_pattern/; # Skip files matching pattern
  open (my $file_handle, '>', $file_name) or warn "Could not open file '$file_name': $!";
  # Write something to $file_name. See <code>perldoc -f open</code>.
  close $file_handle;
}
closedir $directory_handle;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.