develooper Front page | perl.beginners | Postings from May 2007

Re: Passing arguments to subroutine

Thread Previous | Thread Next
Paul Lalli
May 31, 2007 06:33
Re: Passing arguments to subroutine
Message ID:
On May 31, 4:27 am, (Alma) wrote:
> I need to pass the result of prepare statement as an argument to the
> subroutine.
> sub abc()

The () there very specifically say "This subroutine takes no

> {
>         my $self= shift;
>         my($id,$title) = @_;

... and yet you're retrieving arguments from the subroutine.

>         my $sth1= $databasehandle->prepare("select file_path from xyz
> where id='$id' and title like '$title'");
>         my $res = $sth1->execute();
>         my @row = $sth1->fetchrow_array;
>         print @row;
>         &deleteposter_file(@row);

The & there specifically says to ignore the prototype (ie, how many
arguments the subroutine says it needs).  So first you tell your
subroutines "don't take any arguments", and then you tell them "ignore
what I just told you."  Stop doing that.

> }
> #-----------------Deletes from the File system
> sub delete_file()

Where the heck did this subroutine come from?  The subroutine you
called above is called deleteposter_file.  And you're again telling it
it takes no arguments, even though you passed arguments to it.

> {
>         my $self = shift;

What?  You're not calling this subroutine as a method.  Why are you
thinking it has an object reference as the first argument?

>         my $file_path = @_;

This sets $file_path equal to the number of arguments left in @_
(after you shifted off $self).  It does not set $file_path equal to
the first element of @_.  For that, you want either:
my ($file_path) = @_;
my $file_path = shift;
my $file_path = $_[0];

> # extract the file name
> #       my @parts = split('\/',$file_path);
> #       my $file =$parts[$#parts];
> #       #unlink($file);

Why are you commenting this out?  Why are you not checking for
errors?  Why are you not printing $file to see what it is?

unlink $file or die "Cannot unlink '$file': $!";

> #       if( -e "file" ) {

"file" is not the same thing as $file.

even if you change it to $file, you're looking for $file in the
current directory, even though you just determined it may not be
located in that directory, because you were originally passed a full

> #       system("rm $file_path");
>         unlink($file_path);

unlink $file_path or die "Cannot unlink '$file_path': $!";
> }
> abc is calling delete_file() . where it need to delete the file stored
> at the location mentioned in file_path.
> Its not giving me an error

You didn't ask it to.  system calls don't report errors unless you
check for an error and print the error out yourself.

Paul Lalli

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About