Friday, February 4, 2011

PERL : Subrotines

pico sub1.pl
#!/usr/bin/perl -w
use strict;
my $firstname = "Mark";
my @array = ("Mark","Sloan");
testsub();
sub testsub
{
my $name = "Mark";
print $name;
}
#END
@_ -> array that stores all parameters passed to the subrutine
testsub("Mark". "Sloan");
sub testsub
{
foreach (@_)
{
print $_;#Prints Mark and Sloan
}
}


sub testsub
{
prints @_;#prints both values
}


nano sub2.pl
#!/usr/bin/perl
use warnings;
use strict;
open (han1, ">>logfile.sub" || die "errors : $!");
my $etcdir = `ls -l /etc`;
my $etcdir2 = `ls -l /etc/passwd`;
chomp $etcdir2;
my $message = "Launching sub2.pl";
log_message("$message");
log_message("$etcdir"); #->prints /etc dir
sub log_message
{
$current_time = localtime;
print "$current_time - $_[0]","\n";
print han1 "$current_time - $_[0]","\n"; # prints to file
}
chmod u+x sub2.pl

No comments:

Post a Comment