is there a way to do the same sort of thing without if then statements?

You can use references to subroutines in a hash, then check if the key exists.
The following example does not do any real error checking, security, or check any return values, but it should help point you in the correct direction.

#!/usr/bin/perl -w use strict; print "Starting.\n\n"; my %actions = ( 'open' => \&fileOpen, 'create' => \&fileCreate, 'delete' => \&Nono::fileDelete, 'test' => \&fileTest, ); while ( <DATA> ) { chomp; next if /^\s*$/; my ( $function, $file ) = split( /\s+/, $_, 2); print "Testing '$function' -> '$file' . . ."; exists( $actions{ $function } ) ? &{$actions{ $function }}( $file ) : print "\t'$function' does not exist\n"; } print "Done.\n\n"; exit(0); sub fileOpen { my $open = shift; ## open( BLAH, $open ) or die; ## actions ## close( BLAH ); print "\tGot file '$open' to open.\n"; return; } sub fileCreate { my $create = shift; ## open( BLAH, ">$create" ) or die; ## actions ## close( BLAH ); print "\tGot file '$create' to create.\n"; return; } package Nono; sub fileDelete { my $delete = shift; ## actions print "\tGot file '$delete' to delete.\n"; return; } package main; sub fileTest { my $test = shift; ## actions print "\tGot file '$test' to test.\n"; return; } __DATA__ open line1 test line2 create line3 test line 4 blah blah delete line5 open line 6 nothing line 7 fails xxx line8 fails too fail This will fail

In reply to Re: How can I run only part of a script determined by a posted variable? by gt8073a
in thread How can I run only part of a script determined by a posted variable? by einerwitzen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.