Perl as a tool in Astronomy

Making easy jobs easy

Ulrike Heiter

UAO

Click or press Enter or use controls (lower right) to view next slide

Outline

Introduction

Quote from "Programming Perl" book

Introduction

Basic concepts

Similar to shell scripts, no "main program", no beginning or end statements

Basic concepts

Example

# Read a list of star names and altitudes from a file,
# print names of selected stars

# Inititalize

@starlist = ();
%altitude = ();

# Read file

read_file(); # subroutine call

# Print selected stars

foreach $star (@starlist) {
   if ( select_star($star,$altitude{$star}) ) {
      print $star;
   }
} # -----  end foreach  -----

Example

sub read_file {
   open FILE, "< starfile";
   while (<FILE>) {
      push @starlist, $_;
      $altitude{$_} = <FILE>;
   }
   close FILE;
} 
Sirius
20
HD 10000
30
HD 20000
5
Vega
-5
HD 30000
50

Example

sub select_star {
   my ($name, $altitude) = @_;
   if ( ($name =~ /^HD/) and ($altitude > 10) ) {
      return 1; # anything else but "0" or "" is TRUE
   }
   else {
      return 0; # "0" is FALSE
   }
}
unix> perl example.pl
HD 10000
HD 30000

Link to starfile

More examples

More examples

Stellar Model Grid Tool (SMGT)

Perl Modules

Perl Data Language (PDL)s

Resources for further reading