Perl seekdir Function
This function sets the current position within DIRHANDLE to POS. The value of POS must be a value previously returned by telldir.
seekdir() function is similar to Unix seekdir() system call.
Following is the simple syntax for this function −
seekdir DIRHANDLE, POS
This function returns 0 on failure and 1 on success.
Following is the example code showing its basic usage, create one directory testdir inside /tmp −
#!/usr/bin/perl -w opendir(DIR, "/tmp"); print("Position without read : ", telldir(DIR), "\n"); $dir = readdir(DIR); print("Position after one read : ", telldir(DIR), "\n"); print "$dir\n"; seekdir(DIR,0); $dir = readdir(DIR); print "$dir\n"; print("Position after second read : " , telldir(DIR), "\n"); closedir(DIR);
When above code is executed, it produces the following result −
Position without read : 0 Position after one read : 4 . . Position after second read : 4