http://www.perlmonks.org?node_id=1055481


in reply to To call a .pl file when a button is pressed on a GUI created using Perl CGI

  • Comment on Re: To call a .pl file when a button is pressed on a GUI created using Perl CGI

Replies are listed 'Best First'.
Re^2: To call a .pl file when a button is pressed on a GUI created using Perl CGI
by pratibha.blr (Initiate) on Sep 24, 2013 at 13:26 UTC

    One text file is present:

    system1,10.111.23.45,OFF,powered-off

    system2,10.111.23.46,ON,used

    system3,10.111.23.47,OFF,idle

    One CGI file is written which reads the above text file and prints on the GUI. If for system1 OFF is present in text file on GUI one button should be shown as "POWER ON" for this system and if ON is present one button is shown as "POWER OFF".

    Below is the cgi file:

    #!/usr/bin/perl use strict; #use CGI qw/:standard/; #use CGI qw( :html *p); use CGI qw(:cgi-lib :standard); &ReadParse(*input); use CGI qw( :html *p); use CGI::Pretty; print header, start_html('Utilization'); my $cgi_obj = new CGI(); my $power_on = $cgi_obj->param('power_on'); my $power_off = $cgi_obj->param('power_off'); if($power_off){ Change::change_file("INFO", "$power_off", "POWER_STATUS", "OFF"); } if($power_on){ Change::change_file("INFO", "$power_on", "POWER_STATUS", "ON"); } my $h = "<h3><font color=\"#0B0B61\"><center><b>System Management Page +</b></center></font color> </h3>"; $h .= "<table border=\"1\">\n<tr>\n";</p> $h .= "<td WIDTH=50 bgcolor=\"#0101DF\"><font color=\"#FFFFFF\ +"><strong><center>Serial No.</center></strong></td>\n"; $h .= "<td WIDTH=200 bgcolor=\"#0101DF\"><font color=\"#FFFFFF +\"><strong><center>NAME</center></strong></td>\n"; $h .= "<td WIDTH=200 bgcolor=\"#0101DF\"><font color=\"#FFFFFF +\"><strong><center>IP</center></strong></td>\n"; $h .= "<td WIDTH=200 bgcolor=\"#0101DF\"><font color=\"#FFFFFF +\"><strong><center>State_ON_OFF</center></strong></td>\n"; $h .= "<td WIDTH=200 bgcolor=\"#0101DF\"><font color=\"#FFFFFF +\"><strong><center>Used state</center></strong></td>\n"; $h .= "</tr>"; <p>open(my $FH,"INFO") or die ("$!");</p> <p> my $ref={};</p> <p>my $i = 1;</p> <p> while(<$FH>){</p> <p>my ($NAME,$IP,$POWER_OFF_ON,$USED_STATE)=split(/,/);</p> $h .= "<tr>"; $h .= "<td WIDTH=50 bgcolor=\"#BCF5A9\"><center>$i.</center></ +td>"; $h .= "<td WIDTH=150 bgcolor=\"#BCF5A9\"><center>$NAME</center +></td>"; $h .= "<td WIDTH=150 bgcolor=\"#BCF5A9\"><center>$IP</center>< +/td>"; if ( $POWER_OFF_ON eq "OFF"){ $h .= "<form name=\"ON\" method=\"get\" action=\"tool. +cgi\"><br>"; $h .= "<td WIDTH=250 bgcolor=\"#BCF5A9\"><center>$POWE +R_OFF_ON<br>"; $h .= "<input type=\"hidden\" name=\"power_off\" value= +\"$NAME\">"; $h .= "<center><input type=\"submit\" value=\"SWITCH ON +\"></center</td>"; $h .= "</form>"; } else { $h .= "<form name=\"OFF\" method=\"get\" action=\"tool. +cgi\"><br>"; $h .= "<td WIDTH=250 bgcolor=\"#BCF5A9\"><center>$POWER +_OFF_ON<br>"; $h .= "<input type=\"hidden\" name=\"power_ +off\" value=\"$NAME\">"; $h .= "<center><input type=\"submit\" value=\"SWITCH OF +F\"></center</td>"; $h .= "</form>"; } $h .= "<td WIDTH=150 bgcolor=\"#BCF5A9\"><center>$USED_STATE</ +center></td>"; $h .= "</tr>"; $i++; } print "$h"; print $cgi_obj->end_html(); exit;

    I want that if "POWER ON" button is pressed. It changes the INFO file and corresponding to that system it replaces OFF ny ON and the display the same cgi file with update data. Now as written in line 15 and line 18. one subroutine called change_file is called if "POWER ON" or "POWER OFF" buttons are pressed which changes the value as asked.

    Subroutine from the Change.pl

    my ($name, $selected_system, $value_to_change, $new_val) = @_; my $ref; my ($NAME,$IP,$POWER_OFF_ON,$USED_STATE); rename $name, "$name~" or die "Cannot rename: $!"; open FH1, "<$name~" or die "Cannot open: $!"; open FH2, ">$name" or die "Cannot create: $!"; while (<FH1>) { ($NAME,$IP,$POWER_OFF_ON,$USED_STATE)=split(/;/); $ref->{$NAME}{FILER_IP}=$FILER_IP; $ref->{$NAME}{FILER_RLM_IP}=$FILER_RLM_IP; $ref->{$NAME}{POWER_OFF_ON}=$POWER_OFF_ON; $ref->{$NAME}{OWNER}=$OWNER; $ref->{$NAME}{DETAILS}=$DETAILS; $ref->{$NAME}{RESERVED_BY}=$RESERVED_BY; print "$NAME",";","$ref->{$NAME}{FILER_IP}",";","$ref->{$NAM +E}{FILER_RLM_IP}",";","$ref->{$NAME}{POWER_OFF_ON}",";","$ref->{$NAME +}{OWNER}",";","$ref->{$FILER_NAME}{DETAILS}",";","$ref->{$FILER_NAME} +{RESERVED_BY}"; print "$NAME",";","$ref->{$NAME}{FILER_IP}",";","$ref->{$NAME +}{FILER_RLM_IP}",";","$ref->{$NAME}{POWER_OFF_ON}",";","$ref->{$NAME} +{OWNER}",";","$ref->{$FILER_NAME}{DETAILS}",";","$ref->{$FILER_NAME}{ +RESERVED_BY}"; if (!($NAME eq "$selected_system")){ print FH2 $_; } elsif ($value_to_change eq "POWER_STATUS"){ print FH2 "$NAME",",","$ref->{$NAME}{IP}",",","$new_val","," +,"$ref->{$NAME}{USED_STATE}","\n"; } }

      &ReadParse(*input);??? You should never use that

        OKK. So what shall I use instead of that?