Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Subroutines

Expand Messages
  • Hans Ginzel
    ... # I for got this here: return 0; ... Or what about eval? I learned here, it is commonly used for error ... eval { sub1($var1) }; next if $@; # or better
    Message 1 of 13 , Jan 3, 2003
    • 0 Attachment
      On Fri, Jan 03, 2003 at 06:24:50PM +0100, Hans Ginzel wrote:
      > What about return values?
      >
      > foreach my $var1 (@vars) {
      > sub1($var1) and next;
      > }
      >
      > sub sub1 {
      > my $var1 = shift;
      > sub2(var1) and return 1;
      > # I expect some code here...
      > return 0;
      > }
      >
      > sub sub2 {
      > my $var1 = shift;
      > $var1 == 3 and retun 1
      # I for got this here:
      return 0;
      > }

      Or what about eval? I learned here, it is commonly used for error
      handling, but you can use it in this case as well:

      > foreach my $var1 (@vars) {
      eval { sub1($var1) };
      next if $@;
      # or better
      next if $@ =~ /^my_flag$/;
      > }
      >
      > sub sub1 {
      > my $var1 = shift;
      sub2(var1);
      # ...
      > }
      >
      > sub sub2 {
      > my $var1 = shift;
      $var1 == 3 and die "my_flag\n"; # be careful on "\n" here
      > }


      Note the differnce between eval "string" and eval { code; };
      Note the last semicolon is important, see perldoc -f eval.

      Best regards
      Hans

      --
      http://www.apmaths.uwo.ca/~xli/vim/vim_tutorial.html
    Your message has been successfully submitted and would be delivered to recipients shortly.