#!/usr/bin/perl print "Hello world\n"; # scalar $myvariable = "hello world"; # array @myarray = ( "one", "two", "three" ); # hash %myhash = ( "key1" => "value1", "key2" => "value2" ); ################# # lets get the variables out again # scalar: print "my scalar contains: $myvariable \n"; # array # single item: print "content of the first position: " . $myarray[0] . "\n"; # traversal: for ( $i = 0; $i <= $#myarray; $i++ ){ print "item on position: " . $i . " contains: " . $myarray[$i] . "\n"; } foreach $item ( @myarray ){ print "the item is: " . $item . "\n"; } # hashes # single item: print "The value belonging to key1, is: " . $myhash{"key1"} . "\n"; # traversal foreach $key ( keys %myhash ){ print "here is a value for $key: " . $myhash{$key} . "\n"; } # lets call a method: mymethod("here","are","my","argument"); ################################# # methods ( subroutines ) sub mymethod { # all the arguments ( parameters ) # @_ foreach $argument ( @_ ){ print "argument: " . $argument . "\n"; } } ################################# # # running external commands # 1. system( ) system("ls"); # 2. ``; @myoutput = `ls`; # 3. file descriptors open(LS,"ls |") or die "AAARGH $!\n"; while ( $line = ){ # do something with each line } close(LS); ################################ # # Files # ( > == writing, >> == appending ) # open a file, write lines to it: open(FILE,">myfile") or die "Fatal: $!\n"; open(FILE2,">>myfile_append") or die "Fatal: $!\n"; print FILE $myvariable . "\n"; print FILE2 $myvariable . "\n"; close(FILE); close(FILE2);