Perl Fundamentals

Running a 'hello $name' Perl script in the Terminal

Nothing doing lately, me. Some notes I made during lunch today, when I tried to catch up with university work rather than go out and walk around, or take a break of some sort. It gave me a headache eventually but at least I made some sort of a dent in the university workload.

Some basic notes on Perl:

  • Scalar variables use $
  • Simple arrays start with @
  • Associative arrays start with %
  • @ARGV contains the command line arguments
    • @ARGV[0] will call the first argument stored in the array
  • User input through
  • chomp removes any newlines at the end of STDIN
  • use open to open files for input (reading) and output (writing)
    • These are assigned handles, e.g.: (open INFILE, ‘file1’) || die “can’t open the file $!\n”;
    • while() → successively takes lines from INFILE and assigns them to $_, returning true until no more lines left in file
    • $_ is default input and pattern searching space (if variable not specified, assumed $_)
  • || die caters for any errors in opening the files

And some code examples:

  • whereis perl → Perl on your system
  • print “hello $name”; → using variables
  • $first_argument = $ARGV[0]’
print "please enter your name: ";
$_ = ;
print "hello $_\n";

if then else statements

        do some stuff;
        do some other stuff;
        do something else;

while statements

        do something;

foreach statement

foreach $anElement (@AnArray)
        do something;
        you are free to use the variable anElement;

And… that’s as far as I got with that. Won’t have time to do much more before class tomorrow, but hopefully will be able to catch up sometime before the quiz next week. Wish me luck.