Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

unlink $file OR... OR..

by Anonymous Monk
on Aug 30, 2007 at 03:40 UTC ( [id://635980]=perlquestion: print w/replies, xml ) Need Help??

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

Sorry if this is so trivial but I've never done something like this and I'm sure it's easy.

I'm unlinking a file and need to print to the screen AND push(@list, $file) if it fails all in one go.

I don't suppose something like this would be the best way? It's untested but I'm sure it works but still, I don't really care to use a sub for this.

unlink $file or &oopsies($file); sub oopsies { print "Error!"; push(@list, shift); #<-- heck, I've never tried using SHIFT like th +is- so intriguing I'm going to test that. }

Replies are listed 'Best First'.
Re: unlink $file OR... OR..
by GrandFather (Saint) on Aug 30, 2007 at 03:49 UTC

    Why not:

    unless (unlink $file) { print "..."; push @list, $file; }

    which at least avoids using a global array in the sub. If you need to do it a lot put the unlink into a sub:

    DoDaUnlink ($file, \@list); sub DoDaUnlink { my ($file, $list) = @_; return if unlink $file; print "..."; push @$list, $file; }

    DWIM is Perl's answer to Gödel
Re: unlink $file OR... OR..
by bobf (Monsignor) on Aug 30, 2007 at 03:45 UTC

    Perhaps a do block would suffice?

    unlink $file or do { print "Error!"; push( @list, $file ); };
    You could accomplish this using a series of and (or &&, see perlop) and parentheses, but IMO the do block is more clear.

Re: unlink $file OR... OR..
by sulfericacid (Deacon) on Aug 30, 2007 at 03:43 UTC
    Aye, you can use shift as though it's a variable. I do many a print shift;. It saves on typing and stress of coming up with yet ANOTHER unique variable name.

    UPDATE: Adding code for the original question

    #!/usr/bin/perl use warnings; use strict; my @list; push(@list, "blah.txt"), print qq(Error\n) unless unlink "blah.txt"; print @list;


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: unlink $file OR... OR..
by kyle (Abbot) on Aug 30, 2007 at 03:50 UTC

    What's wrong with using a sub? If you really don't want to, make it a block:

    if ( ! unlink $file ) { warn "Can't unlink '$file': $!\n"; push @unlink_failures, $file; }

    If you want it shorter, maybe:

    @unlink_failures = grep { ! unlink } @files_to_unlink; warn "unlink failed for '$_'\n" for @unlink_failures;

    I guess that could be all one line (though I haven't tried this) this way:

    warn "unlink failed for '$_'\n" for ( @unlink_failures = grep { ! unlink } @files_to_unlink );

    ...but that's getting pretty convoluted (and it loses the error messages from $!). I recommend the block.

Re: unlink $file OR... OR..
by Prof Vince (Friar) on Aug 30, 2007 at 13:58 UTC
    For the sake of completeness, you can also use an anonymous sub :
    unlink $file or sub { print 'Error!'; push @list, $file; }->();
    Using unless or a do block would be the preferred way though.
    Also, don't call subs with & unless you've read perlsub and you've understood what it does.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://635980]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-16 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found