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

Re: [PBML] Subroutines

Expand Messages
  • Jenda Krynicky
    From: Octavian Rasnita ... They opened this years obfuscation contest already? :-) Just don t do that. Goto s should be considered
    Message 1 of 13 , Jan 3, 2003
    • 0 Attachment
      From: "Octavian Rasnita" <orasnita@...>
      > I would like to do something like:
      >
      > URL: foreach my $var1(@vars) {
      > &sub1($var1);
      > }
      >
      > sub sub1 {
      > my $var1 = shift;
      > &sub2(var1);
      > }
      >
      > sub sub2 {
      > my $var1 = shift;
      > if ($var1 == 3) {
      > next URL;
      > }
      > }
      >
      > It doesn't work because I guess the "next" function can't work to get
      > out of the subroutines. I also tried using the "goto" function but
      > with the same result.

      They opened this years obfuscation contest already? :-)

      Just don't do that.

      "Goto's should be considered harmfull"

      Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    • Hans Ginzel
      ... 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
      Message 2 of 13 , Jan 3, 2003
      • 0 Attachment
        On Fri, Jan 03, 2003 at 04:31:19PM +0200, Octavian Rasnita wrote:
        > I would like to do something like:

        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;
        # If the sub2 calling is the last statement, write
        # return sub2(var1);
        # subroutine calling returns defautly value of the last statement,
        # but _write_ return there, you know sub1 depends on what sub2
        # returns --- someone is interested on return vale.
        }

        sub sub2 {
        my $var1 = shift;
        $var1 == 3 and retun 1
        }

        See also goto &sub, which is quite different then goto LABEL (perldoc -f goto).

        Hans

        --
        You can find a Vim desktop calendar here: http://www.moolenaar.net/
        Happy Vimming in 2003!
      • Charles K. Clarkson
        ... I assume you re leaving out a lot of the guts to your subs. How about: foreach my $var1 ( @vars ) { next unless sub1( $var1 ); . . . } sub sub1 { my $var1
        Message 3 of 13 , Jan 3, 2003
        • 0 Attachment
          Octavian Rasnita [mailto:orasnita@...] asked:

          : I would like to do something like:
          :
          : URL: foreach my $var1(@vars) {
          : &sub1($var1);
          : }
          :
          : sub sub1 {
          : my $var1 = shift;
          : &sub2(var1);
          : }
          :
          : sub sub2 {
          : my $var1 = shift;
          : if ($var1 == 3) {
          : next URL;
          : }
          : }

          I assume you're leaving out a lot of the guts to your
          subs. How about:

          foreach my $var1 ( @vars ) {
          next unless sub1( $var1 );
          .
          .
          .
          }

          sub sub1 {
          my $var1 = shift;
          return unless sub2( $var1 );
          .
          .
          .
          return 1; # or some result
          }

          sub sub2 {
          my $var1 = shift;
          return if $var1 == 3;
          .
          .
          .
          return 1; # or some result
          }

          If $var1 in sub2() is 3, sub1 returns undef
          and we get the next $var1 in the foreach. Otherwise,
          processing continues.


          HTH,

          Charles K. Clarkson
          --
          Head Bottle Washer,
          Clarkson Energy Homes, Inc.
          Mobile Home Specialists
          254 968-8328
        • 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 4 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.