Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

((Conditional, Ternary Operator)) Specifying default and non-default values

by princepawn (Parson)
on Oct 02, 2000 at 22:31 UTC ( [id://34949]=perlquestion: print w/replies, xml ) Need Help??

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

This script was a candidate for YAPC BC2000 Script of the Year. Found scrawled on a cave wall, it determines the extension of a filename based on the "root" name:
@pod = qw(HostCycle Instinet index); $extension{index} = 'pod'; map { $infile_extension = $extension{$_} ? $extension{$_} : '.pm'; system "pod2html $_.$infile_extension > $_.html"; } @pod;
I was wondering if anyone would care to reduce the number of lines required to do this.
  • Comment on ((Conditional, Ternary Operator)) Specifying default and non-default values
  • Download Code

Replies are listed 'Best First'.
Re: Conditional, Ternary Operator - Specifying default and non-default values
by ncw (Friar) on Oct 02, 2000 at 22:42 UTC
    This script nicely demonstrates a bad use of map - if you are discarding the output of map then you should use for/foreach.

    You can tidy the ternary operator up too. I'd write something like (untested but optimised for brevity :-)

    $extension{index} = 'pod'; system "pod2html $_." . ($extension{$_} || 'pm') . " > $_.html" for (qw(HostCycle Instinet index));
    For real I'd put an or die after the system though ;-)

    Update: I did test it, and I got this result

    $ perl -
    [fx: cuts and pastes the code above]
    Can't open HostCycle.pm: No such file or directory
    Can't open Instinet.pm: No such file or directory
    Can't open index.pod: No such file or directory
    
    It works! (The error messages are from pod2html.)
(tye)Re: ((Conditional, Ternary Operator)) Specifying default and non-default
by tye (Sage) on Oct 02, 2000 at 22:49 UTC

    If you already have the very useful def in your toolbag:

    sub def { $_[0]= def($_[1],"") if ! defined $_[0]; return $_[0]; }
    then you can do this as:
    $ext{index} = 'pod'; for my $pod ( qw( HostCycle Instinet index ) ) { system "pod2html $pod" . def($ext{$pod},".pm") . " > $pod.html"; }

    def (short for "define" which would be too easy to confuse with "defined") is useful for avoiding warnings. You can use it like || as above or like ||=. It differs from these two operators in that it triggers on definedness rather than truthness.

    Note that def is so useful that even def uses it. ;)

            - tye (but my friends call me "Tye")
Re: ((Conditional
by cwest (Friar) on Oct 02, 2000 at 23:43 UTC
    my @pod = qw(HostCycle Instinet index); my %ext = ( index => 'pod' ); system "pod2html $_.".($ext{$_}?$ext{$_}:'pm')." > $_.html" foreach @p +od;
    Enjoy!
    --
    Casey
       I am a superhero.
    
Re: ((Conditional
by runrig (Abbot) on Oct 02, 2000 at 22:51 UTC
    Map in void context - bad. just use a for loop:
    @pod = qw(HostCycle Instinet index); $extension{index} = '.pod'; for (@pod) { # Assuming there's no '0' or blank extensions $infile_extension = $extension{$_} || '.pm'; # Otherwise use this #$infile_extension = exists $extension{$_} ? $extension{$_} : '.pm'; # Check status ? system "pod2html $_$infile_extension > $_.html" and die "pod2html fai +led: $!"; }
    I've just made it longer, oh well...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found