background preloader

Perl

Facebook Twitter

How do I list the files in a directory? You want a list of all the files, or all the files matching a certain pattern, or with a certain ending, in a directory The solution Reading directories is a bit like reading files. First you open the directory, then you read from it and then you close it. You use a directory handle much as you use a file handle. Step 1: Opening the directory To open the directory, we use a function called opendir. . #! Step 2: Reading the directory To read the files and directories in the directory we use the readdir function. readdir returns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context. While (my $file = readdir(DIR)) { print "$file\n"; } Step 3: Closing the directory We use the function closedir to close the directory once we are finished with it.

Closedir(DIR); Directory Listing #! See further down for a more compact way of doing this. Find the directories #! #! #! Glob #! Sprintf. Perl does its own sprintf formatting: it emulates the C function sprintf(3), but doesn't use it except for floating-point numbers, and even then only standard modifiers are allowed. Non-standard extensions in your local sprintf(3) are therefore unavailable from Perl. Unlike printf , sprintf does not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful.

Finally, for backward (and we do mean "backward") compatibility, Perl permits these unnecessary but widely-supported conversions: Note that the number of exponent digits in the scientific notation produced by %e , %E , %g and %G for numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). An explicit format parameter index, such as 2$ . One or more of: So: Perl Regular Expressions. Troubleshooters.Com and Code Corner Present Copyright (C) 1998-2001 by Steve Litt Without regular expressions, Perl would be a fast development environment. Probably a little faster than VB for console apps. With the addition of regular expressions, Perl exceeds other RAD environments five to twenty-fold in the hands of an experienced practitioner, on console apps whose problem domains include parsing (and that's a heck of a lot of them).

Regular expressions is a HUGE area of knowledge, bordering on an art. Rather than regurgitate the contents of the Perl documentation or the plethora of Perl books at your local bookstore, this page will attempt to give you the 10% of regular expressions you'll use 90% of the time. Note that for this reason we assume all strings to be single-line strings containing no newline chars. Simple String Comparisons The most basic string comparison is $string =~ m/sought_text/; $string =~ m/^sought_text/; Similarly, the $ operator indicates "end of string". . . Ississ. Exit. How to Read and Write Files in Perl - Tutorial on Reading and Writing Files in Perl. Next: Writing to a file in Perl Perl is an ideal language for working with files. It has the basic capability of any shell script, and some very advanced tools, like regular expressions, that make it infinitely more useful. In order to work with files, we first need to learn how to read and write to them.

Reading a file is done in Perl by opening a filehandle to a specific resource - in this case a file on our system. #! In order to work with this example, you'll need a file for our Perl script to read. Larry Curly Moe When you run the script, the output should be the same as the file itself. Open (MYFILE, 'data.txt'); Then we use a simple while loop to automatically read each line of the data file one at a time - this places the value of each line in the temporary variable $_ for one loop. while (<MYFILE>) { Inside the loop, we use the chomp function to clear off the newlines from the end of each line, then we print the value of $_ to show that it was read. chomp; print "$_\n"; close (MYFILE); Perl globbing tutorial - Reading a directory in perl, learn how to get a directory list. It's very simple to print a list of all files in a directory using the built-in Perl glob function.

Let's look over a short script that globs and prints a list of all files, in the directory containing the script itself: #! /usr/bin/perl -w @files = <*>; foreach $file (@files) { print $file . "\n"; } When you run the program, you'll see it output the filenames of all files in the directory, one per line. The glob is happening on the first line, as the <*> characters pulls the filenames into the @files array.

@files = <*>; Then we simply use a foreach loop to print out the files in the array. You can include any path in your filesystem between the <> marks. @files = </var/www/htdocs/*>; Or if you just want a list of the files with the extension .html: @files = </var/www/htdocs/*.html>; How to wait for a certain period of time in Perl on Linux.