home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


B.6 Dynamic Behavior

  1. Symbolic references:

    $i = "foo";
    $$i = 10;             # Sets $foo to 10
    ${"i"} = 10;          # Sets $foo to 10
    &$i();                # Calls foo();
    push (@$i, 10, 20);   # Pushes 10,20 into @foo
  2. Run-time expression evaluation:

    while (defined($str = <STDIN>)) {
        eval ($str);
        print "Error: $@" if $@;
    }

    This is a tiny Perl shell, which reads standard input, treats $str as a small program, and puts the compilation and run-time errors in $@ .

  3. Dynamic substitutions. Use the /e flag for the s/// operator, to specify an expression instead of a pattern:

    $l = "You owe me 400+100 dollars";
    $l =~ s/(\d+)\+(\d+)/$1 + $2/e;
    print $l; # prints "You own me 500 dollars"
  4. Module and object method invocations (see also # 20 and # 21 ):

    $modulename->foo(); # Calls foo() in the module indicated by
                        # $modulename


Previous: B.5 Objects Advanced Perl Programming Next: B.7 Exception Handling
B.5 Objects Book Index B.7 Exception Handling

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.