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

Hi I want to make a simple wrapper subroutine which delete a file and delete file.gz too.

sub unlink{# new unlink my $file_name=shift; unlink "$file_name"; #original unlink unlink "${file_name}.gz"; #original unlink }

How can I do this properly?

Replies are listed 'Best First'.
Re: How to wrap a subroutine
by AnomalousMonk (Archbishop) on Feb 06, 2015 at 00:00 UTC

    The first rule of "How to wrap a subroutine": don't name it the same as a Perl built-in function (without a really good reason). This is such a bad idea! You are digging yourself a pitfall and seeding it with nasty booby-traps. The most likely ultimate result is a bamboo stake up a very sensitive part of your anatomy. Please call it something like  delete_file_and_zip() so I can sleep peacefully tonight.

    Otherwise, the logic of the function looks ok.


    Give a man a fish:  <%-(-(-(-<

Re: How to wrap a subroutine
by LanX (Saint) on Feb 05, 2015 at 23:57 UTC
Re: How to wrap a subroutine
by eyepopslikeamosquito (Archbishop) on Feb 06, 2015 at 07:00 UTC

    Minor nit:

    unlink "$file_name"; # you don't need the quotes here unlink $file_name; # ... so suggest you write it like this

    More importantly, you are currently ignoring all unlink failures. Are you happy to ignore them? What are your error handling requirements?

    If you simply want to write a new function, please don't call it unlink.

    OTOH, if you really, truly want to override the built-in unlink function, that is an XY Problem and you need to tell us the real underlying problem you are trying to solve, why you think overriding unlink is a good way to solve it, and what alternatives you have considered.

Re: How to wrap a subroutine
by i5513 (Pilgrim) on Feb 06, 2015 at 07:44 UTC

    Hello,

    I think you don't need to wrap unlink

    unlink can remove both files with a uniq call, glob can help you to get the correct arguments::

    unlink glob("${your_variable}{,.gz}");

    Like other monks told, maybe you need to explain why do you need override a default function (not a good idea if you have not a strong reason)

    Regards,
Re: How to wrap a subroutine
by Anonymous Monk on Feb 05, 2015 at 23:34 UTC
    why are you calling it unlink?
Re: How to wrap a subroutine
by Anonymous Monk on Feb 05, 2015 at 23:58 UTC
    Your question is unclear. If you're asking how to override the built-in unlink, see perlsub and CORE. But maybe try to explain instead what you actually want your program to achieve and what difficulties you're having with it.