einerwitzen has asked for the wisdom of the Perl Monks concerning the following question:

yes i've looked around but i still don't understand much, i wrote this up and I don't understand why it doesn't work. No matter what $func is, it only runs the first if-statement. Maybe give me an example of how to do this without if/thens or point out my dumb mistake :)

#!/usr/bin/perl use CGI; $query = new CGI; $func = $query->param("func"); $file = $query->param("file"); @text = $query->param("text"); print $query->header ( ); if ($func == "vi") { if (-1 != index($file, '.')) { open (FILE, ">/home/3dwccom/www/$file") || die "file must have .extension"; print FILE @text; close (FILE); print "File created successfully!"; } else { die "name must have a . in it."; } } elsif ($func == "edit") { open (PAGE, ">../$file") || die print "couldn't open file"; print PAGE @text; close (PAGE); print "File successfully edited"; } elsif ($func == "rm") { `rm /home/3dwccom/www/$file` || die "couldn't remove file"; print "Success"; } else { print "there was an error"; }


Multiple forms submit to this so I'd like to keep only the listed variable, not different ones for each part.
  • Comment on How can I run only a determined part of script not using if/then statements
  • Download Code

Replies are listed 'Best First'.
Re: How can I run only a determined part of script not using if/then statements
by busunsl (Vicar) on Jan 15, 2002 at 12:01 UTC
    In addition to grep's answer:

    Adding use warnings; to your code will give you warnings upon errors like this.

    Additionally: using filenames entered by users is probably not a good idea. What if the filename is '../../*' and $func is 'rm'?

    Do yourself a favour and read a bit about taint.

    Also consider using strict and error messages that give you more information than just that something failed.

Re: How can I run only a determined part of script not using if/then statements
by grep (Monsignor) on Jan 15, 2002 at 11:37 UTC
    '==' is a numeric comparison. You want 'eq' for text.

    Change
    if ($func == "vi")
    for
    if ($func eq "vi")
    and you are set.

    also you should check out perlop

    UPDATE: PLEASE follow busunsl's suggestions I would also suggest Ovid's CGI Course it addresses many security concerns.

    grep
    grep> cd pub grep> more beer
Re: How can I run only a determined part of script not using if/then statements
by hakkr (Chaplain) on Jan 15, 2002 at 18:11 UTC
    You can use a hash of function refs to simulate a switch statement. You'll need to break your blocks of code into subroutines.
    #!/usr/bin/perl use CGI; use strict; use warnings; my $query = new CGI; my $func = $query->param("func"); my $file = $query->param("file"); my @text = $query->param("text"); print $query->header ( ); my %functions=('rm'=>\&rm, 'edit'=>\&edit, 'vi'=>\&vi); #call your block of code $functions{$func}; sub rm { `rm /home/3dwccom/www/$file` || die "couldn't remove file"; print "Success"; } sub vi { open (FILE, ">/home/3dwccom/www/$file") || die "file must have .extension"; print FILE @text; close (FILE); print "File created successfully!"; } sub edit { open (PAGE, ">../$file") || die print "couldn't open file"; print PAGE @text; close (PAGE); print "File successfully edited"; }