Skip to main content
added 98 characters in body; added 4 characters in body; added 2 characters in body; added 7 characters in body
Source Link
Tempire
  • 160
  • 5

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

sub print_result_xml { ... }
sub print_result_text { ... }
sub print_result_whatever { ... }

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample)(I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly (You're using 'use strict', aren't you? If not, for your own sanity, and for the children, start). In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome)(some might say awesome), you can replace the last 5 lines of the subroutine with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.orghttp://blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

sub print_result_xml { ... }
sub print_result_text { ... }
sub print_result_whatever { ... }

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 5 lines of the subroutine with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

sub print_result_xml { ... }
sub print_result_text { ... }
sub print_result_whatever { ... }

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly (You're using 'use strict', aren't you? If not, for your own sanity, and for the children, start). In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 5 lines of the subroutine with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow http://blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

added 108 characters in body; added 18 characters in body
Source Link
Tempire
  • 160
  • 5

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
} 

sub print_result_xml { ... }
sub print_result_text { ... }
sub print_result_whatever { ... }

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 35 lines of the subroutine with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 3 lines with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
} 

sub print_result_xml { ... }
sub print_result_text { ... }
sub print_result_whatever { ... }

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 5 lines of the subroutine with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

added 134 characters in body; deleted 8 characters in body
Source Link
Tempire
  • 160
  • 5

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 3 lines with:

    no strict 'refs';
    main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 3 lines with:

    no strict 'refs';
    main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

I find that Perl's flexibility can help you eliminate many IF/ELSIF/* code constructs, making code much easier to read.

sub print_result {
    my ($stats, $tolerance, $outformat) = @_;

    my $name = "print_result_$outformat";

    print "$outformat is not a valid format" and return
      if !main->can($name);

    no strict 'refs';
    &$name(@_);
}

**Walkthrough**
print "$outformat is not a valid format" and return if !main->can($name);

This checks the main namespace (I presume you're not using classes, given your code sample) for the $name subroutine. If it doesn't exist, print an error message and get out. The sooner you exit from a subroutine, the easier your code will be to maintain.

 no strict 'refs';

no strict 'refs' turns off warnings & errors that would be generated for creating a subroutine reference on the fly. In this case, since you've already checked for it's existence with main->can, you're safe.

 &$name(@_);

Now you don't need any central registry of valid formatting subroutines - just add a subroutine with the appropriate name, and your program will work as expected.

If you want to be super hip (some might say awesome), you can replace the last 3 lines with:

no strict 'refs';
main->can($name) and &$name(@_) or print "$outformat is not a valid format";

Whether you find that more readable or not is a simple personal preference; just keep in mind the sort of folk that will be maintaining your code in the future, and make sure to code in accordance with what makes the most sense to them.

Perl is the ultimate in flexibility, making it inherently the hippest language in existence. Make sure to follow blogs.perl.org and ironman.enlightenedperl.org to keep up with the latest in Modern::Perl.

On a separate note, it's Perl, not PERL. The distinction is important in determining reliable & up-to-date sources of Perl ninja-foo.

Source Link
Tempire
  • 160
  • 5
Loading