Sieve of Eratosthenes

 

When writing a statement for perl to interpret in a source file, there is a great deal of flexibility available to the programmer.

The basic perl statement follows one of the following syntaxes (brackets indicate optional elements):

  1. [scope declaration] [lvalue assignment_operator] EXPRESSION [conditional];

    This is the main type of statement that you will write in perl. It is composed of an (optional) lvalue (with an optional, but highly recommended scope declaration, usually 'my') and assignment operator (we will speak more of these later), followed by an expression of some form, terminated with the semi-colon. Expressions in perl can take on a similar variety of forms, and spread across multiple lines, with interspersed comments.

      print; #The simplest perl statement

      $variable = $array[0];

      my @array = ('one',

      'two',

      'three');

    print "HELLO"

    ,"WORLD\n";

    #Note above and below print different things, as newlines inside quotes are taken literally as part of the string

    print "HELLO

      WORLD\n";

    my @array = (

      "a",

      # this is the a

      "b", #this is the b

      "c"

      #this is the c

      ,"d" #this is the d

      );

    #Expressions and conditionals can also be spread across multiple lines

    my $value =

      (($a + $b)

      / $c

      )

       * $d;

    Note, some EXPRESSIONS will contain BLOCKS (see below). Conditionals are like flow control statements (see below) which allow the programmer to specify test statements which will prevent the EXPRESSION from being evaluated under certain conditions.

  2. flow_control [ [scope declaration] variable] (EXPRESSION) BLOCK

    This is the form of statement which allows a block of code to be conditionally executed depending on the evaluation of the flow_control statement and EXPRESSION. It may also use optional variables which are set up during each iteration of the flow_control statement (this is called an iterator pattern). We will talk more about flow_control statements later, this is just to get a feel for the variety present.

    foreach my $value (@values) { print "GOT VALUE $value\n"; }

    while (&we_have_data) {

      #get the data

      my $data =

      &get_data();

      print "${data}\n";

      }

    if (

      defined($value)

      && (length($value) > 0)

      &&!($value eq "NOT WANTED")

      ) {

      print "$value\n";

      }

  3. BLOCKS: these are logically defined units of perl statements defined to conduct a specific task. Local variables named with the same name as variables occuring elsewhere in the program can be created without conflict for the duration of the block, using 'my'. These variables are invisible to the rest of the program. Blocks can be defined within the context of a flow-control statement, a subroutine, as a block of code to be fed to list manipulation functions such as sort, grep, or map (see later), after evals (see later), or on their own to create local execution blocks where certain global aspects of the perl execution environment can be temporarily changed. They, too, can be all on one line, or separated across multiple lines, with interspersed comments.

    {

      #A local block where the strict pragma can be turned off for references (a bit advanced)

      local no strict refs;

      $value = undef;

    }

    map {

      $_ =~ s/\s+/@/g;

      } @values;

 


Page Information

  • 4 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts