Total Pageviews

Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Friday, April 13, 2012

repetition operator in perl

Another very handy operator is the repetition operator. Let's say you want to underline some output. You might be tempted to code it like this:
    $underline = '--------------------';
    print $underline;
But Perl lets you define a single character and then how many times you want to repeat it:
    $underline = '-' x 20;
    print $underline; # displays '--------------------'

Dealing with files/directories in Perl

Operator     Function                          
-e       Tests if OPERAND exists.          
-f       Tests if OPERAND is a regular file as opposed to a directory,symbolic link or other type of file
-l       Tests if OPERAND is a symbolic link
-s       Returns size of OPERAND in bytes  
-d       Tests if OPERAND is a directory.  
-T       Tests if OPERAND is a text file.  
 
Example:
#!/usr/bin/perl
if(-e "/tmp/test.pl")
 {
     print "File/directory exists.";
 }
 if(-f "/tmp/test.pl")
 {
     print "File exists";
 }
 $size = -s "/tmp/test.pl";
 print "Size of /tmp/test.pl is $size bytes";

Create an Array of Specific Numbers in Perl

@ft=(1 .. 14);
foreach $h(@ft) {
print "$h ";
}
Which Prints:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-We created an array and named it ft because we don't like long names.
-We stored 1 through 14 in ft The two periods basically say "through".
-We used Perl's built-in foreach loop to loop over the array and print.
Free Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
@ft=(1 .. 14);
foreach $h(@ft) {
print "$h ";
}
exit;

Making a Directory Using mkdir in Perl

mkdir("hohofiles", 0777) || print $!;
This will make a directory named hohofiles in the current directory.
-We called Perl's built-in mkdir function.
-We enclosed the name we'd like our directory to have in quotation marks. This could have also included the path to our new directory.
-We set permissions to 0777. (Most likely your directory will have permissions of 755 due to a umask of 22.)
-We included two vertical bars or pipes, which mean "or".
-We said to print any error we might receive. The errors are contained in the built-in variable $!
-Summary: make our directory or print errors.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
mkdir("hohofiles", 0777) || print $!;
mkdir("hohofiles/jiji", 0777) || print $!;
print " -Done";
exit;

Delete a File Using unlink in Perl

unlink("hohofiles/koko.html");
-We called Perl's built-in unlink function.
-We enclosed the path to the file we want deleted in quotation marks.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
unlink("hohofiles/koko.html") || print $!;
print " -Done";
exit;

Use chmod in Perl

chmod(0777, "hohofiles") || print $!;
This will change the permissions of our directory hohofiles to 777.
-We called Perl's built-in chmod function.
-We set permissions to 777.
-We enclosed the name of the thing to be chmoded in quotation marks. This could have also included the path to hohofiles if it was in a different directory than our script.
-We included two vertical bars or pipes, which mean "or".
-We said to print any error we might receive. The errors are contained in the built-in variable $!
-Summary: chmod our directory or print errors.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
chmod(0777, "hohofiles") || print $!;
print " -Done";
exit;