Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with call SUB with param

Status
Not open for further replies.

PasPy

Programmer
May 16, 2002
11
CA
when I call a sub, I check wich sub is called. When I call , I want to give the param 1001 to the sub delete. here my code, I don"t know how to make this . . .

$referer = $ENV{HTTP_REFERER};
if(($action eq "delete") && ($referer =~ "$scripturl")) {
&delete;
}
else {
&login;
}

#and here the sub delete

sub delete {

my $obj = new CGI;
my $todelete = $obj->param('delete');

#the message in the heat of the &main
$message = "The numbers $todelete was deleted";

#add the deleted numbers to the rest of the deleted numbers
push @did, $addoffers;
&main;

}
 
In order to pass parameters to a subroutine you do the following:

1) call the subroutine with the parameters
&mysub("bob");

2) retrieve the parameter from the array @_
Code:
   sub mysub {
       my $arg = shift; # Implied shift(@_);
       print "hello $arg \n";
   }


 
Here ya go.

Code:
(my $sub,my $argument) = split /=/,$ENV{QUERY_STRING}; # Get the name of the sub and the argument to pass to it.
$sub->($argument); # Execute the subroutine.

sub delete {
  my $todelete = shift;

  #the message in the heat of the &main
  $message = "The numbers $todelete was deleted";

  #add the deleted numbers to the rest of the deleted numbers
  push @did, $addoffers;
  &main;

}

This is untested, so if you are actually deleting files and stuff inside delete, make sure you make a backup of the file(s).


 
I have problem when I check wich sub is called when they have parameters ?

#here the code, it's in the delete and offer sub that I'va trouble, I'm not able to send param, check my code, you will understand, I wish.

(my $sub,my $argument) = split /=/,$ENV{QUERY_STRING}; $referer = $ENV{HTTP_REFERER};
if (($action eq "checklogin") && ($referer =~ "$scripturl")) {
&check_login;
}
elsif (($action eq "update") && ($referer =~ "$scripturl")) {
&update;
}
elsif (($action eq "delete=$argument") && ($referer =~ "$scripturl")) {
&delete;
}
elsif (($action eq "offer=$argument") && ($referer =~ "$scripturl")) {
&offer;
}
else {
&login;
}

Thanks againt
 
I'm not clear on how you're setting up your data. If I got the right impression before, you want the sub name to be on the left side of the equals sign and the argument on the right. Let's say you have two subs: delete and printit.
Here is an example of how to use a variable as a sub name:
Code:
sub delete {
  unlink $_[0];
}

sub printit {
  print $_[0];
}


$ENV{QUERY_STRING} = "delete=path/to/my/file";
($sub,$argument) = split /=/,$ENV{QUERY_STRING};
$sub->($argument); # This will delete the file specified as the argument

$ENV{QUERY_STRING} = "printit=hi there!";
($sub,$argument) = split /=/,$ENV{QUERY_STRING};
$sub->($argument); # This will print the argument.
Of course, you won't be setting $ENV{QUERY_STRING} manually; that variable refers to whatever is after the ? in the URL.

 
This is with this line I'va trouble;

elsif (($action eq "delete=$argument") && ($referer =~ "$scripturl")) {
&delete;
}

#$action = #Here the action"

so if I call " the $action variable = "delete=1001", so my code doesn't work !

(($action eq "delete=($argument)")

I want know wath a should put here when I call the sub...

when I call or
or

If $action = " ??????? " then call &delete

I'm not sure you will understand, but It hard to explain !

thank againt
 
Oh, I understand you now! Replace this line of code:
Code:
elsif (($action eq "delete=$argument") && ($referer =~ "$scripturl")) {
        &delete;
}

...with this:

Code:
elsif (($action =~ /^delete/) && ($referer =~ /$scripturl/)) { &delete; }

The first condition checks whether the action starts with the word "delete".

I assume you're trying to see if $scripturl is in $referer in the second condition? If so, you need to code it as above. You can't use quotes for a regular expression.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top